Passed
Push — master ( 743cc2...6ffec3 )
by Oleg
02:50
created

ApiSuiteCommand::generateRoutes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 6
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;
10
use SlayerBirden\DFCodeGeneration\Generator\Controllers\DecoratedProvider;
11
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Delete;
12
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Get;
13
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Gets;
14
use SlayerBirden\DFCodeGeneration\Generator\Controllers\RelationsProviderDecorator;
15
use SlayerBirden\DFCodeGeneration\Generator\Controllers\UniqueProviderDecorator;
16
use SlayerBirden\DFCodeGeneration\Generator\Controllers\Update;
17
use SlayerBirden\DFCodeGeneration\Generator\Factory\Routes;
18
use SlayerBirden\DFCodeGeneration\Generator\Tests\Add as TestAdd;
19
use SlayerBirden\DFCodeGeneration\Generator\Tests\Delete as TestDelete;
20
use SlayerBirden\DFCodeGeneration\Generator\Tests\Get as TestGet;
21
use SlayerBirden\DFCodeGeneration\Generator\Tests\Gets as TestGets;
22
use SlayerBirden\DFCodeGeneration\Generator\Tests\Update as TestUpdate;
23
use SlayerBirden\DFCodeGeneration\Generator\Factory\SimpleProvider;
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Exception\InvalidArgumentException;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
use SlayerBirden\DFCodeGeneration\Generator\Controllers\SimpleProvider as ControllerSimpleProvider;
31
32
class ApiSuiteCommand extends Command
33
{
34
    /**
35
     * @var OutputInterface
36
     */
37
    private $output;
38
    /**
39
     * @var string
40
     */
41
    private $entityClassName;
42
    /**
43
     * @var bool
44
     */
45
    private $force;
46
    /**
47
     * @var bool
48
     */
49
    private $tests;
50
51
    protected function configure()
52
    {
53
        $this->setName('generate:api')
54
            ->setDescription('Api Suite for Entity.')
55
            ->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.')
56
            ->addArgument('entity', InputArgument::REQUIRED, 'Entity class')
57
            ->addOption('tests', 't', InputOption::VALUE_NONE, 'Whether to create tests.')
58
            ->addOption('force', 'f', InputOption::VALUE_NONE, 'Without force flag no writes happen (only output).');
59
    }
60
61
    protected function initialize(InputInterface $input, OutputInterface $output)
62
    {
63
        $this->output = $output;
64
        $this->entityClassName = $input->getArgument('entity');
65
        $this->force = $input->getOption('force');
66
        $this->tests = $input->getOption('tests');
67
68
        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

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