Passed
Pull Request — master (#176)
by
unknown
02:38
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
        string $resultIteratorNamespace,
93
        Connection $connection,
94
        NamingStrategyInterface $namingStrategy = null,
95
        Cache $cache = null,
96
        SchemaAnalyzer $schemaAnalyzer = null,
97
        LoggerInterface $logger = null,
98
        array $generatorListeners = [],
99
        AnnotationParser $annotationParser = null,
100
        array $codeGeneratorListeners = []
101
    ) {
102
        $this->beanNamespace = rtrim($beanNamespace, '\\');
103
        $this->daoNamespace = rtrim($daoNamespace, '\\');
104
        $this->resultIteratorNamespace = rtrim($resultIteratorNamespace, '\\');
105
        $this->connection = $connection;
106
        if ($cache !== null) {
107
            $this->cache = $cache;
108
        } else {
109
            $this->cache = new VoidCache();
110
        }
111
        if ($schemaAnalyzer !== null) {
112
            $this->schemaAnalyzer = $schemaAnalyzer;
113
        } else {
114
            $this->schemaAnalyzer = new SchemaAnalyzer($this->connection->getSchemaManager(), $this->cache, $this->getConnectionUniqueId());
115
        }
116
        $this->logger = $logger;
117
        $this->generatorEventDispatcher = new GeneratorEventDispatcher($generatorListeners);
118
        $this->pathFinder = new PathFinder();
119
        $this->annotationParser = $annotationParser ?: AnnotationParser::buildWithDefaultAnnotations([]);
120
        $this->codeGeneratorListener = new CodeGeneratorEventDispatcher($codeGeneratorListeners);
121
        $this->namingStrategy = $namingStrategy ?: new DefaultNamingStrategy($this->annotationParser, $this->connection->getSchemaManager());
122
    }
123
124
    /**
125
     * @return string
126
     */
127
    public function getBeanNamespace(): string
128
    {
129
        return $this->beanNamespace;
130
    }
131
132
    /**
133
     * @return string
134
     */
135
    public function getDaoNamespace(): string
136
    {
137
        return $this->daoNamespace;
138
    }
139
140
    /**
141
     * @return string
142
     */
143
    public function getResultIteratorNamespace(): string
144
    {
145
        return $this->resultIteratorNamespace;
146
    }
147
148
    /**
149
     * @return Connection
150
     */
151
    public function getConnection(): Connection
152
    {
153
        return $this->connection;
154
    }
155
156
    /**
157
     * @return NamingStrategyInterface
158
     */
159
    public function getNamingStrategy(): NamingStrategyInterface
160
    {
161
        return $this->namingStrategy;
162
    }
163
164
    /**
165
     * @return Cache
166
     */
167
    public function getCache(): Cache
168
    {
169
        return $this->cache;
170
    }
171
172
    /**
173
     * @return SchemaAnalyzer
174
     */
175
    public function getSchemaAnalyzer(): SchemaAnalyzer
176
    {
177
        return $this->schemaAnalyzer;
178
    }
179
180
    /**
181
     * @return LoggerInterface
182
     */
183
    public function getLogger(): ?LoggerInterface
184
    {
185
        return $this->logger;
186
    }
187
188
    /**
189
     * @return GeneratorListenerInterface
190
     */
191
    public function getGeneratorEventDispatcher(): GeneratorListenerInterface
192
    {
193
        return $this->generatorEventDispatcher;
194
    }
195
196
    /**
197
     * @return CodeGeneratorListenerInterface
198
     */
199
    public function getCodeGeneratorListener(): CodeGeneratorListenerInterface
200
    {
201
        return $this->codeGeneratorListener;
202
    }
203
204
    /**
205
     * Creates a unique cache key for the current connection.
206
     *
207
     * @return string
208
     */
209
    private function getConnectionUniqueId(): string
210
    {
211
        return hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName());
212
    }
213
214
    /**
215
     * Returns a class able to find the place of a PHP file based on the class name.
216
     * Useful to find the path where DAOs and beans should be written to.
217
     *
218
     * @return PathFinderInterface
219
     */
220
    public function getPathFinder(): PathFinderInterface
221
    {
222
        return $this->pathFinder;
223
    }
224
225
    /**
226
     * @param PathFinderInterface $pathFinder
227
     */
228
    public function setPathFinder(PathFinderInterface $pathFinder): void
229
    {
230
        $this->pathFinder = $pathFinder;
231
    }
232
233
    /**
234
     * @return AnnotationParser
235
     */
236
    public function getAnnotationParser(): AnnotationParser
237
    {
238
        return $this->annotationParser;
239
    }
240
}
241