Passed
Push — master ( 9b7912...982aac )
by Janis
02:15
created

GeneratorFeatureContext::addMySQLWriters()   C

Complexity

Conditions 14
Paths 5

Size

Total Lines 44
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 29
nc 5
nop 1
dl 0
loc 44
rs 6.2666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
3
namespace Janisbiz\LightOrm\Tests\Behat\Features\Dms\MySQL\Generator;
4
5
use Behat\Gherkin\Node\TableNode;
6
use Janisbiz\LightOrm\Dms\MySQL\Connection\ConnectionConfig as MySQLConnectionConfig;
7
use Janisbiz\LightOrm\Dms\MySQL\Generator\DmsFactory as MySQLDmsFactory;
8
use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\BaseEntityClassWriter;
9
use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\EntityClassWriter;
10
use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\RepositoryClassWriter;
11
use Janisbiz\LightOrm\Dms\MySQL\Generator\Writer\WriterConfig as MySQLWriterConfig;
12
use Janisbiz\LightOrm\Generator;
13
use Janisbiz\LightOrm\Generator\Writer\WriterInterface;
14
use Janisbiz\LightOrm\Tests\Behat\Bootstrap\AbstractFeatureContext;
15
16
class GeneratorFeatureContext extends AbstractFeatureContext
17
{
18
    /**
19
     * @var string
20
     */
21
    private $connectionName;
22
23
    /**
24
     * @var Generator
25
     */
26
    private $generator;
27
28
    /**
29
     * @var WriterInterface[]
30
     */
31
    private $writers = [];
32
33
    /**
34
     * @Given /^I create generator for connection "(\w+)"$/
35
     *
36
     * @param string $connectionName
37
     *
38
     * @throws \Exception
39
     */
40
    public function iCreateGeneratorForConnection(string $connectionName)
41
    {
42
        $this->connectionName = $connectionName;
43
44
        switch ($this->getConnectionConfig($this->connectionName)['adapter']) {
45
            case MySQLConnectionConfig::ADAPTER:
46
                $this->generator = new Generator(new MySQLDmsFactory());
47
48
                return;
49
        }
50
51
        throw new \Exception(\sprintf('Could not create generator for connection "%s"!', $this->connectionName));
52
    }
53
54
    /**
55
     * @Given I add writers to generator
56
     *
57
     * @throws \Exception
58
     */
59
    public function iAddWritersToGenerator()
60
    {
61
        $this->addWritersToGenerator();
62
    }
63
64
    /**
65
     * @Given /^I add writers to generator with directory override "(.*)"$/
66
     *
67
     * @param null|string $directoryOverride
68
     */
69
    public function iAddWritersToGeneratorWithDirectoryOverride(string $directoryOverride)
70
    {
71
        $directoryOverride = \preg_replace('/\/\\\/', DIRECTORY_SEPARATOR, $directoryOverride);
72
73
        $this->addWritersToGenerator($directoryOverride);
74
    }
75
76
    /**
77
     * @When I run generator
78
     */
79
    public function iRunGenerator()
80
    {
81
        $this->generator->generate($this->connectionPool->getConnection($this->connectionName), $this->connectionName);
82
    }
83
84
    /**
85
     * @Then /^Then I have following files generated:$/
86
     *
87
     * @param TableNode $files
88
     *
89
     * @throws \Exception
90
     */
91
    public function iShouldGetTheseRowsInDatabaseFilteredByScopeId(TableNode $files)
92
    {
93
        foreach ($this->normalizeTableNode($files) as $file) {
94
            $relativeFilePath = $file['path'];
95
            $absoluteFilePath = \implode(
96
                '',
97
                [
98
                    $this->rootDir,
99
                    DIRECTORY_SEPARATOR,
100
                    $relativeFilePath
101
                ]
102
            );
103
104
            if (!\file_exists($absoluteFilePath)) {
105
                throw new \Exception(\sprintf('File "%s" does not exist!', $relativeFilePath));
106
            }
107
        }
108
    }
109
110
    /**
111
     * @Given /^I remove directory "(.*)"$/
112
     *
113
     * @param string $directory
114
     */
115
    public function iRemoveDirectory(string $directory)
116
    {
117
        if (\is_dir($directory)) {
118
            $recursiveDirectoryIterator = new \RecursiveDirectoryIterator(
119
                $directory,
120
                \RecursiveDirectoryIterator::SKIP_DOTS
121
            );
122
            $files = new \RecursiveIteratorIterator(
123
                $recursiveDirectoryIterator,
124
                \RecursiveIteratorIterator::CHILD_FIRST
125
            );
126
            foreach ($files as $file) {
127
                if ($file->isDir()) {
128
                    \rmdir($file->getRealPath());
129
                } else {
130
                    \unlink($file->getRealPath());
131
                }
132
            }
133
            \rmdir($directory);
134
        }
135
    }
136
137
    /**
138
     * @param null|string $directoryOverride
139
     */
140
    private function addWritersToGenerator(string $directoryOverride = null)
141
    {
142
        switch ($this->getConnectionConfig($this->connectionName)['adapter']) {
143
            case MySQLConnectionConfig::ADAPTER:
144
                $this->addMySQLWriters($directoryOverride);
145
146
                return;
147
        }
148
149
        throw new \Exception(\sprintf('Could not add writers for connection "%s"!', $this->connectionName));
150
    }
151
152
    /**
153
     * @param null|string $directoryOverride
154
     *
155
     * @throws \Exception
156
     */
157
    private function addMySQLWriters(?string $directoryOverride)
158
    {
159
        foreach ($this->getWritersConfig($this->connectionName) as $writerClass => $writerConfig) {
160
            switch ($writerClass) {
161
                case BaseEntityClassWriter::class:
162
                    $this->writers[BaseEntityClassWriter::class] = new BaseEntityClassWriter(
163
                        new MySQLWriterConfig(
164
                            $directoryOverride ?: $writerConfig['directory'],
165
                            $writerConfig['namespace'],
166
                            !empty($writerConfig['classPrefix']) ? $writerConfig['classPrefix'] : '',
167
                            !empty($writerConfig['classSuffix']) ? $writerConfig['classSuffix'] : ''
168
                        )
169
                    );
170
171
                    break;
172
173
                case EntityClassWriter::class:
174
                    $this->writers[EntityClassWriter::class] = new EntityClassWriter(
175
                        new MySQLWriterConfig(
176
                            $directoryOverride ?: $writerConfig['directory'],
177
                            $writerConfig['namespace'],
178
                            !empty($writerConfig['classPrefix']) ? $writerConfig['classPrefix'] : '',
179
                            !empty($writerConfig['classSuffix']) ? $writerConfig['classSuffix'] : ''
180
                        ),
181
                        $this->writers[BaseEntityClassWriter::class]
182
                    );
183
184
                    break;
185
186
                case RepositoryClassWriter::class:
187
                    $this->writers[RepositoryClassWriter::class] = new RepositoryClassWriter(
188
                        new MySQLWriterConfig(
189
                            $directoryOverride ?: $writerConfig['directory'],
190
                            $writerConfig['namespace'],
191
                            !empty($writerConfig['classPrefix']) ? $writerConfig['classPrefix'] : '',
192
                            !empty($writerConfig['classSuffix']) ? $writerConfig['classSuffix'] : ''
193
                        ),
194
                        $this->writers[EntityClassWriter::class]
195
                    );
196
197
                    break;
198
            }
199
200
            $this->generator->addWriter($this->writers[$writerClass]);
201
        }
202
    }
203
}
204