Completed
Pull Request — master (#1875)
by Andreas
16:37
created

Configuration   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 414
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 10

Test Coverage

Coverage 64.05%

Importance

Changes 0
Metric Value
wmc 60
lcom 2
cbo 10
dl 0
loc 414
ccs 98
cts 153
cp 0.6405
rs 3.6
c 0
b 0
f 0

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