Completed
Pull Request — 4.2 (#140)
by David
09:39 queued 04:42
created

Configuration::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 16
nc 4
nop 8

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