Completed
Push — master ( f0fe3a...13ea99 )
by Andreas
12s
created

Configuration   C

Complexity

Total Complexity 54

Size/Duplication

Total Lines 387
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 64.96%

Importance

Changes 0
Metric Value
wmc 54
lcom 1
cbo 5
dl 0
loc 387
ccs 89
cts 137
cp 0.6496
rs 6.4799
c 0
b 0
f 0

46 Methods

Rating   Name   Duplication   Size   Complexity  
A addDocumentNamespace() 0 4 1
A getDocumentNamespace() 0 8 2
A getDocumentNamespaces() 0 4 1
A setDocumentNamespaces() 0 4 1
A setMetadataDriverImpl() 0 4 1
A newDefaultAnnotationDriver() 0 6 1
A getMetadataDriverImpl() 0 4 1
A getMetadataCacheImpl() 0 4 1
A setMetadataCacheImpl() 0 4 1
A setProxyDir() 0 4 1
A getProxyDir() 0 4 1
A getAutoGenerateProxyClasses() 0 4 1
A setAutoGenerateProxyClasses() 0 4 1
A getProxyNamespace() 0 4 1
A setProxyNamespace() 0 4 1
A setHydratorDir() 0 4 1
A getHydratorDir() 0 4 1
A getAutoGenerateHydratorClasses() 0 4 1
A setAutoGenerateHydratorClasses() 0 4 1
A getHydratorNamespace() 0 4 1
A setHydratorNamespace() 0 4 1
A setPersistentCollectionDir() 0 4 1
A getPersistentCollectionDir() 0 4 1
A getAutoGeneratePersistentCollectionClasses() 0 4 1
A setAutoGeneratePersistentCollectionClasses() 0 4 1
A getPersistentCollectionNamespace() 0 4 1
A setPersistentCollectionNamespace() 0 4 1
A setDefaultDB() 0 4 1
A getDefaultDB() 0 4 1
A setClassMetadataFactoryName() 0 4 1
A getClassMetadataFactoryName() 0 7 2
A getDefaultCommitOptions() 0 4 1
A setDefaultCommitOptions() 0 4 1
A addFilter() 0 7 1
A getFilterClassName() 0 6 2
A getFilterParameters() 0 6 2
A setDefaultDocumentRepositoryClassName() 0 10 2
A getDefaultDocumentRepositoryClassName() 0 4 1
A setDefaultGridFSRepositoryClassName() 0 10 2
A getDefaultGridFSRepositoryClassName() 0 4 1
A setRepositoryFactory() 0 4 1
A getRepositoryFactory() 0 4 1
A setPersistentCollectionFactory() 0 4 1
A getPersistentCollectionFactory() 0 7 2
A setPersistentCollectionGenerator() 0 4 1
A getPersistentCollectionGenerator() 0 10 2

How to fix   Complexity   

Complex Class

Complex classes like Configuration often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Configuration, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\ODM\MongoDB;
6
7
use Doctrine\Common\Annotations\AnnotationReader;
8
use Doctrine\Common\Cache\Cache;
9
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
10
use Doctrine\Common\Persistence\ObjectRepository;
11
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
12
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
13
use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionFactory;
14
use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionGenerator;
15
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionFactory;
16
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionGenerator;
17
use Doctrine\ODM\MongoDB\Repository\DefaultGridFSRepository;
18
use Doctrine\ODM\MongoDB\Repository\DefaultRepositoryFactory;
19
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
20
use Doctrine\ODM\MongoDB\Repository\GridFSRepository;
21
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;
22
use function trim;
23
24
/**
25
 * Configuration class for the DocumentManager. When setting up your DocumentManager
26
 * you can optionally specify an instance of this class as the second argument.
27
 * If you do not pass a configuration object, a blank one will be created for you.
28
 *
29
 *     <?php
30
 *
31
 *     $config = new Configuration();
32
 *     $dm = DocumentManager::create(new Connection(), $config);
33
 *
34
 */
