Passed
Pull Request — master (#176)
by
unknown
04:32
created

Configuration::getResultIteratorNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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