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