Passed
Push — master ( 8120b7...3b7d13 )
by David
05:52 queued 03:03
created

Configuration::__construct()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 41
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 41
rs 8.9137
cc 6
nc 8
nop 12

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
declare(strict_types=1);
3
4
5
namespace TheCodingMachine\TDBM;
6
7
use Doctrine\Common\Cache\Cache;
8
use Doctrine\Common\Cache\VoidCache;
9
use Doctrine\DBAL\Connection;
10
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
11
use TheCodingMachine\TDBM\Utils\Annotation\AnnotationParser;
12
use TheCodingMachine\TDBM\Utils\Annotation\Autoincrement;
13
use TheCodingMachine\TDBM\Utils\Annotation\UUID;
14
use TheCodingMachine\TDBM\Utils\BaseCodeGeneratorListener;
15
use TheCodingMachine\TDBM\Utils\CodeGeneratorEventDispatcher;
16
use TheCodingMachine\TDBM\Utils\CodeGeneratorListenerInterface;
17
use TheCodingMachine\TDBM\Utils\DefaultNamingStrategy;
18
use TheCodingMachine\TDBM\Utils\GeneratorEventDispatcher;
19
use TheCodingMachine\TDBM\Utils\GeneratorListenerInterface;
20
use TheCodingMachine\TDBM\Utils\NamingStrategyInterface;
21
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinderInterface;
22
use Psr\Log\LoggerInterface;
23
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder;
24
use TheCodingMachine\TDBM\Utils\RootProjectLocator;
25
26
class Configuration implements ConfigurationInterface
27
{
28
29
    /**
30
     * @var string
31
     */
32
    private $beanNamespace;
33
    /**
34
     * @var string
35
     */
36
    private $daoNamespace;
37
    /** @var string */
38
    private $resultIteratorNamespace;
39
    /**
40
     * @var Connection
41
     */
42
    private $connection;
43
    /**
44
     * @var Cache
45
     */
46
    private $cache;
47
    /**
48
     * @var SchemaAnalyzer
49
     */
50
    private $schemaAnalyzer;
51
    /**
52
     * @var LoggerInterface|null
53
     */
54
    private $logger;
55
    /**
56
     * @var GeneratorListenerInterface
57
     */
58
    private $generatorEventDispatcher;
59
    /**
60
     * @var CodeGeneratorListenerInterface
61
     */
62
    private $codeGeneratorListener;
63
    /**
64
     * @var NamingStrategyInterface
65
     */
66
    private $namingStrategy;
67
    /**
68
     * @var PathFinderInterface
69
     */
70
    private $pathFinder;
71
    /**
72
     * @var AnnotationParser
73
     */
74
    private $annotationParser;
75
    private $lockFilePath;
76
77
    /**
78
     * @param string $beanNamespace The namespace hosting the beans
79
     * @param string $daoNamespace The namespace hosting the DAOs
80
     * @param string $resultIteratorNamespace The namespace hosting the ResultIterators
81
     * @param Connection $connection The connection to the database
82
     * @param NamingStrategyInterface|null $namingStrategy
83
     * @param Cache|null $cache The Doctrine cache to store database metadata
84
     * @param SchemaAnalyzer|null $schemaAnalyzer The schema analyzer that will be used to find shortest paths... Will be automatically created if not passed
85
     * @param LoggerInterface|null $logger The logger
86
     * @param GeneratorListenerInterface[] $generatorListeners A list of listeners that will be triggered when beans/daos are generated
87
     * @param AnnotationParser|null $annotationParser
88
     * @param CodeGeneratorListenerInterface[] $codeGeneratorListeners A list of listeners that can alter code generation of each bean/dao
89
     * @param string|null $lockFilePath
90
     * @throws \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerException
91
     */
92
    public function __construct(
93
        string $beanNamespace,
94
        string $daoNamespace,
95
        Connection $connection,
96
        NamingStrategyInterface $namingStrategy = null,
97
        Cache $cache = null,
98
        SchemaAnalyzer $schemaAnalyzer = null,
99
        LoggerInterface $logger = null,
100
        array $generatorListeners = [],
101
        AnnotationParser $annotationParser = null,
102
        array $codeGeneratorListeners = [],
103
        string $resultIteratorNamespace = null,
104
        string $lockFilePath = null
105
    ) {
106
        $this->beanNamespace = rtrim($beanNamespace, '\\');
107
        $this->daoNamespace = rtrim($daoNamespace, '\\');
108
        if ($resultIteratorNamespace === null) {
109
            $baseNamespace = explode('\\', $this->daoNamespace);
110
            array_pop($baseNamespace);
111
            $baseNamespace[] = 'ResultIterator';
112
            $resultIteratorNamespace = implode('\\', $baseNamespace);
113
        }
114
        $this->resultIteratorNamespace = rtrim($resultIteratorNamespace, '\\');
115
        $this->connection = $connection;
116
        if ($cache !== null) {
117
            $this->cache = $cache;
118
        } else {
119
            $this->cache = new VoidCache();
120
        }
121
        if ($schemaAnalyzer !== null) {
122
            $this->schemaAnalyzer = $schemaAnalyzer;
123
        } else {
124
            $this->schemaAnalyzer = new SchemaAnalyzer($this->connection->getSchemaManager(), $this->cache, $this->getConnectionUniqueId());
125
        }
126
        $this->logger = $logger;
127
        $this->generatorEventDispatcher = new GeneratorEventDispatcher($generatorListeners);
128
        $this->pathFinder = new PathFinder();
129
        $this->annotationParser = $annotationParser ?: AnnotationParser::buildWithDefaultAnnotations([]);
130
        $this->codeGeneratorListener = new CodeGeneratorEventDispatcher($codeGeneratorListeners);
131
        $this->namingStrategy = $namingStrategy ?: new DefaultNamingStrategy($this->annotationParser, $this->connection->getSchemaManager());
132
        $this->lockFilePath = $lockFilePath;
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function getBeanNamespace(): string
139
    {
140
        return $this->beanNamespace;
141
    }
142
143
    /**
144
     * @return string
145
     */
146
    public function getDaoNamespace(): string
147
    {
148
        return $this->daoNamespace;
149
    }
150
151
    /**
152
     * @return string
153
     */
154
    public function getResultIteratorNamespace(): string
155
    {
156
        return $this->resultIteratorNamespace;
157
    }
158
159
    /**
160
     * @return Connection
161
     */
162
    public function getConnection(): Connection
163
    {
164
        return $this->connection;
165
    }
166
167
    /**
168
     * @return NamingStrategyInterface
169
     */
170
    public function getNamingStrategy(): NamingStrategyInterface
171
    {
172
        return $this->namingStrategy;
173
    }
174
175
    /**
176
     * @return Cache
177
     */
178
    public function getCache(): Cache
179
    {
180
        return $this->cache;
181
    }
182
183
    /**
184
     * @return SchemaAnalyzer
185
     */
186
    public function getSchemaAnalyzer(): SchemaAnalyzer
187
    {
188
        return $this->schemaAnalyzer;
189
    }
190
191
    /**
192
     * @return LoggerInterface
193
     */
194
    public function getLogger(): ?LoggerInterface
195
    {
196
        return $this->logger;
197
    }
198
199
    /**
200
     * @return GeneratorListenerInterface
201
     */
202
    public function getGeneratorEventDispatcher(): GeneratorListenerInterface
203
    {
204
        return $this->generatorEventDispatcher;
205
    }
206
207
    /**
208
     * @return CodeGeneratorListenerInterface
209
     */
210
    public function getCodeGeneratorListener(): CodeGeneratorListenerInterface
211
    {
212
        return $this->codeGeneratorListener;
213
    }
214
215
    /**
216
     * Creates a unique cache key for the current connection.
217
     *
218
     * @return string
219
     */
220
    private function getConnectionUniqueId(): string
221
    {
222
        return hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName());
223
    }
224
225
    /**
226
     * Returns a class able to find the place of a PHP file based on the class name.
227
     * Useful to find the path where DAOs and beans should be written to.
228
     *
229
     * @return PathFinderInterface
230
     */
231
    public function getPathFinder(): PathFinderInterface
232
    {
233
        return $this->pathFinder;
234
    }
235
236
    /**
237
     * @param PathFinderInterface $pathFinder
238
     */
239
    public function setPathFinder(PathFinderInterface $pathFinder): void
240
    {
241
        $this->pathFinder = $pathFinder;
242
    }
243
244
    /**
245
     * @return AnnotationParser
246
     */
247
    public function getAnnotationParser(): AnnotationParser
248
    {
249
        return $this->annotationParser;
250
    }
251
252
    /**
253
     * @internal
254
     */
255
    public static function getDefaultLockFilePath(): string
256
    {
257
        return RootProjectLocator::getRootLocationPath().'tdbm.lock.yml';
258
    }
259
260
    public function getLockFilePath(): string
261
    {
262
        return $this->lockFilePath ?: self::getDefaultLockFilePath();
263
    }
264
}
265