Completed
Pull Request — 4.2 (#140)
by David
26:56
created

Configuration::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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