Completed
Pull Request — master (#1419)
by Robert
39:37 queued 36:54
created

Configuration   C

Complexity

Total Complexity 66

Size/Duplication

Total Lines 556
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 70.2%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 66
c 5
b 1
f 1
lcom 1
cbo 7
dl 0
loc 556
ccs 106
cts 151
cp 0.702
rs 5.7474

44 Methods

Rating   Name   Duplication   Size   Complexity  
A setPersistentCollectionDir() 0 4 1
A getPersistentCollectionDir() 0 5 2
A getAutoGeneratePersistentCollectionClasses() 0 5 2
A setAutoGeneratePersistentCollectionClasses() 0 4 1
A getPersistentCollectionNamespace() 0 5 2
A setPersistentCollectionNamespace() 0 4 1
A setPersistentCollectionFactory() 0 4 1
A getPersistentCollectionFactory() 0 7 2
A setPersistentCollectionGenerator() 0 4 1
A getPersistentCollectionGenerator() 0 10 2
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 5 2
A getMetadataCacheImpl() 0 5 2
A setMetadataCacheImpl() 0 4 1
A setProxyDir() 0 4 1
A getProxyDir() 0 5 2
A getAutoGenerateProxyClasses() 0 5 2
A setAutoGenerateProxyClasses() 0 4 1
A getProxyNamespace() 0 5 2
A setProxyNamespace() 0 4 1
A setHydratorDir() 0 4 1
A getHydratorDir() 0 5 2
A getAutoGenerateHydratorClasses() 0 5 2
A setAutoGenerateHydratorClasses() 0 4 1
A getHydratorNamespace() 0 5 2
A setHydratorNamespace() 0 4 1
A setDefaultDB() 0 4 1
A getDefaultDB() 0 5 2
A setClassMetadataFactoryName() 0 4 1
A addFilter() 0 7 1
A getFilterClassName() 0 6 2
A getFilterParameters() 0 6 2
A setDefaultRepositoryClassName() 0 10 2
A setRepositoryFactory() 0 4 1
A getClassMetadataFactoryName() 0 7 2
A getDefaultRepositoryClassName() 0 6 2
A getRepositoryFactory() 0 6 2
A getDefaultCommitOptions() 0 8 2
A setDefaultCommitOptions() 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
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB;
21
22
use Doctrine\Common\Annotations\AnnotationReader;
23
use Doctrine\Common\Cache\Cache;
24
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
25
use Doctrine\Common\Persistence\ObjectRepository;
26
use Doctrine\ODM\MongoDB\Mapping\ClassMetadataFactory;
27
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
28
use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionFactory;
29
use Doctrine\ODM\MongoDB\PersistentCollection\DefaultPersistentCollectionGenerator;
30
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionFactory;
31
use Doctrine\ODM\MongoDB\PersistentCollection\PersistentCollectionGenerator;
32
use Doctrine\ODM\MongoDB\Repository\DefaultRepositoryFactory;
33
use Doctrine\ODM\MongoDB\Repository\RepositoryFactory;
34
35
/**
36
 * Configuration class for the DocumentManager. When setting up your DocumentManager
37
 * you can optionally specify an instance of this class as the second argument.
38
 * If you do not pass a configuration object, a blank one will be created for you.
39
 *
40
 *     <?php
41
 *
42
 *     $config = new Configuration();
43
 *     $dm = DocumentManager::create(new Connection(), $config);
44
 *
45
 * @since       1.0
46
 */
47
class Configuration extends \Doctrine\MongoDB\Configuration
48
{
49
    /**
50
     * Never autogenerate a proxy/hydrator/persistent collection and rely that
51
     * it was generated by some process before deployment. Copied from
52
     * \Doctrine\Common\Proxy\AbstractProxyFactory.
53
     *
54
     * @var integer
55
     */
56
    const AUTOGENERATE_NEVER = 0;
57
58
    /**
59
     * Always generates a new proxy/hydrator/persistent collection in every request.
60
     *
61
     * This is only sane during development.
62
     * Copied from \Doctrine\Common\Proxy\AbstractProxyFactory.
63
     *
64
     * @var integer
65
     */
66
    const AUTOGENERATE_ALWAYS = 1;
67
68
    /**
69
     * Autogenerate the proxy/hydrator/persistent collection class when the file does not exist.
70
     *
71
     * This strategy causes a file exists call whenever any proxy/hydrator is used the
72
     * first time in a request. Copied from \Doctrine\Common\Proxy\AbstractProxyFactory.
73
     *
74
     * @var integer
75
     */
76
    const AUTOGENERATE_FILE_NOT_EXISTS = 2;
77
78
    /**
79
     * Generate the proxy/hydrator/persistent collection classes using eval().
80
     *
81
     * This strategy is only sane for development.
82
     * Copied from \Doctrine\Common\Proxy\AbstractProxyFactory.
83
     *
84
     * @var integer
85
     */
86
    const AUTOGENERATE_EVAL = 3;
87
88
    /**
89
     * Adds a namespace under a certain alias.
90
     *
91
     * @param string $alias
92
     * @param string $namespace
93
     */
94
    public function addDocumentNamespace($alias, $namespace)
95
    {
96
        $this->attributes['documentNamespaces'][$alias] = $namespace;
97
    }
98
99
    /**
100
     * Resolves a registered namespace alias to the full namespace.
101
     *
102
     * @param string $documentNamespaceAlias
103
     * @return string
104
     * @throws MongoDBException
105
     */
106
    public function getDocumentNamespace($documentNamespaceAlias)
107
    {
108
        if ( ! isset($this->attributes['documentNamespaces'][$documentNamespaceAlias])) {
109
            throw MongoDBException::unknownDocumentNamespace($documentNamespaceAlias);
110
        }
111
112
        return trim($this->attributes['documentNamespaces'][$documentNamespaceAlias], '\\');
113
    }
114
115
    /**
116
     * Retrieves the list of registered document namespace aliases.
117
     *
118
     * @return array
119
     */
120
    public function getDocumentNamespaces()
121
    {
122
        return $this->attributes['documentNamespaces'];
123
    }
124
125
    /**
126
     * Set the document alias map
127
     *
128
     * @param array $documentNamespaces
129
     * @return void
130
     */
131
    public function setDocumentNamespaces(array $documentNamespaces)
132
    {
133
        $this->attributes['documentNamespaces'] = $documentNamespaces;
134
    }
135
136
    /**
137
     * Sets the cache driver implementation that is used for metadata caching.
138
     *
139
     * @param MappingDriver $driverImpl
140
     * @todo Force parameter to be a Closure to ensure lazy evaluation
141
     *       (as soon as a metadata cache is in effect, the driver never needs to initialize).
142
     */
143 1089
    public function setMetadataDriverImpl(MappingDriver $driverImpl)
144
    {
145 1089
        $this->attributes['metadataDriverImpl'] = $driverImpl;
146 1089
    }
147
148
    /**
149
     * Add a new default annotation driver with a correctly configured annotation reader.
150
     *
151
     * @param array $paths
152
     * @return Mapping\Driver\AnnotationDriver
153
     */
154
    public function newDefaultAnnotationDriver($paths = array())
155
    {
156
        $reader = new AnnotationReader();
157
158
        return new AnnotationDriver($reader, (array)$paths);
159
    }
160
161
    /**
162
     * Gets the cache driver implementation that is used for the mapping metadata.
163
     *
164
     * @return MappingDriver
165
     */
166 864
    public function getMetadataDriverImpl()
167
    {
168 864
        return isset($this->attributes['metadataDriverImpl']) ?
169 864
            $this->attributes['metadataDriverImpl'] : null;
170
    }
171
172
    /**
173
     * Gets the cache driver implementation that is used for metadata caching.
174
     *
175
     * @return \Doctrine\Common\Cache\Cache
176
     */
177 1038
    public function getMetadataCacheImpl()
178
    {
179 1038
        return isset($this->attributes['metadataCacheImpl']) ?
180 1038
            $this->attributes['metadataCacheImpl'] : null;
181
    }
182
183
    /**
184
     * Sets the cache driver implementation that is used for metadata caching.
185
     *
186
     * @param \Doctrine\Common\Cache\Cache $cacheImpl
187
     */
188
    public function setMetadataCacheImpl(Cache $cacheImpl)
189
    {
190
        $this->attributes['metadataCacheImpl'] = $cacheImpl;
191
    }
192
193
    /**
194
     * Sets the directory where Doctrine generates any necessary proxy class files.
195
     *
196
     * @param string $dir
197
     */
198 1038
    public function setProxyDir($dir)
199
    {
200 1038
        $this->attributes['proxyDir'] = $dir;
201 1038
    }
202
203
    /**
204
     * Gets the directory where Doctrine generates any necessary proxy class files.
205
     *
206
     * @return string
207
     */
208 1038
    public function getProxyDir()
209
    {
210 1038
        return isset($this->attributes['proxyDir']) ?
211 1038
            $this->attributes['proxyDir'] : null;
212
    }
213
214
    /**
215
     * Gets a boolean flag that indicates whether proxy classes should always be regenerated
216
     * during each script execution.
217
     *
218
     * @return boolean|integer
219
     */
220 1038
    public function getAutoGenerateProxyClasses()
221
    {
222 1038
        return isset($this->attributes['autoGenerateProxyClasses']) ?
223 1038
            $this->attributes['autoGenerateProxyClasses'] : true;
224
    }
225
226
    /**
227
     * Sets a boolean flag that indicates whether proxy classes should always be regenerated
228
     * during each script execution.
229
     *
230
     * @param boolean|int $bool Possible values are constants of Doctrine\Common\Proxy\AbstractProxyFactory
231
     */
232
    public function setAutoGenerateProxyClasses($bool)
233
    {
234
        $this->attributes['autoGenerateProxyClasses'] = $bool;
235
    }
236
237
    /**
238
     * Gets the namespace where proxy classes reside.
239
     *
240
     * @return string
241
     */
242 1038
    public function getProxyNamespace()
243
    {
244 1038
        return isset($this->attributes['proxyNamespace']) ?
245 1038
            $this->attributes['proxyNamespace'] : null;
246
    }
247
248
    /**
249
     * Sets the namespace where proxy classes reside.
250
     *
251
     * @param string $ns
252
     */
253 1038
    public function setProxyNamespace($ns)
254
    {
255 1038
        $this->attributes['proxyNamespace'] = $ns;
256 1038
    }
257
258
    /**
259
     * Sets the directory where Doctrine generates hydrator class files.
260
     *
261
     * @param string $dir
262
     */
263 1038
    public function setHydratorDir($dir)
264
    {
265 1038
        $this->attributes['hydratorDir'] = $dir;
266 1038
    }
267
268
    /**
269
     * Gets the directory where Doctrine generates hydrator class files.
270
     *
271
     * @return string
272
     */
273 1038
    public function getHydratorDir()
274
    {
275 1038
        return isset($this->attributes['hydratorDir']) ?
276 1038
            $this->attributes['hydratorDir'] : null;
277
    }
278
279
    /**
280
     * Gets a boolean flag that indicates whether hydrator classes should always be regenerated
281
     * during each script execution.
282
     *
283
     * @return boolean|integer Possible values are defined constants
284
     */
285 1038
    public function getAutoGenerateHydratorClasses()
286
    {
287 1038
        return isset($this->attributes['autoGenerateHydratorClasses']) ?
288 1038
            $this->attributes['autoGenerateHydratorClasses'] : true;
289
    }
290
291
    /**
292
     * Sets a boolean flag that indicates whether hydrator classes should always be regenerated
293
     * during each script execution.
294
     *
295
     * @param boolean|integer $bool
296
     */
297
    public function setAutoGenerateHydratorClasses($bool)
298
    {
299
        $this->attributes['autoGenerateHydratorClasses'] = $bool;
300
    }
301
302
    /**
303
     * Gets the namespace where hydrator classes reside.
304
     *
305
     * @return string
306
     */
307 1038
    public function getHydratorNamespace()
308
    {
309 1038
        return isset($this->attributes['hydratorNamespace']) ?
310 1038
            $this->attributes['hydratorNamespace'] : null;
311
    }
312
313
    /**
314
     * Sets the namespace where hydrator classes reside.
315
     *
316
     * @param string $ns
317
     */
318 1038
    public function setHydratorNamespace($ns)
319
    {
320 1038
        $this->attributes['hydratorNamespace'] = $ns;
321 1038
    }
322
323
    /**
324
     * Sets the directory where Doctrine generates persistent collection class files.
325
     *
326
     * @param string $dir
327
     */
328 1038
    public function setPersistentCollectionDir($dir)
329
    {
330 1038
        $this->attributes['persistentCollectionDir'] = $dir;
331 1038
    }
332
333
    /**
334
     * Gets the directory where Doctrine generates persistent collection class files.
335
     *
336
     * @return string
337
     */
338 8
    public function getPersistentCollectionDir()
339
    {
340 8
        return isset($this->attributes['persistentCollectionDir']) ?
341 8
            $this->attributes['persistentCollectionDir'] : null;
342
    }
343
344
    /**
345
     * Gets a integer flag that indicates how and when persistent collection
346
     * classes should be generated.
347
     *
348
     * @return integer Possible values are defined constants
349
     */
350 7
    public function getAutoGeneratePersistentCollectionClasses()
351
    {
352 7
        return isset($this->attributes['autoGeneratePersistentCollectionClasses']) ?
353 7
            $this->attributes['autoGeneratePersistentCollectionClasses'] : self::AUTOGENERATE_ALWAYS;
354
    }
355
356
    /**
357
     * Sets a integer flag that indicates how and when persistent collection
358
     * classes should be generated.
359
     *
360
     * @param integer $mode Possible values are defined constants
361
     */
362
    public function setAutoGeneratePersistentCollectionClasses($mode)
363
    {
364
        $this->attributes['autoGeneratePersistentCollectionClasses'] = $mode;
365
    }
366
367
    /**
368
     * Gets the namespace where persistent collection classes reside.
369
     *
370
     * @return string
371
     */
372 8
    public function getPersistentCollectionNamespace()
373
    {
374 8
        return isset($this->attributes['persistentCollectionNamespace']) ?
375 8
            $this->attributes['persistentCollectionNamespace'] : null;
376
    }
377
378
    /**
379
     * Sets the namespace where persistent collection classes reside.
380
     *
381
     * @param string $ns
382
     */
383 1038
    public function setPersistentCollectionNamespace($ns)
384
    {
385 1038
        $this->attributes['persistentCollectionNamespace'] = $ns;
386 1038
    }
387
388
    /**
389
     * Sets the default DB to use for all Documents that do not specify
390
     * a database.
391
     *
392
     * @param string $defaultDB
393
     */
394 1038
    public function setDefaultDB($defaultDB)
395
    {
396 1038
        $this->attributes['defaultDB'] = $defaultDB;
397 1038
    }
398
399
    /**
400
     * Gets the default DB to use for all Documents that do not specify a database.
401
     *
402
     * @return string $defaultDB
403
     */
404 741
    public function getDefaultDB()
405
    {
406 741
        return isset($this->attributes['defaultDB']) ?
407 741
            $this->attributes['defaultDB'] : null;
408
    }
409
410
    /**
411
     * Set the class metadata factory class name.
412
     *
413
     * @param string $cmfName
414
     */
415
    public function setClassMetadataFactoryName($cmfName)
416
    {
417
        $this->attributes['classMetadataFactoryName'] = $cmfName;
418
    }
419
420
    /**
421
     * Gets the class metadata factory class name.
422
     *
423
     * @return string
424
     */
425 1038
    public function getClassMetadataFactoryName()
426
    {
427 1038
        if ( ! isset($this->attributes['classMetadataFactoryName'])) {
428 1038
            $this->attributes['classMetadataFactoryName'] = ClassMetadataFactory::class;
429 1038
        }
430 1038
        return $this->attributes['classMetadataFactoryName'];
431
    }
432
433
    /**
434
     * Gets array of default commit options.
435
     *
436
     * @return array
437
     */
438 583
    public function getDefaultCommitOptions()
439
    {
440 583
        if (isset($this->attributes['defaultCommitOptions'])) {
441 1
            return $this->attributes['defaultCommitOptions'];
442
        }
443
444 582
        return array('w' => 1);
445
    }
446
447
    /**
448
     * Sets array of default commit options.
449
     *
450
     * @param array $defaultCommitOptions
451
     */
452 1
    public function setDefaultCommitOptions($defaultCommitOptions)
453
    {
454 1
        $this->attributes['defaultCommitOptions'] = $defaultCommitOptions;
455 1
    }
456
457
    /**
458
     * Add a filter to the list of possible filters.
459
     *
460
     * @param string $name       The name of the filter.
461
     * @param string $className  The class name of the filter.
462
     * @param array  $parameters The parameters for the filter.
463
     */
464 1038
    public function addFilter($name, $className, array $parameters = array())
465
    {
466 1038
        $this->attributes['filters'][$name] = array(
467 1038
            'class' => $className,
468
            'parameters' => $parameters
469 1038
        );
470 1038
    }
471
472
    /**
473
     * Gets the class name for a given filter name.
474
     *
475
     * @param string $name The name of the filter.
476
     *
477
     * @return string|null The filter class name, or null if it is undefined
478
     */
479 22
    public function getFilterClassName($name)
480
    {
481 22
        return isset($this->attributes['filters'][$name])
482 22
            ? $this->attributes['filters'][$name]['class']
483 22
            : null;
484
    }
485
486
    /**
487
     * Gets the parameters for a given filter name.
488
     *
489
     * @param string $name The name of the filter.
490
     *
491
     * @return array|null The filter parameters, or null if it is undefined
492
     */
493 21
    public function getFilterParameters($name)
494
    {
495 21
        return isset($this->attributes['filters'][$name])
496 21
            ? $this->attributes['filters'][$name]['parameters']
497 21
            : null;
498
    }
499
500
    /**
501
     * Sets default repository class.
502
     *
503
     * @param string $className
504
     *
505
     * @return void
506
     *
507
     * @throws MongoDBException If not is a ObjectRepository
508
     */
509
    public function setDefaultRepositoryClassName($className)
510
    {
511
        $reflectionClass = new \ReflectionClass($className);
512
513
        if ( ! $reflectionClass->implementsInterface(ObjectRepository::class)) {
514
            throw MongoDBException::invalidDocumentRepository($className);
515
        }
516
517
        $this->attributes['defaultRepositoryClassName'] = $className;
518
    }
519
520
    /**
521
     * Get default repository class.
522
     *
523
     * @return string
524
     */
525 340
    public function getDefaultRepositoryClassName()
526
    {
527 340
        return isset($this->attributes['defaultRepositoryClassName'])
528 340
            ? $this->attributes['defaultRepositoryClassName']
529 340
            : DocumentRepository::class;
530
    }
531
532
    /**
533
     * Set the document repository factory.
534
     *
535
     * @param RepositoryFactory $repositoryFactory
536
     */
537
    public function setRepositoryFactory(RepositoryFactory $repositoryFactory)
538
    {
539
        $this->attributes['repositoryFactory'] = $repositoryFactory;
540
    }
541
542
    /**
543
     * Get the document repository factory.
544
     *
545
     * @return RepositoryFactory
546
     */
547 1038
    public function getRepositoryFactory()
548
    {
549 1038
        return isset($this->attributes['repositoryFactory'])
550 1038
            ? $this->attributes['repositoryFactory']
551 1038
            : new DefaultRepositoryFactory();
552
    }
553
554
    /**
555
     * Set the persistent collection factory.
556
     *
557
     * @param PersistentCollectionFactory $persistentCollectionFactory
558
     */
559
    public function setPersistentCollectionFactory(PersistentCollectionFactory $persistentCollectionFactory)
560
    {
561
        $this->attributes['persistentCollectionFactory'] = $persistentCollectionFactory;
562
    }
563
564
    /**
565
     * Get the persistent collection factory.
566
     *
567
     * @return DefaultPersistentCollectionFactory
568
     */
569 407
    public function getPersistentCollectionFactory()
570
    {
571 407
        if ( ! isset($this->attributes['persistentCollectionFactory'])) {
572 407
            $this->attributes['persistentCollectionFactory'] = new DefaultPersistentCollectionFactory();
573 407
        }
574 407
        return $this->attributes['persistentCollectionFactory'];
575
    }
576
577
    /**
578
     * Set the persistent collection generator.
579
     *
580
     * @param PersistentCollectionGenerator $persistentCollectionGenerator
581
     */
582
    public function setPersistentCollectionGenerator(PersistentCollectionGenerator $persistentCollectionGenerator)
583
    {
584
        $this->attributes['persistentCollectionGenerator'] = $persistentCollectionGenerator;
585
    }
586
587
    /**
588
     * Get the persistent collection generator.
589
     *
590
     * @return DefaultPersistentCollectionGenerator
591
     */
592 8
    public function getPersistentCollectionGenerator()
593
    {
594 8
        if ( ! isset($this->attributes['persistentCollectionGenerator'])) {
595 8
            $this->attributes['persistentCollectionGenerator'] = new DefaultPersistentCollectionGenerator(
596 8
                $this->getPersistentCollectionDir(),
597 8
                $this->getPersistentCollectionNamespace()
598 8
            );
599 8
        }
600 8
        return $this->attributes['persistentCollectionGenerator'];
601
    }
602
}
603