Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
ApieSeedCommand | |
100.00% |
23 / 23 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
configure | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | namespace Apie\Faker\Command; |
3 | |
4 | use Apie\Common\ApieFacade; |
5 | use Apie\Common\Wrappers\GeneralServiceFactory; |
6 | use Apie\Core\Actions\BoundedContextEntityTuple; |
7 | use Apie\Core\Attributes\FakeCount; |
8 | use Apie\Core\BoundedContext\BoundedContextHashmap; |
9 | use Apie\Faker\Seeders\ApieResourceSeeder; |
10 | use Symfony\Component\Console\Attribute\AsCommand; |
11 | use Symfony\Component\Console\Command\Command; |
12 | use Symfony\Component\Console\Input\InputInterface; |
13 | use Symfony\Component\Console\Input\InputOption; |
14 | use Symfony\Component\Console\Output\OutputInterface; |
15 | |
16 | #[AsCommand(name: 'apie:seed-entities', description: 'Seeds the Apie datalayer with faked domain objects.')] |
17 | class ApieSeedCommand extends Command |
18 | { |
19 | public function __construct( |
20 | private readonly BoundedContextHashmap $boundedContextHashmap, |
21 | private readonly ApieFacade $apieFacade |
22 | ) { |
23 | parent::__construct(); |
24 | } |
25 | |
26 | protected function configure(): void |
27 | { |
28 | $this->addOption('amount', 'a', InputOption::VALUE_REQUIRED, description: 'How many objects should be created', default: 10); |
29 | } |
30 | |
31 | protected function execute(InputInterface $input, OutputInterface $output): int |
32 | { |
33 | $seeders = []; |
34 | /** @var BoundedContextEntityTuple $tuple */ |
35 | foreach ($this->boundedContextHashmap->getTupleIterator() as $tuple) { |
36 | $counter = $input->getOption('amount'); |
37 | $attributes = $tuple->resourceClass->getAttributes(FakeCount::class); |
38 | foreach ($attributes as $attribute) { |
39 | $counter = $attribute->newInstance()->count; |
40 | } |
41 | if ($counter > 0) { |
42 | $seeders[] = new ApieResourceSeeder( |
43 | $tuple, |
44 | $counter |
45 | ); |
46 | } |
47 | } |
48 | $generator = GeneralServiceFactory::createFaker($seeders); |
49 | foreach ($seeders as $seeder) { |
50 | $index = 0; |
51 | $output->writeln($seeder->getResourceClass()->getShortName()); |
52 | do { |
53 | $resource = $seeder->getResource($generator, $index); |
54 | if ($resource) { |
55 | $this->apieFacade->persistNew($resource, $seeder->getBoundedContextId()); |
56 | } |
57 | $index++; |
58 | } while ($resource); |
59 | } |
60 | return Command::SUCCESS; |
61 | } |
62 | } |