Configuration   F
last analyzed

Complexity

Total Complexity 62

Size/Duplication

Total Lines 437
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 63.98%

Importance

Changes 0
Metric Value
wmc 62
lcom 2
cbo 5
dl 0
loc 437
ccs 103
cts 161
cp 0.6398
rs 3.44
c 0
b 0
f 0

49 Methods

Rating   Name   Duplication   Size   Complexity  
A setMetadataDriverImpl() 0 4 1
A __construct() 0 5 1
A addDocumentNamespace() 0 4 1
A getDocumentNamespace() 0 8 2
A getDocumentNamespaces() 0 4 1
A setDocumentNamespaces() 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 11 2
A getProxyDir() 0 4 1
A getAutoGenerateProxyClasses() 0 4 1
A setAutoGenerateProxyClasses() 0 20 3
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 8 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 8 2
A setPersistentCollectionGenerator() 0 4 1
A getPersistentCollectionGenerator() 0 19 4
A buildGhostObjectFactory() 0 4 1
A getProxyManagerConfiguration() 0 4 1

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