Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
97.50% covered (success)
97.50%
39 / 40
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApieUpdateRecalculatingCommand
97.50% covered (success)
97.50%
39 / 40
66.67% covered (warning)
66.67%
2 / 3
9
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
97.37% covered (success)
97.37%
37 / 38
0.00% covered (danger)
0.00%
0 / 1
7
1<?php
2namespace Apie\Common\Command;
3
4use Apie\Core\BoundedContext\BoundedContext;
5use Apie\Core\BoundedContext\BoundedContextHashmap;
6use Apie\Core\BoundedContext\BoundedContextId;
7use Apie\Core\ContextBuilders\ContextBuilderFactory;
8use Apie\Core\ContextConstants;
9use Apie\Core\Datalayers\ApieDatalayer;
10use Apie\Core\Datalayers\Search\QuerySearch;
11use Apie\Core\Entities\EntityInterface;
12use Apie\Core\Entities\RequiresRecalculatingInterface;
13use Apie\Core\Enums\ConsoleCommand;
14use Apie\Core\Lists\StringHashmap;
15use Apie\Core\ValueObjects\Utils;
16use ReflectionClass;
17use Symfony\Component\Console\Command\Command;
18use Symfony\Component\Console\Input\InputInterface;
19use Symfony\Component\Console\Input\InputOption;
20use Symfony\Component\Console\Output\OutputInterface;
21
22class ApieUpdateRecalculatingCommand extends Command
23{
24    private const CHUNKSIZE = 2000;
25    public function __construct(
26        private readonly BoundedContextHashmap $boundedContextHashmap,
27        private readonly ApieDatalayer $apieDatalayer,
28        private readonly ContextBuilderFactory $contextBuilderFactory
29    ) {
30        parent::__construct('apie:recalculate-resources');
31    }
32    protected function configure(): void
33    {
34        $this->addOption('limit', 'l', InputOption::VALUE_OPTIONAL, 'limit number of resources to check');
35    }
36
37    protected function execute(InputInterface $input, OutputInterface $output): int
38    {
39        $limit = $input->getOption('limit');
40        if ($limit !== null) {
41            $limit = (int) $limit;
42        }
43        $apieContext = $this->contextBuilderFactory->createGeneralContext([
44            ConsoleCommand::class => ConsoleCommand::CONSOLE_COMMAND,
45            ConsoleCommand::CONSOLE_COMMAND->value => true,
46            Command::class => $this,
47            ContextConstants::DISABLE_CONTEXT_FILTER => true,
48        ]);
49        /** @var BoundedContext $boundedContext */
50        foreach ($this->boundedContextHashmap as $contextId => $boundedContext) {
51            $subApieContext = $apieContext->registerInstance($boundedContext)
52                ->withContext(ContextConstants::BOUNDED_CONTEXT_ID, $contextId)
53                ->registerInstance(new BoundedContextId($contextId));
54            /** @var ReflectionClass<EntityInterface> $resource */
55            foreach ($boundedContext->resources as $resource) {
56                if (in_array(RequiresRecalculatingInterface::class, $resource->getInterfaceNames())) {
57                    $offset = 0;
58                    $boundedContextId = new BoundedContextId($contextId);
59                    $output->writeln($resource->getShortName() . ' (' . $boundedContextId . ')');
60                    $list = $this->apieDatalayer->all($resource, $boundedContextId);
61                    do {
62                        $chunk = $list->toPaginatedResult(
63                            new QuerySearch(
64                                $offset,
65                                $limit ?? self::CHUNKSIZE,
66                                textSearch: null,
67                                searches: null,
68                                orderBy: new StringHashmap(['timestamp' => 'ASC']),
69                                apieContext: $subApieContext
70                            )
71                        );
72                        $offset++;
73                        $stop = true;
74                        foreach ($chunk as $item) {
75                            /** @var RequiresRecalculatingInterface $item */
76                            $output->write(sprintf('%40s', Utils::toString($item->getId())));
77                            $stop = false;
78                            $this->apieDatalayer->persistExisting($item, $boundedContextId);
79                            $output->writeln(' Done');
80                        }
81                    } while (!$stop || $limit !== null);
82                }
83            }
84        }
85        return Command::SUCCESS;
86    }
87}