| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 97.44% | 38 / 39 |  | 66.67% | 2 / 3 | CRAP |  | 0.00% | 0 / 1 | 
| ApieUpdateRecalculatingCommand |  | 97.44% | 38 / 39 |  | 66.67% | 2 / 3 | 9 |  | 0.00% | 0 / 1 | 
| __construct |  | 100.00% | 1 / 1 |  | 100.00% | 1 / 1 | 1 | |||
| configure |  | 100.00% | 1 / 1 |  | 100.00% | 1 / 1 | 1 | |||
| execute |  | 97.30% | 36 / 37 |  | 0.00% | 0 / 1 | 7 | |||
| 1 | <?php | 
| 2 | namespace Apie\Common\Command; | 
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContext; | 
| 5 | use Apie\Core\BoundedContext\BoundedContextHashmap; | 
| 6 | use Apie\Core\BoundedContext\BoundedContextId; | 
| 7 | use Apie\Core\ContextBuilders\ContextBuilderFactory; | 
| 8 | use Apie\Core\ContextConstants; | 
| 9 | use Apie\Core\Datalayers\ApieDatalayer; | 
| 10 | use Apie\Core\Datalayers\Search\QuerySearch; | 
| 11 | use Apie\Core\Entities\EntityInterface; | 
| 12 | use Apie\Core\Entities\RequiresRecalculatingInterface; | 
| 13 | use Apie\Core\Enums\ConsoleCommand; | 
| 14 | use Apie\Core\Lists\StringHashmap; | 
| 15 | use Apie\Core\ValueObjects\Utils; | 
| 16 | use ReflectionClass; | 
| 17 | use Symfony\Component\Console\Command\Command; | 
| 18 | use Symfony\Component\Console\Input\InputInterface; | 
| 19 | use Symfony\Component\Console\Input\InputOption; | 
| 20 | use Symfony\Component\Console\Output\OutputInterface; | 
| 21 | |
| 22 | class 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 | ContextConstants::DISABLE_CONTEXT_FILTER => true, | 
| 47 | ]); | 
| 48 | /** @var BoundedContext $boundedContext */ | 
| 49 | foreach ($this->boundedContextHashmap as $contextId => $boundedContext) { | 
| 50 | $subApieContext = $apieContext->registerInstance($boundedContext) | 
| 51 | ->withContext(ContextConstants::BOUNDED_CONTEXT_ID, $contextId) | 
| 52 | ->registerInstance(new BoundedContextId($contextId)); | 
| 53 | /** @var ReflectionClass<EntityInterface> $resource */ | 
| 54 | foreach ($boundedContext->resources as $resource) { | 
| 55 | if (in_array(RequiresRecalculatingInterface::class, $resource->getInterfaceNames())) { | 
| 56 | $offset = 0; | 
| 57 | $boundedContextId = new BoundedContextId($contextId); | 
| 58 | $output->writeln($resource->getShortName() . ' (' . $boundedContextId . ')'); | 
| 59 | $list = $this->apieDatalayer->all($resource, $boundedContextId); | 
| 60 | do { | 
| 61 | $chunk = $list->toPaginatedResult( | 
| 62 | new QuerySearch( | 
| 63 | $offset, | 
| 64 | $limit ?? self::CHUNKSIZE, | 
| 65 | textSearch: null, | 
| 66 | searches: null, | 
| 67 | orderBy: new StringHashmap(['timestamp' => 'ASC']), | 
| 68 | apieContext: $subApieContext | 
| 69 | ) | 
| 70 | ); | 
| 71 | $offset++; | 
| 72 | $stop = true; | 
| 73 | foreach ($chunk as $item) { | 
| 74 | /** @var RequiresRecalculatingInterface $item */ | 
| 75 | $output->write(sprintf('%40s', Utils::toString($item->getId()))); | 
| 76 | $stop = false; | 
| 77 | $this->apieDatalayer->persistExisting($item, $boundedContextId); | 
| 78 | $output->writeln(' Done'); | 
| 79 | } | 
| 80 | } while (!$stop || $limit !== null); | 
| 81 | } | 
| 82 | } | 
| 83 | } | 
| 84 | return Command::SUCCESS; | 
| 85 | } | 
| 86 | } |