Passed
Pull Request — master (#95)
by David
02:49
created

Configuration::getSchemaAnalyzer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 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\GeneratorEventDispatcher;
15
use TheCodingMachine\TDBM\Utils\GeneratorListenerInterface;
16
use TheCodingMachine\TDBM\Utils\NamingStrategyInterface;
17
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinderInterface;
18
use Psr\Log\LoggerInterface;
19
use TheCodingMachine\TDBM\Utils\PathFinder\PathFinder;
20
21
class Configuration implements ConfigurationInterface
22
{
23
24
    /**
25
     * @var string
26
     */
27
    private $beanNamespace;
28
    /**
29
     * @var string
30
     */
31
    private $daoNamespace;
32
    /**
33
     * @var Connection
34
     */
35
    private $connection;
36
    /**
37
     * @var Cache
38
     */
39
    private $cache;
40
    /**
41
     * @var SchemaAnalyzer
42
     */
43
    private $schemaAnalyzer;
44
    /**
45
     * @var LoggerInterface|null
46
     */
47
    private $logger;
48
    /**
49
     * @var GeneratorListenerInterface
50
     */
51
    private $generatorEventDispatcher;
52
    /**
53
     * @var NamingStrategyInterface
54
     */
55
    private $namingStrategy;
56
    /**
57
     * @var PathFinderInterface
58
     */
59
    private $pathFinder;
60
    /**
61
     * @var array<string, string>
62
     */
63
    private $annotations;
64
    /**
65
     * @var AnnotationParser
66
     */
67
    private $annotationParser;
68
69
    /**
70
     * @param string $beanNamespace The namespace hosting the beans
71
     * @param string $daoNamespace The namespace hosting the DAOs
72
     * @param Connection $connection The connection to the database
73
     * @param NamingStrategyInterface $namingStrategy
74
     * @param Cache|null $cache The Doctrine cache to store database metadata
75
     * @param SchemaAnalyzer|null $schemaAnalyzer The schema analyzer that will be used to find shortest paths... Will be automatically created if not passed
76
     * @param LoggerInterface|null $logger The logger
77
     * @param GeneratorListenerInterface[] $generatorListeners A list of listeners that will be triggered when beans/daos are generated
78
     * @param array<string,string> $annotations An array associating the name of the annotation in DB comments to the name of a fully qualified Doctrine annotation class
79
     * @throws \Mouf\Database\SchemaAnalyzer\SchemaAnalyzerException
80
     */
81
    public function __construct(string $beanNamespace, string $daoNamespace, Connection $connection, NamingStrategyInterface $namingStrategy, Cache $cache = null, SchemaAnalyzer $schemaAnalyzer = null, LoggerInterface $logger = null, array $generatorListeners = [], array $annotations = [])
82
    {
83
        $this->beanNamespace = rtrim($beanNamespace, '\\');
84
        $this->daoNamespace = rtrim($daoNamespace, '\\');
85
        $this->connection = $connection;
86
        $this->namingStrategy = $namingStrategy;
87
        if ($cache !== null) {
88
            $this->cache = $cache;
89
        } else {
90
            $this->cache = new VoidCache();
91
        }
92
        if ($schemaAnalyzer !== null) {
93
            $this->schemaAnalyzer = $schemaAnalyzer;
94
        } else {
95
            $this->schemaAnalyzer = new SchemaAnalyzer($this->connection->getSchemaManager(), $this->cache, $this->getConnectionUniqueId());
96
        }
97
        $this->logger = $logger;
98
        $this->generatorEventDispatcher = new GeneratorEventDispatcher($generatorListeners);
99
        $this->pathFinder = new PathFinder();
100
        $this->annotations = $annotations;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getBeanNamespace(): string
107
    {
108
        return $this->beanNamespace;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getDaoNamespace(): string
115
    {
116
        return $this->daoNamespace;
117
    }
118
119
    /**
120
     * @return Connection
121
     */
122
    public function getConnection(): Connection
123
    {
124
        return $this->connection;
125
    }
126
127
    /**
128
     * @return NamingStrategyInterface
129
     */
130
    public function getNamingStrategy(): NamingStrategyInterface
131
    {
132
        return $this->namingStrategy;
133
    }
134
135
    /**
136
     * @return Cache
137
     */
138
    public function getCache(): Cache
139
    {
140
        return $this->cache;
141
    }
142
143
    /**
144
     * @return SchemaAnalyzer
145
     */
146
    public function getSchemaAnalyzer(): SchemaAnalyzer
147
    {
148
        return $this->schemaAnalyzer;
149
    }
150
151
    /**
152
     * @return LoggerInterface
153
     */
154
    public function getLogger(): ?LoggerInterface
155
    {
156
        return $this->logger;
157
    }
158
159
    /**
160
     * @return GeneratorListenerInterface
161
     */
162
    public function getGeneratorEventDispatcher(): GeneratorListenerInterface
163
    {
164
        return $this->generatorEventDispatcher;
165
    }
166
167
168
169
    /**
170
     * Creates a unique cache key for the current connection.
171
     *
172
     * @return string
173
     */
174
    private function getConnectionUniqueId(): string
175
    {
176
        return hash('md4', $this->connection->getHost().'-'.$this->connection->getPort().'-'.$this->connection->getDatabase().'-'.$this->connection->getDriver()->getName());
177
    }
178
179
    /**
180
     * Returns a class able to find the place of a PHP file based on the class name.
181
     * Useful to find the path where DAOs and beans should be written to.
182
     *
183
     * @return PathFinderInterface
184
     */
185
    public function getPathFinder(): PathFinderInterface
186
    {
187
        return $this->pathFinder;
188
    }
189
190
    /**
191
     * @param PathFinderInterface $pathFinder
192
     */
193
    public function setPathFinder(PathFinderInterface $pathFinder): void
194
    {
195
        $this->pathFinder = $pathFinder;
196
    }
197
198
    /**
199
     * @return AnnotationParser
200
     */
201
    public function getAnnotationParser(): AnnotationParser
202
    {
203
        if ($this->annotationParser === null) {
204
            $this->annotationParser = AnnotationParser::buildWithDefaultAnnotations($this->annotations);
205
        }
206
        return $this->annotationParser;
207
    }
208
}
209