Completed
Pull Request — master (#602)
by Tom
07:07
created

Configuration   F

Complexity

Total Complexity 65

Size/Duplication

Total Lines 714
Duplicated Lines 0 %

Coupling/Cohesion

Components 10
Dependencies 3

Test Coverage

Coverage 89.8%

Importance

Changes 0
Metric Value
wmc 65
lcom 10
cbo 3
dl 0
loc 714
ccs 132
cts 147
cp 0.898
rs 3.086
c 0
b 0
f 0

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 DoctrineORMModule\Options;
6
7
use Doctrine\ORM\Mapping\EntityListenerResolver;
8
use Doctrine\ORM\Mapping\NamingStrategy;
9
use Doctrine\ORM\Mapping\QuoteStrategy;
10
use Doctrine\ORM\Repository\RepositoryFactory;
11
use Laminas\Stdlib\Exception\InvalidArgumentException;
12
use function get_class;
13
use function gettype;
14
use function is_object;
15
use function is_string;
16
use function sprintf;
17
18
/**
19
 * Configuration options for an ORM Configuration
20
 *
21
 * @link    http://www.doctrine-project.org/
22
 */
23
class Configuration extends DBALConfiguration
24
{
25
    /**
26
     * Set the cache key for the metadata cache. Cache key
27
     * is assembled as "doctrine.cache.{key}" and pulled from
28
     * service locator.
29
     */
30
    protected string $metadataCache = 'array';
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
31
32
    /**
33
     * Set the cache key for the query cache. Cache key
34
     * is assembled as "doctrine.cache.{key}" and pulled from
35
     * service locator.
36
     */
37
    protected string $queryCache = 'array';
38
39
    /**
40
     * Set the cache key for the result cache. Cache key
41
     * is assembled as "doctrine.cache.{key}" and pulled from
42
     * service locator.
43
     */
44
    protected string $resultCache = 'array';
45
46
    /**
47
     * Set the cache key for the hydration cache. Cache key
48
     * is assembled as "doctrine.cache.{key}" and pulled from
49
     * service locator.
50
     */
51
    protected string $hydrationCache = 'array';
52
53
    /**
54
     * Set the driver key for the metadata driver. Driver key
55
     * is assembled as "doctrine.driver.{key}" and pulled from
56
     * service locator.
57
     */
58
    protected string $driver = 'orm_default';
59
60
    /**
61
     * Automatic generation of proxies (disable for production!)
62
     */
63
    protected bool $generateProxies = true;
64
65
    /**
66
     * Proxy directory.
67
     */
68
    protected string $proxyDir = 'data';
69
70
    /**
71
     * Proxy namespace.
72
     */
73
    protected string $proxyNamespace = 'DoctrineORMModule\Proxy';
74
75
    /**
76
     * Entity alias map.
77
     *
78
     * @var array
79
     */
80
    protected array $entityNamespaces = [];
81
82
    /**
83
     * Keys must be function names and values the FQCN of the implementing class.
84
     * The function names will be case-insensitive in DQL.
85
     *
86
     * @var array
87
     */
88
    protected array $datetimeFunctions = [];
89
90
    /**
91
     * Keys must be function names and values the FQCN of the implementing class.
92
     * The function names will be case-insensitive in DQL.
93
     *
94
     * @var array
95
     */
96
    protected array $stringFunctions = [];
97
98
    /**
99
     * Keys must be function names and values the FQCN of the implementing class.
100
     * The function names will be case-insensitive in DQL.
101
     *
102
     * @var array
103
     */
104
    protected array $numericFunctions = [];
105
106
    /**
107
     * Keys must be the name of the custom filter and the value must be
108
     * the class name for the custom filter.
109
     *
110
     * @var array
111
     */
112
    protected array $filters = [];
113
114
    /**
115
     * Keys must be the name of the query and values the DQL query string.
116
     *
117
     * @var array
118
     */
119
    protected array $namedQueries = [];
120
121
    /**
122
     * Keys must be the name of the query and the value is an array containing
123
     * the keys 'sql' for native SQL query string and 'rsm' for the Query\ResultSetMapping.
124
     *
125
     * @var array
126
     */
127
    protected array $namedNativeQueries = [];
128
129
    /**
130
     * Keys must be the name of the custom hydration method and the value must be
131
     * the class name for the custom hydrator
132
     *
133
     * @var array
134
     */
135
    protected array $customHydrationModes = [];
136
137
    /**
138
     * Naming strategy or name of the naming strategy service to be set in ORM
139
     * configuration (if any)
140
     *
141
     * @var string|NamingStrategy|null
142
     */
143
    protected $namingStrategy;
144
145
    /**
146
     * Quote strategy or name of the quote strategy service to be set in ORM
147
     * configuration (if any)
148
     *
149
     * @var string|QuoteStrategy|null
150
     */
151
    protected $quoteStrategy;
152
153
    /**
154
     * Default repository class
155
     */
156
    protected ?string $defaultRepositoryClassName = null;
157
158
    /**
159
     * Repository factory or name of the repository factory service to be set in ORM
160
     * configuration (if any)
161
     *
162
     * @var string|RepositoryFactory|null
163
     */
164
    protected $repositoryFactory;
165
166
    /**
167
     * Class name of MetaData factory to be set in ORM.
168
     * The entityManager will create a new instance on construction.
169
     */
170
    protected ?string $classMetadataFactoryName = null;
171
172
    /**
173
     * Entity listener resolver or service name of the entity listener resolver
174
     * to be set in ORM configuration (if any)
175
     *
176
     * @link http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html
177
     *
178
     * @var string|EntityListenerResolver|null
179
     */
180
    protected $entityListenerResolver;
181
182
    /**
183
     * Configuration for second level cache
184
     *
185
     * @link http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/second-level-cache.html
186
     */
187
    protected ?SecondLevelCacheConfiguration $secondLevelCache = null;
188
189
    /**
190
     * Configuration option for the filter schema assets expression
191
     */
192
    protected ?string $filterSchemaAssetsExpression = null;
193
194
    /**
195
     * @param  array $datetimeFunctions
196
     */
197
    public function setDatetimeFunctions(array $datetimeFunctions) : self
198
    {
199
        $this->datetimeFunctions = $datetimeFunctions;
200
201
        return $this;
202
    }
203
204
    /**
205
     * @return array
206
     */
207
    public function getDatetimeFunctions() : array
208
    {
209
        return $this->datetimeFunctions;
210
    }
211
212
    public function setDriver(string $driver) : self
213
    {
214
        $this->driver = $driver;
215
216 72
        return $this;
217
    }
218 72
219
    public function getDriver() : string
220 72
    {
221
        return "doctrine.driver.{$this->driver}";
222
    }
223
224
    /**
225
     * @param  array $entityNamespaces
226 88
     */
227
    public function setEntityNamespaces(array $entityNamespaces) : self
228 88
    {
229
        $this->entityNamespaces = $entityNamespaces;
230
231
        return $this;
232
    }
233
234
    /**
235 72
     * @return array
236
     */
237 72
    public function getEntityNamespaces() : array
238
    {
239 72
        return $this->entityNamespaces;
240
    }
241
242
    public function setGenerateProxies(bool $generateProxies) : self
243
    {
244
        $this->generateProxies = $generateProxies;
245 88
246
        return $this;
247 88
    }
248
249
    public function getGenerateProxies() : bool
250
    {
251
        return $this->generateProxies;
252
    }
253
254
    public function setMetadataCache(string $metadataCache) : self
255
    {
256
        $this->metadataCache = $metadataCache;
257
258
        return $this;
259
    }
260
261
    public function getMetadataCache() : string
262
    {
263
        return "doctrine.cache.{$this->metadataCache}";
264 88
    }
265
266 88
    public function setResultCache(string $resultCache) : self
267
    {
268
        $this->resultCache = $resultCache;
269
270
        return $this;
271
    }
272
273 72
    public function getResultCache() : string
274
    {
275 72
        return "doctrine.cache.{$this->resultCache}";
276
    }
277 72
278
    public function setHydrationCache(string $hydrationCache) : self
279
    {
280
        $this->hydrationCache = $hydrationCache;
281
282
        return $this;
283 88
    }
284
285 88
    public function getHydrationCache() : string
286
    {
287
        return "doctrine.cache.{$this->hydrationCache}";
288
    }
289
290
    /**
291
     * @param  array $namedNativeQueries
292 72
     */
293
    public function setNamedNativeQueries(array $namedNativeQueries) : self
294 72
    {
295
        $this->namedNativeQueries = $namedNativeQueries;
296 72
297
        return $this;
298
    }
299
300
    /**
301
     * @return array
302 88
     */
303
    public function getNamedNativeQueries() : array
304 88
    {
305
        return $this->namedNativeQueries;
306
    }
307
308
    /**
309
     * @param  array $namedQueries
310
     */
311 73
    public function setNamedQueries(array $namedQueries) : self
312
    {
313 73
        $this->namedQueries = $namedQueries;
314
315 73
        return $this;
316
    }
317
318
    /**
319
     * @return array
320
     */
321 88
    public function getNamedQueries() : array
322
    {
323 88
        return $this->namedQueries;
324
    }
325
326
    /**
327
     * @param  array $numericFunctions
328
     */
329
    public function setNumericFunctions(array $numericFunctions) : self
330 74
    {
331
        $this->numericFunctions = $numericFunctions;
332 74
333
        return $this;
334 74
    }
335
336
    /**
337
     * @return array
338
     */
339
    public function getNumericFunctions() : array
340 88
    {
341
        return $this->numericFunctions;
342 88
    }
343
344
    /**
345
     * @param  array $filters
346
     */
347
    public function setFilters(array $filters) : self
348
    {
349
        $this->filters = $filters;
350
351
        return $this;
352
    }
353
354
    /**
355
     * @return array
356
     */
357
    public function getFilters() : array
358
    {
359 88
        return $this->filters;
360
    }
361 88
362
    public function setProxyDir(string $proxyDir) : self
363
    {
364
        $this->proxyDir = $proxyDir;
365
366
        return $this;
367
    }
368
369
    public function getProxyDir() : string
370
    {
371
        return $this->proxyDir;
372
    }
373
374
    public function setProxyNamespace(string $proxyNamespace) : self
375
    {
376
        $this->proxyNamespace = $proxyNamespace;
377
378 88
        return $this;
379
    }
380 88
381
    public function getProxyNamespace() : string
382
    {
383
        return $this->proxyNamespace;
384
    }
385
386
    public function setQueryCache(string $queryCache) : self
387 72
    {
388
        $this->queryCache = $queryCache;
389 72
390
        return $this;
391 72
    }
392
393
    public function getQueryCache() : string
394
    {
395
        return "doctrine.cache.{$this->queryCache}";
396
    }
397 88
398
    /**
399 88
     * @param  array $stringFunctions
400
     */
401
    public function setStringFunctions(array $stringFunctions) : self
402
    {
403
        $this->stringFunctions = $stringFunctions;
404
405
        return $this;
406
    }
407 72
408
    /**
409 72
     * @return array
410
     */
411 72
    public function getStringFunctions() : array
412
    {
413
        return $this->stringFunctions;
414
    }
415
416
    /**
417
     * @param  array $modes
418 88
     */
419
    public function setCustomHydrationModes(array $modes) : self
420 88
    {
421
        $this->customHydrationModes = $modes;
422
423
        return $this;
424
    }
425
426
    /**
427 72
     * @return array
428
     */
429 72
    public function getCustomHydrationModes() : array
430
    {
431 72
        return $this->customHydrationModes;
432
    }
433
434
    /**
435
     * @param string|NamingStrategy|null $namingStrategy
436
     *
437 88
     * @throws InvalidArgumentException   when the provided naming strategy does not fit the expected type
438
     */
439 88
    public function setNamingStrategy($namingStrategy) : self
440
    {
441
        if ($namingStrategy === null
442
            || is_string($namingStrategy)
443
            || $namingStrategy instanceof NamingStrategy
444
        ) {
445
            $this->namingStrategy = $namingStrategy;
446 72
447
            return $this;
448 72
        }
449
450 72
        throw new InvalidArgumentException(
451
            sprintf(
452
                'namingStrategy must be either a string, a Doctrine\ORM\Mapping\NamingStrategy '
453
                . 'instance or null, %s given',
454
                is_object($namingStrategy) ? get_class($namingStrategy) : gettype($namingStrategy)
455
            )
456 88
        );
457
    }
458 88
459
    /**
460
     * @return string|NamingStrategy|null
461
     */
462
    public function getNamingStrategy()
463
    {
464
        return $this->namingStrategy;
465 72
    }
466
467 72
    /**
468
     * @param string|QuoteStrategy|null $quoteStrategy
469 72
     *
470
     * @throws InvalidArgumentException   when the provided quote strategy does not fit the expected type
471
     */
472
    public function setQuoteStrategy($quoteStrategy) : self
473
    {
474
        if ($quoteStrategy === null
475 88
            || is_string($quoteStrategy)
476
            || $quoteStrategy instanceof QuoteStrategy
477 88
        ) {
478
            $this->quoteStrategy = $quoteStrategy;
479
480
            return $this;
481
        }
482
483
        throw new InvalidArgumentException(
484 72
            sprintf(
485
                'quoteStrategy must be either a string, a Doctrine\ORM\Mapping\QuoteStrategy '
486 72
                . 'instance or null, %s given',
487
                is_object($quoteStrategy) ? get_class($quoteStrategy) : gettype($quoteStrategy)
488 72
            )
489
        );
490
    }
491
492
    /**
493
     * @return string|QuoteStrategy|null
494 88
     */
495
    public function getQuoteStrategy()
496 88
    {
497
        return $this->quoteStrategy;
498
    }
499
500
    /**
501
     * @param string|RepositoryFactory|null $repositoryFactory
502
     *
503
     * @throws InvalidArgumentException   when the provided repository factory does not fit the expected type
504
     */
505
    public function setRepositoryFactory($repositoryFactory) : self
506
    {
507
        if ($repositoryFactory === null
508
            || is_string($repositoryFactory)
509
            || $repositoryFactory instanceof RepositoryFactory
510
        ) {
511
            $this->repositoryFactory = $repositoryFactory;
512
513 88
            return $this;
514
        }
515 88
516
        throw new InvalidArgumentException(
517
            sprintf(
518
                'repositoryFactory must be either a string, a Doctrine\ORM\Repository\RepositoryFactory '
519
                . 'instance or null, %s given',
520
                is_object($repositoryFactory) ? get_class($repositoryFactory) : gettype($repositoryFactory)
521
            )
522
        );
523 4
    }
524
525 4
    /**
526 4
     * @return string|RepositoryFactory|null
527 4
     */
528
    public function getRepositoryFactory()
529 4
    {
530
        return $this->repositoryFactory;
531 4
    }
532
533
    /**
534 1
     * Set the metadata factory class name to use
535 1
     *
536
     * @see \Doctrine\ORM\Configuration::setClassMetadataFactoryName()
537 1
     */
538 1
    public function setClassMetadataFactoryName(?string $factoryName) : self
539
    {
540
        $this->classMetadataFactoryName = $factoryName;
541
542
        return $this;
543
    }
544
545
    public function getClassMetadataFactoryName() : ?string
546 89
    {
547
        return $this->classMetadataFactoryName;
548 89
    }
549
550
    /**
551
     * @param string|EntityListenerResolver|null $entityListenerResolver
552
     *
553
     * @throws InvalidArgumentException           When the provided entity listener resolver
554
     *                                            does not fit the expected type
555
     */
556 4
    public function setEntityListenerResolver($entityListenerResolver) : self
557
    {
558 4
        if ($entityListenerResolver === null
559 4
            || $entityListenerResolver instanceof EntityListenerResolver
560 4
            || is_string($entityListenerResolver)
561
        ) {
562 4
            $this->entityListenerResolver = $entityListenerResolver;
563
564 4
            return $this;
565
        }
566
567 1
        throw new InvalidArgumentException(sprintf(
568 1
            'entityListenerResolver must be either a string, a Doctrine\ORM\Mapping\EntityListenerResolver '
569
            . 'instance or null, %s given',
570 1
            is_object($entityListenerResolver) ? get_class($entityListenerResolver) : gettype($entityListenerResolver)
571 1
        ));
572
    }
573
574
    /**
575
     * @return string|EntityListenerResolver|null
576
     */
577
    public function getEntityListenerResolver()
578
    {
579 88
        return $this->entityListenerResolver;
580
    }
581 88
582
    /**
583
     * @param  array $secondLevelCache
584
     */
585
    public function setSecondLevelCache(array $secondLevelCache) : self
586
    {
587
        $this->secondLevelCache = new SecondLevelCacheConfiguration($secondLevelCache);
588
589 1
        return $this;
590
    }
591 1
592 1
    public function getSecondLevelCache() : SecondLevelCacheConfiguration
593 1
    {
594
        return $this->secondLevelCache ?: new SecondLevelCacheConfiguration();
595 1
    }
596
597 1
    public function setFilterSchemaAssetsExpression(string $filterSchemaAssetsExpression) : self
598
    {
599
        $this->filterSchemaAssetsExpression = $filterSchemaAssetsExpression;
600 1
601 1
        return $this;
602
    }
603 1
604 1
    public function getFilterSchemaAssetsExpression() : ?string
605
    {
606
        return $this->filterSchemaAssetsExpression;
607
    }
608
609
    /**
610
     * Sets default repository class.
611
     */
612 87
    public function setDefaultRepositoryClassName(string $className) : self
613
    {
614 87
        $this->defaultRepositoryClassName = (string) $className;
615
616
        return $this;
617
    }
618
619
    /**
620
     * Get default repository class name.
621
     */
622
    public function getDefaultRepositoryClassName() : ?string
623
    {
624
        return $this->defaultRepositoryClassName;
625 1
    }
626
}
627