Test Failed
Branch issue/#3 (7deea8)
by Koldo
05:23
created

AbstractMakerCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 46
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A __construct() 0 13 1
A execute() 0 32 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\DevTools\Application\Command;
6
7
use Antidot\DevTools\Application\Service\CreateClassFile;
8
use Antidot\DevTools\Application\Service\GetClassNameFromFQCN;
9
use Antidot\DevTools\Application\Service\GetNamespaceFromFQCN;
10
use Antidot\DevTools\Application\Service\GetRealPathFromNamespace;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Throwable;
16
17
use function sprintf;
18
19
abstract class AbstractMakerCommand extends Command
20
{
21
    public const NAME = self::NAME;
22
    protected const FQCN_ARGUMENT_DESCRIPTION = self::FQCN_ARGUMENT_DESCRIPTION;
23
    protected const TEMPLATE = self::TEMPLATE;
24
    protected const SUCCESS_HELP_TEMPLATE = self::SUCCESS_HELP_TEMPLATE;
25
    /** @var GetClassNameFromFQCN */
26
    protected $getClassNameFromFQCN;
27
    /** @var GetNamespaceFromFQCN */
28
    protected $getNamespaceFromFQCN;
29
    /** @var GetRealPathFromNamespace */
30
    protected $getRealPathFromNamespace;
31
    /** @var CreateClassFile */
32
    protected $createClassFile;
33
    /** @var array */
34
    protected $config;
35
36
    public function __construct(
37
        GetClassNameFromFQCN $getClassNameFromFQCN,
38
        GetNamespaceFromFQCN $getNamespaceFromFQCN,
39
        GetRealPathFromNamespace $getRealPathFromNamespace,
40
        CreateClassFile $createClassFile,
41
        array $config
42
    ) {
43
        $this->getClassNameFromFQCN = $getClassNameFromFQCN;
44
        $this->getNamespaceFromFQCN = $getNamespaceFromFQCN;
45
        $this->getRealPathFromNamespace = $getRealPathFromNamespace;
46
        $this->createClassFile = $createClassFile;
47
        $this->config = $config;
48
        parent::__construct();
49
    }
50
51
    protected function configure(): void
52
    {
53
        $this
54
            ->setName(static::NAME)
55
            ->addArgument(
56
                'fqcn',
57
                InputArgument::REQUIRED,
58
                static::FQCN_ARGUMENT_DESCRIPTION
59
            );
60
    }
61
62
63
    protected function execute(InputInterface $input, OutputInterface $output): ?int
64
    {
65
        /** @var string $fqcn */
66
        $fqcn = $input->getArgument('fqcn');
67
        $getClassNameFromFQCN = $this->getClassNameFromFQCN;
68
        $getNamespaceFromFQCN = $this->getNamespaceFromFQCN;
69
        $getRealPathFromNamespace = $this->getRealPathFromNamespace;
70
        $createClassFile = $this->createClassFile;
71
        try {
72
            $className = $getClassNameFromFQCN($fqcn);
73
            $namespace = $getNamespaceFromFQCN($fqcn);
74
            $classDir = $getRealPathFromNamespace($namespace);
75
            $realFilePath = $createClassFile(
76
                $classDir,
77
                $className,
78
                sprintf(static::TEMPLATE, $namespace, $className)
79
            );
80
        } catch (Throwable $exception) {
81
            $output->writeln(sprintf('<error>%s</error>', $exception->getMessage()));
82
            return 1;
83
        }
84
85
        $output->writeln(sprintf(
86
            '<info>Factory %s successfully created in file %s</info>',
87
            $className,
88
            $realFilePath
89
        ));
90
        $output->writeln(sprintf(
91
            static::SUCCESS_HELP_TEMPLATE,
92
            $this->config['config_dir'],
93
            $fqcn,
94
            $className
95
        ));
96
    }
97
}
98