Passed
Push — master ( 4826f7...743cc2 )
by Oleg
04:24
created

ApiSuiteCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 122
rs 10
c 0
b 0
f 0
ccs 0
cts 59
cp 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A generateControllerStack() 0 14 2
A generateRoutes() 0 6 2
A generateTests() 0 13 2
A execute() 0 11 3
A initialize() 0 8 1
A generateConfig() 0 6 2
A configure() 0 8 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DFCodeGeneration\Command;
5
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use SlayerBirden\DFCodeGeneration\Generator\Config\Config;
8
use SlayerBirden\DFCodeGeneration\Generator\Config\StandardProvider;
9
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Add\Add;
10
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Add\Delete;
11
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Add\Get;
12
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Add\Gets;
13
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Add\Update;
14
use SlayerBirden\DFCodeGeneration\Generator\Factory\Routes;
15
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Tests\Add as TestAdd;
16
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Tests\Delete as TestDelete;
17
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Tests\Get as TestGet;
18
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Tests\Gets as TestGets;
19
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Tests\Update as TestUpdate;
20
use SlayerBirden\DFCodeGeneration\Generator\Factory\SimpleProvider;
21
use Symfony\Component\Console\Command\Command;
22
use Symfony\Component\Console\Exception\InvalidArgumentException;
23
use Symfony\Component\Console\Input\InputArgument;
24
use Symfony\Component\Console\Input\InputInterface;
25
use Symfony\Component\Console\Input\InputOption;
26
use Symfony\Component\Console\Output\OutputInterface;
27
28
class ApiSuiteCommand extends Command
29
{
30
    /**
31
     * @var OutputInterface
32
     */
33
    private $output;
34
    /**
35
     * @var string
36
     */
37
    private $entityClassName;
38
    /**
39
     * @var bool
40
     */
41
    private $force;
42
    /**
43
     * @var bool
44
     */
45
    private $tests;
46
47
    protected function configure()
48
    {
49
        $this->setName('generate:api')
50
            ->setDescription('Api Suite for Entity.')
51
            ->setHelp('This command creates the full Api suite (CRUD + tests) for DataFlow Server Entity. Don\'t forget to use force flag if you want to write files.')
52
            ->addArgument('entity', InputArgument::REQUIRED, 'Entity class')
53
            ->addOption('tests', 't', InputOption::VALUE_NONE, 'Whether to create tests.')
54
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Without force flag no writes happen (only output).');
55
    }
56
57
    protected function initialize(InputInterface $input, OutputInterface $output)
58
    {
59
        $this->output = $output;
60
        $this->entityClassName = $input->getArgument('entity');
61
        $this->force = $input->getOption('force');
62
        $this->tests = $input->getOption('tests');
63
64
        AnnotationRegistry::registerLoader('class_exists');
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\Common\Annotati...istry::registerLoader() has been deprecated: this method is deprecated and will be removed in doctrine/annotations 2.0 autoloading should be deferred to the globally registered autoloader by then. For now, use @example AnnotationRegistry::registerLoader('class_exists') ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

64
        /** @scrutinizer ignore-deprecated */ AnnotationRegistry::registerLoader('class_exists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     * @throws \Doctrine\Common\Annotations\AnnotationException
70
     * @throws \ReflectionException
71
     */
72
    protected function execute(InputInterface $input, OutputInterface $output)
73
    {
74
        if (!class_exists($this->entityClassName)) {
75
            throw new InvalidArgumentException('Entity Class does not exist.');
76
        }
77
78
        $this->generateControllerStack();
79
        $this->generateRoutes();
80
        $this->generateConfig();
81
        if ($this->tests) {
82
            $this->generateTests();
83
        }
84
    }
85
86
    /**
87
     * @throws \Doctrine\Common\Annotations\AnnotationException
88
     * @throws \ReflectionException
89
     */
90
    private function generateTests(): void
91
    {
92
        $addBody = (new TestAdd($this->entityClassName))->generate();
93
        $deleteBody = (new TestDelete($this->entityClassName))->generate();
94
        $getBody = (new TestGet($this->entityClassName))->generate();
95
        $getsBody = (new TestGets($this->entityClassName))->generate();
96
        $updateBody = (new TestUpdate($this->entityClassName))->generate();
97
        if (!$this->force) {
98
            $this->output->write($addBody);
99
            $this->output->write($deleteBody);
100
            $this->output->write($getBody);
101
            $this->output->write($getsBody);
102
            $this->output->write($updateBody);
103
        }
104
    }
105
106
    private function generateConfig(): void
107
    {
108
        $configBody = (new Config(new StandardProvider($this->entityClassName)))->generate();
109
110
        if (!$this->force) {
111
            $this->output->write($configBody);
112
        }
113
    }
114
115
    /**
116
     * @throws \Twig_Error_Loader
117
     * @throws \Twig_Error_Runtime
118
     * @throws \Twig_Error_Syntax
119
     */
120
    private function generateRoutes(): void
121
    {
122
        $routesBody = (new Routes(new SimpleProvider($this->entityClassName)))->generate();
123
124
        if (!$this->force) {
125
            $this->output->write($routesBody);
126
        }
127
    }
128
129
    /**
130
     * @throws \Doctrine\Common\Annotations\AnnotationException
131
     * @throws \ReflectionException
132
     * @throws \Twig_Error_Loader
133
     * @throws \Twig_Error_Runtime
134
     * @throws \Twig_Error_Syntax
135
     */
136
    private function generateControllerStack(): void
137
    {
138
        $addBody = (new Add($this->entityClassName))->generate();
139
        $deleteBody = (new Delete($this->entityClassName))->generate();
140
        $getBody = (new Get($this->entityClassName))->generate();
141
        $getsBody = (new Gets($this->entityClassName))->generate();
142
        $updateBody = (new Update($this->entityClassName))->generate();
143
144
        if (!$this->force) {
145
            $this->output->write($addBody);
146
            $this->output->write($deleteBody);
147
            $this->output->write($getBody);
148
            $this->output->write($getsBody);
149
            $this->output->write($updateBody);
150
        }
151
    }
152
}
153