Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
ApieCreateDomainCommand
100.00% covered (success)
100.00%
41 / 41
100.00% covered (success)
100.00%
3 / 3
3
100.00% covered (success)
100.00%
1 / 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%
19 / 19
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2namespace Apie\Maker\Command;
3
4use Apie\Core\BoundedContext\BoundedContextHashmap;
5use Apie\Core\BoundedContext\BoundedContextId;
6use Apie\Core\Identifiers\PascalCaseSlug;
7use Apie\Core\Other\ActualFileWriter;
8use Apie\Core\Other\FileWriterInterface;
9use Apie\Maker\CodeGenerators\CreateDomainObject;
10use Apie\Maker\Dtos\DomainObjectDto;
11use Apie\Maker\Enums\IdType;
12use Symfony\Component\Console\Attribute\AsCommand;
13use Symfony\Component\Console\Command\Command;
14use Symfony\Component\Console\Input\InputArgument;
15use Symfony\Component\Console\Input\InputInterface;
16use Symfony\Component\Console\Input\InputOption;
17use Symfony\Component\Console\Output\OutputInterface;
18
19#[AsCommand(name: 'apie:create-domain-object', description: 'Create a new domain object.')]
20class ApieCreateDomainCommand extends Command
21{
22    public function __construct(
23        private readonly BoundedContextHashmap $boundedContextHashmap,
24        private readonly CreateDomainObject $createDomainObject,
25        private readonly FileWriterInterface $fileWriter = new ActualFileWriter(),
26    ) {
27        parent::__construct();
28    }
29
30    protected function configure(): void
31    {
32        $this->addArgument(
33            'name',
34            InputArgument::REQUIRED,
35            description: 'name of the new class',
36        );
37        $this->addOption(
38            'bounded-context',
39            'b',
40            InputOption::VALUE_REQUIRED,
41            description: 'Add to bounded context',
42            default: $this->boundedContextHashmap->getIterator()->key()
43        );
44        $this->addOption(
45            'id-type',
46            'i',
47            InputOption::VALUE_REQUIRED,
48            description: 'Type of the identifier',
49            default: IdType::Ulid->name
50        );
51    }
52
53    protected function execute(InputInterface $input, OutputInterface $output): int
54    {
55        $object = new DomainObjectDto(
56            new PascalCaseSlug($input->getArgument('name')),
57            new BoundedContextId($input->getOption('bounded-context')),
58            IdType::tryFromName($input->getOption('id-type')),
59            true
60        );
61        
62        $identifierPath = $this->createDomainObject->getIdentifierPath($object);
63        $output->writeln(sprintf('Creating "%s"', $identifierPath));
64        $this->fileWriter->writeFile(
65            $identifierPath,
66            $this->createDomainObject->generateDomainIdentifierCode($object)
67        );
68        $output->writeln('Done!');
69
70        $domainObjectPath = $this->createDomainObject->getDomainObjectPath($object);
71        $output->writeln(sprintf('Creating "%s"', $domainObjectPath));
72        $this->fileWriter->writeFile(
73            $domainObjectPath,
74            $this->createDomainObject->generateDomainObjectCode($object)
75        );
76        $output->writeln('Done!');
77        return Command::SUCCESS;
78    }
79}