35
class Configuration
36
{
37
    /**
38
     * Array of attributes for this configuration instance.
39
     *
40
     * @var array
41
     */
42
    private $attributes = [];
43
44
    /**
45
     * Never autogenerate a proxy/hydrator/persistent collection and rely that
46
     * it was generated by some process before deployment. Copied from
47
     * \Doctrine\Common\Proxy\AbstractProxyFactory.
48
     *
49
     * @var integer
50
     */
51
    public const AUTOGENERATE_NEVER = 0;
52
53
    /**
54
     * Always generates a new proxy/hydrator/persistent collection in every request.
55
     *
56
     * This is only sane during development.
57
     * Copied from \Doctrine\Common\Proxy\AbstractProxyFactory.
58
     *
59
     * @var integer
60
     */
61
    public const AUTOGENERATE_ALWAYS = 1;
62
63
    /**
64
     * Autogenerate the proxy/hydrator/persistent collection class when the file does not exist.
65
     *
66
     * This strategy causes a file exists call whenever any proxy/hydrator is used the
67
     * first time in a request. Copied from \Doctrine\Common\Proxy\AbstractProxyFactory.
68
     *
69
     * @var integer
70
     */
71
    public const AUTOGENERATE_FILE_NOT_EXISTS = 2;
72
73
    /**
74
     * Generate the proxy/hydrator/persistent collection classes using eval().
75
     *
76
     * This strategy is only sane for development.
77
     * Copied from \Doctrine\Common\Proxy\AbstractProxyFactory.
78
     *
79
     * @var integer
80
     */
81
    public const AUTOGENERATE_EVAL = 3;
82
83
    /**
84
     * Adds a namespace under a certain alias.
85
     */
86
    public function addDocumentNamespace(string $alias, string $namespace): void
87
    {
88
        $this->attributes['documentNamespaces'][$alias] = $namespace;
89
    }
90
91
    /**
92
     * Resolves a registered namespace alias to the full namespace.
93
     *
94
     * @throws MongoDBException
95
     */
96
    public function getDocumentNamespace(string $documentNamespaceAlias): string
97
    {
98
        if (! isset($this->attributes['documentNamespaces'][$documentNamespaceAlias])) {
99
            throw MongoDBException::unknownDocumentNamespace($documentNamespaceAlias);
100
        }
101
102
        return trim($this->attributes['documentNamespaces'][$documentNamespaceAlias], '\\');
103
    }
104
105
    /**
106
     * Retrieves the list of registered document namespace aliases.
107
     */
108
    public function getDocumentNamespaces(): array
109
    {
110
        return $this->attributes['documentNamespaces'];
111
    }
112
113
    /**
114
     * Set the document alias map
115
     */
116
    public function setDocumentNamespaces(array $documentNamespaces): void
117
    {
118
        $this->attributes['documentNamespaces'] = $documentNamespaces;
119
    }
120
121
    /**
122
     * Sets the cache driver implementation that is used for metadata caching.
123
     *
124
     * @todo Force parameter to be a Closure to ensure lazy evaluation
125
     *       (as soon as a metadata cache is in effect, the driver never needs to initialize).
126
     */
127 1665
    public function setMetadataDriverImpl(MappingDriver $driverImpl): void
128
    {
129 1665
        $this->attributes['metadataDriverImpl'] = $driverImpl;
130 1665
    }
131
132
    /**
133
     * Add a new default annotation driver with a correctly configured annotation reader.
134
     */
135
    public function newDefaultAnnotationDriver(array $paths = []): AnnotationDriver
136
    {
137
        $reader = new AnnotationReader();
138
139
        return new AnnotationDriver($reader, $paths);
140
    }
141
142
    /**
143
     * Gets the cache driver implementation that is used for the mapping metadata.
144
     */
145 1403
    public function getMetadataDriverImpl(): ?MappingDriver
146
    {
147 1403
        return $this->attributes['metadataDriverImpl'] ?? null;
148
    }
149
150 1602
    public function getMetadataCacheImpl(): ?Cache
151
    {
152 1602
        return $this->attributes['metadataCacheImpl'] ?? null;
153
    }
154
155
    public function setMetadataCacheImpl(Cache $cacheImpl): void
156
    {
157
        $this->attributes['metadataCacheImpl'] = $cacheImpl;
158
    }
159
160
    /**
161
     * Sets the directory where Doctrine generates any necessary proxy class files.
162
     */
163 1602
    public function setProxyDir(string $dir): void
164
    {
165 1602
        $this->attributes['proxyDir'] = $dir;
166 1602
    }
167
168
    /**
169
     * Gets the directory where Doctrine generates any necessary proxy class files.
170
     */
171 1602
    public function getProxyDir(): ?string
172
    {
173 1602
        return $this->attributes['proxyDir'] ?? null;
174
    }
175
176
    /**
177
     * Gets an int flag that indicates whether proxy classes should always be regenerated
178
     * during each script execution.
179
     */
180 1602
    public function getAutoGenerateProxyClasses(): int
181
    {
182 1602
        return $this->attributes['autoGenerateProxyClasses'] ?? self::AUTOGENERATE_ALWAYS;
183
    }
184
185
    /**
186
     * Sets an int flag that indicates whether proxy classes should always be regenerated
187
     * during each script execution.
188
     */
189
    public function setAutoGenerateProxyClasses(int $mode): void
190
    {
191
        $this->attributes['autoGenerateProxyClasses'] = $mode;
192
    }
193
194 1602
    public function getProxyNamespace(): ?string
195
    {
196 1602
        return $this->attributes['proxyNamespace'] ?? null;
197
    }
198
199 1602
    public function setProxyNamespace(string $ns): void
200
    {
201 1602
        $this->attributes['proxyNamespace'] = $ns;
202 1602
    }
203
204 1602
    public function setHydratorDir(string $dir): void
205
    {
206 1602
        $this->attributes['hydratorDir'] = $dir;
207 1602
    }
208
209 1602
    public function getHydratorDir(): ?string
210
    {
211 1602
        return $this->attributes['hydratorDir'] ?? null;
212
    }
213
214
    /**
215
     * Gets an int flag that indicates whether hydrator classes should always be regenerated
216
     * during each script execution.
217
     */
218 1602
    public function getAutoGenerateHydratorClasses(): int
219
    {
220 1602
        return $this->attributes['autoGenerateHydratorClasses'] ?? self::AUTOGENERATE_ALWAYS;
221
    }
222
223
    /**
224
     * Sets an int flag that indicates whether hydrator classes should always be regenerated
225
     * during each script execution.
226
     */
227
    public function setAutoGenerateHydratorClasses(int $mode): void
228
    {
229
        $this->attributes['autoGenerateHydratorClasses'] = $mode;
230
    }
231
232 1602
    public function getHydratorNamespace(): ?string
233
    {
234 1602
        return $this->attributes['hydratorNamespace'] ?? null;
235
    }
236
237 1602
    public function setHydratorNamespace(string $ns): void
238
    {
239 1602
        $this->attributes['hydratorNamespace'] = $ns;
240 1602
    }
241
242 1602
    public function setPersistentCollectionDir(string $dir): void
243
    {
244 1602
        $this->attributes['persistentCollectionDir'] = $dir;
245 1602
    }
246
247 11
    public function getPersistentCollectionDir(): ?string
248
    {
249 11
        return $this->attributes['persistentCollectionDir'] ?? null;
250
    }
251
252
    /**
253
     * Gets a integer flag that indicates how and when persistent collection
254
     * classes should be generated.
255
     */
256 7
    public function getAutoGeneratePersistentCollectionClasses(): int
257
    {
258 7
        return $this->attributes['autoGeneratePersistentCollectionClasses'] ?? self::AUTOGENERATE_ALWAYS;
259
    }
260
261
    /**
262
     * Sets a integer flag that indicates how and when persistent collection
263
     * classes should be generated.
264
     */
265
    public function setAutoGeneratePersistentCollectionClasses(int $mode): void
266
    {
267
        $this->attributes['autoGeneratePersistentCollectionClasses'] = $mode;
268
    }
269
270 11
    public function getPersistentCollectionNamespace(): ?string
271
    {
272 11
        return $this->attributes['persistentCollectionNamespace'] ?? null;
273
    }
274
275 1602
    public function setPersistentCollectionNamespace(string $ns): void
276
    {
277 1602
        $this->attributes['persistentCollectionNamespace'] = $ns;
278 1602
    }
279
280
    /**
281
     * Sets the default DB to use for all Documents that do not specify
282
     * a database.
283
     */
284 1602
    public function setDefaultDB(string $defaultDB): void
285
    {
286 1602
        $this->attributes['defaultDB'] = $defaultDB;
287 1602
    }
288
289
    /**
290
     * Gets the default DB to use for all Documents that do not specify a database.
291
     */
292 1263
    public function getDefaultDB(): ?string
293
    {
294 1263
        return $this->attributes['defaultDB'] ?? null;
295
    }
296
297
    public function setClassMetadataFactoryName(string $cmfName): void
298
    {
299
        $this->attributes['classMetadataFactoryName'] = $cmfName;
300
    }
301
302 1602
    public function getClassMetadataFactoryName(): string
303
    {
304 1602
        if (! isset($this->attributes['classMetadataFactoryName'])) {
305 1602
            $this->attributes['classMetadataFactoryName'] = ClassMetadataFactory::class;
306
        }
307 1602
        return $this->attributes['classMetadataFactoryName'];
308
    }
309
310 554
    public function getDefaultCommitOptions(): array
311
    {
312 554
        return $this->attributes['defaultCommitOptions'] ?? ['w' => 1];
313
    }
314
315 1
    public function setDefaultCommitOptions(array $defaultCommitOptions): void
316
    {
317 1
        $this->attributes['defaultCommitOptions'] = $defaultCommitOptions;
318 1
    }
319
320
    /**
321
     * Add a filter to the list of possible filters.
322
     */
323 1602
    public function addFilter(string $name, string $className, array $parameters = []): void
324
    {
325 1602
        $this->attributes['filters'][$name] = [
326 1602
            'class' => $className,
327 1602
            'parameters' => $parameters,
328
        ];
329 1602
    }
330
331 24
    public function getFilterClassName(string $name): ?string
332
    {
333 24
        return isset($this->attributes['filters'][$name])
334 24
            ? $this->attributes['filters'][$name]['class']
335 24
            : null;
336
    }
337
338 23
    public function getFilterParameters(string $name): ?array
339
    {
340 23
        return isset($this->attributes['filters'][$name])
341 23
            ? $this->attributes['filters'][$name]['parameters']
342 23
            : null;
343
    }
344
345
    /**
346
     * @throws MongoDBException If not is a ObjectRepository.
347
     */
348
    public function setDefaultDocumentRepositoryClassName(string $className): void
349
    {
350
        $reflectionClass = new \ReflectionClass($className);
351
352
        if (! $reflectionClass->implementsInterface(ObjectRepository::class)) {
353
            throw MongoDBException::invalidDocumentRepository($className);
354
        }
355
356
        $this->attributes['defaultDocumentRepositoryClassName'] = $className;
357
    }
358
359 319
    public function getDefaultDocumentRepositoryClassName(): string
360
    {
361 319
        return $this->attributes['defaultDocumentRepositoryClassName'] ?? DocumentRepository::class;
362
    }
363
364
    /**
365
     * @throws MongoDBException If the class does not implement the GridFSRepository interface.
366
     */
367
    public function setDefaultGridFSRepositoryClassName(string $className): void
368
    {
369
        $reflectionClass = new \ReflectionClass($className);
370
371
        if (! $reflectionClass->implementsInterface(GridFSRepository::class)) {
372
            throw MongoDBException::invalidGridFSRepository($className);
373
        }
374
375
        $this->attributes['defaultGridFSRepositoryClassName'] = $className;
376
    }
377
378 10
    public function getDefaultGridFSRepositoryClassName(): string
379
    {
380 10
        return $this->attributes['defaultGridFSRepositoryClassName'] ?? DefaultGridFSRepository::class;
381
    }
382
383 2
    public function setRepositoryFactory(RepositoryFactory $repositoryFactory): void
384
    {
385 2
        $this->attributes['repositoryFactory'] = $repositoryFactory;
386 2
    }
387
388 1602
    public function getRepositoryFactory(): RepositoryFactory
389
    {
390 1602
        return $this->attributes['repositoryFactory'] ?? new DefaultRepositoryFactory();
391
    }
392
393
    public function setPersistentCollectionFactory(PersistentCollectionFactory $persistentCollectionFactory): void
394
    {
395
        $this->attributes['persistentCollectionFactory'] = $persistentCollectionFactory;
396
    }
397
398 394
    public function getPersistentCollectionFactory(): PersistentCollectionFactory
399
    {
400 394
        if (! isset($this->attributes['persistentCollectionFactory'])) {
401 394
            $this->attributes['persistentCollectionFactory'] = new DefaultPersistentCollectionFactory();
402
        }
403 394
        return $this->attributes['persistentCollectionFactory'];
404
    }
405
406
    public function setPersistentCollectionGenerator(PersistentCollectionGenerator $persistentCollectionGenerator): void
407
    {
408
        $this->attributes['persistentCollectionGenerator'] = $persistentCollectionGenerator;
409
    }
410
411 8
    public function getPersistentCollectionGenerator(): PersistentCollectionGenerator
412
    {
413 8
        if (! isset($this->attributes['persistentCollectionGenerator'])) {
414 8
            $this->attributes['persistentCollectionGenerator'] = new DefaultPersistentCollectionGenerator(
415 8
                $this->getPersistentCollectionDir(),
416 8
                $this->getPersistentCollectionNamespace()
417
            );
418
        }
419 8
        return $this->attributes['persistentCollectionGenerator'];
420
    }
421
}
422