Passed
Pull Request — master (#171)
by ARP
06:42 queued 03:08
created

Configuration::__construct()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 22
rs 9.3554
cc 5
nc 4
nop 11

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
    /**
38
     * @var Connection
39
     */
40
    private $connection;
41
    /**
42
     * @var Cache
43
     */
44
    private $cache;
45
    /**
46
     * @var SchemaAnalyzer
47
     */
48
    private $schemaAnalyzer;
49
    /**
50
     * @var LoggerInterface|null
51
     */
52
    private $logger;
53
    /**
54
     * @var GeneratorListenerInterface
55
     */
56
    private $generatorEventDispatcher;
57
    /**
58
     * @var CodeGeneratorListenerInterface
59
     */
60
    private $codeGeneratorListener;
61
    /**
62
     * @var NamingStrategyInterface
63
     */
64
    private $namingStrategy;
65
    /**
66
     * @var PathFinderInterface
67
     */
68
    private $pathFinder;
69
    /**
70
     * @var AnnotationParser
71
     */
72
    private $annotationParser;
73
    private $lockFilePath;
74
75
    /**
76
     * @param string $beanNamespace The namespace hosting the beans
77
     * @param string $daoNamespace The namespace hosting the DAOs
78
     * @param Connection $connection The connection to the database
79
     * @param NamingStrategyInterface|null $namingStrategy
80
     * @param Cache|null $cache The Doctrine cache to store database metadata
81
     * @param SchemaAnalyzer|null $schemaAnalyzer The schema analyzer that will be used to find shortest paths... Will be automatically created if not passed
82
     * @param LoggerInterface|null $logger The logger
83
     * @param GeneratorListenerInterface[] $generatorListeners A list of listeners that will be triggered when beans/daos are generated
84
     * @param AnnotationParser|null $annotationParser
85
     * @param CodeGeneratorListenerInterface[] $codeGeneratorListeners A list of listeners that can alter code generation of each bean/dao
86
     * @param string|null $lockFilePath
87
     * @throws \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerException
88
     */
89
    public function __construct(string $beanNamespace, string $daoNamespace, Connection $connection, NamingStrategyInterface $namingStrategy = null, Cache $cache = null, SchemaAnalyzer $schemaAnalyzer = null, LoggerInterface $logger = null, array $generatorListeners = [], AnnotationParser $annotationParser = null, array $codeGeneratorListeners = [], string $lockFilePath = null)
90
    {
91
        $this->beanNamespace = rtrim($beanNamespace, '\\');
92
        $this->daoNamespace = rtrim($daoNamespace, '\\');
93
        $this->connection = $connection;
94
        if ($cache !== null) {
95
            $this->cache = $cache;
96
        } else {
97
            $this->cache = new VoidCache();
98
        }
99
        if ($schemaAnalyzer !== null) {
100
            $this->schemaAnalyzer = $schemaAnalyzer;
101
        } else {
102
            $this->schemaAnalyzer = new SchemaAnalyzer($this->connection->getSchemaManager(), $this->cache, $this->getConnectionUniqueId());
103
        }
104
        $this->logger = $logger;
105
        $this->generatorEventDispatcher = new GeneratorEventDispatcher($generatorListeners);
106
        $this->pathFinder = new PathFinder();
107
        $this->annotationParser = $annotationParser ?: AnnotationParser::buildWithDefaultAnnotations([]);
108
        $this->codeGeneratorListener = new CodeGeneratorEventDispatcher($codeGeneratorListeners);
109
        $this->namingStrategy = $namingStrategy ?: new DefaultNamingStrategy($this->annotationParser, $this->connection->getSchemaManager());
110
        $this->lockFilePath = $lockFilePath;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getBeanNamespace(): string
117
    {
118
        return $this->beanNamespace;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getDaoNamespace(): string
125
    {
126
        return $this->daoNamespace;
127
    }
128
129
    /**
130
     * @return Connection
131
     */
132
    public function getConnection(): Connection
133
    {
134
        return $this->connection;
135
    }
136
137
    /**
138
     * @return NamingStrategyInterface
139
     */
140
    public function getNamingStrategy(): NamingStrategyInterface
141
    {
142
        return $this->namingStrategy;
143
    }
144
145
    /**
146
     * @return Cache
147
     */
148
    public function getCache(): Cache
149
    {
150
        return $this->cache;
151
    }
152
153
    /**
154
     * @return SchemaAnalyzer
155
     */
156
    public function getSchemaAnalyzer(): SchemaAnalyzer
157
    {
158
        return $this->schemaAnalyzer;
159
    }
160
161
    /**
162
     * @return LoggerInterface
163
     */
164
    public function getLogger(): ?LoggerInterface
165
    {
166
        return $this->logger;
167
    }
168
169
    /**
170
     * @return GeneratorListenerInterface
171
     */
172
    public function getGeneratorEventDispatcher(): GeneratorListenerInterface
173
    {
174
        return $this->generatorEventDispatcher;
175
    }
176
177
    /**
178
     * @return CodeGeneratorListenerInterface
179
     */
180
    public function getCodeGeneratorListener(): CodeGeneratorListenerInterface
181
    {
182
        return $this->codeGeneratorListener;
183
    }
184
185
    /**
186
     * Creates a unique cache key for the current connection.
187
     *
188
     * @return string
189
     */
190
    private function getConnectionUniqueId(): string
191
    {
192
        return hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName());
193
    }
194
195
    /**
196
     * Returns a class able to find the place of a PHP file based on the class name.
197
     * Useful to find the path where DAOs and beans should be written to.
198
     *
199
     * @return PathFinderInterface
200
     */
201
    public function getPathFinder(): PathFinderInterface
202
    {
203
        return $this->pathFinder;
204
    }
205
206
    /**
207
     * @param PathFinderInterface $pathFinder
208
     */
209
    public function setPathFinder(PathFinderInterface $pathFinder): void
210
    {
211
        $this->pathFinder = $pathFinder;
212
    }
213
214
    /**
215
     * @return AnnotationParser
216
     */
217
    public function getAnnotationParser(): AnnotationParser
218
    {
219
        return $this->annotationParser;
220
    }
221
222
    public static function getDefaultLockFilePath(): string
223
    {
224
        return RootProjectLocator::getRootLocationPath().'tdbm.lock.yml';
225
    }
226
227
    public function getLockFilePath(): ?string
228
    {
229
        return $this->lockFilePath ?: self::getDefaultLockFilePath();
230
    }
231
}
232