ConfigurationFactory::__invoke()   F
last analyzed

Complexity

Conditions 22
Paths 10576

Size

Total Lines 135

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 65
CRAP Score 23.8304

Importance

Changes 0
Metric Value
dl 0
loc 135
ccs 65
cts 77
cp 0.8442
rs 0
c 0
b 0
f 0
cc 22
nc 10576
nop 3
crap 23.8304

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModule\Service;
6
7
use Doctrine\ORM\Cache\CacheConfiguration;
8
use Doctrine\ORM\Cache\DefaultCacheFactory;
9
use Doctrine\ORM\Cache\RegionsConfiguration;
10
use Doctrine\ORM\Configuration;
11
use Doctrine\ORM\Mapping\EntityListenerResolver;
12
use DoctrineORMModule\Options\Configuration as DoctrineORMModuleConfiguration;
13
use DoctrineORMModule\Service\DBALConfigurationFactory as DoctrineConfigurationFactory;
14
use Interop\Container\ContainerInterface;
15
use Laminas\ServiceManager\Exception\InvalidArgumentException;
16
use Laminas\ServiceManager\ServiceLocatorInterface;
17
use function is_string;
18
use function sprintf;
19
20
class ConfigurationFactory extends DoctrineConfigurationFactory
21
{
22
    /**
23
     * {@inheritDoc}
24
     *
25
     * @return Configuration
26
     */
27 88
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
28
    {
29 88
        $options = $this->getOptions($container);
30 88
        $config  = new Configuration();
31
32 88
        $config->setAutoGenerateProxyClasses($options->getGenerateProxies());
33 88
        $config->setProxyDir($options->getProxyDir());
34 88
        $config->setProxyNamespace($options->getProxyNamespace());
35
36 88
        $config->setEntityNamespaces($options->getEntityNamespaces());
37
38 88
        $config->setCustomDatetimeFunctions($options->getDatetimeFunctions());
39 88
        $config->setCustomStringFunctions($options->getStringFunctions());
40 88
        $config->setCustomNumericFunctions($options->getNumericFunctions());
41
42 88
        $config->setClassMetadataFactoryName($options->getClassMetadataFactoryName());
43
44 88
        foreach ($options->getNamedQueries() as $name => $query) {
45
            $config->addNamedQuery($name, $query);
46
        }
47
48 88
        foreach ($options->getNamedNativeQueries() as $name => $query) {
49
            $config->addNamedNativeQuery($name, $query['sql'], new $query['rsm']());
50
        }
51
52 88
        foreach ($options->getCustomHydrationModes() as $modeName => $hydrator) {
53
            $config->addCustomHydrationMode($modeName, $hydrator);
54
        }
55
56 88
        foreach ($options->getFilters() as $name => $class) {
57
            $config->addFilter($name, $class);
58
        }
59
60 88
        $config->setMetadataCacheImpl($container->get($options->getMetadataCache()));
61 88
        $config->setQueryCacheImpl($container->get($options->getQueryCache()));
62 88
        $config->setResultCacheImpl($container->get($options->getResultCache()));
63 88
        $config->setHydrationCacheImpl($container->get($options->getHydrationCache()));
64 88
        $config->setMetadataDriverImpl($container->get($options->getDriver()));
65
66 88
        $namingStrategy = $options->getNamingStrategy();
67 88
        if ($namingStrategy) {
68 3
            if (is_string($namingStrategy)) {
69 2
                if (! $container->has($namingStrategy)) {
70 1
                    throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy));
71
                }
72
73 1
                $config->setNamingStrategy($container->get($namingStrategy));
74
            } else {
75 1
                $config->setNamingStrategy($namingStrategy);
76
            }
77
        }
78
79 87
        $quoteStrategy = $options->getQuoteStrategy();
80 87
        if ($quoteStrategy) {
81 3
            if (is_string($quoteStrategy)) {
82 2
                if (! $container->has($quoteStrategy)) {
83 1
                    throw new InvalidArgumentException(sprintf('Quote strategy "%s" not found', $quoteStrategy));
84
                }
85
86 1
                $config->setQuoteStrategy($container->get($quoteStrategy));
87
            } else {
88 1
                $config->setQuoteStrategy($quoteStrategy);
89
            }
90
        }
91
92 86
        $repositoryFactory = $options->getRepositoryFactory();
93 86
        if ($repositoryFactory) {
94
            if (is_string($repositoryFactory)) {
95
                if (! $container->has($repositoryFactory)) {
96
                    throw new InvalidArgumentException(
97
                        sprintf('Repository factory "%s" not found', $repositoryFactory)
98
                    );
99
                }
100
101
                $config->setRepositoryFactory($container->get($repositoryFactory));
102
            } else {
103
                $config->setRepositoryFactory($repositoryFactory);
104
            }
105
        }
106
107 86
        $entityListenerResolver = $options->getEntityListenerResolver();
108 86
        if ($entityListenerResolver) {
109 2
            if ($entityListenerResolver instanceof EntityListenerResolver) {
110 1
                $config->setEntityListenerResolver($entityListenerResolver);
111
            } else {
112 1
                $config->setEntityListenerResolver($container->get($entityListenerResolver));
113
            }
114
        }
115
116 86
        $secondLevelCache = $options->getSecondLevelCache();
117
118 86
        if ($secondLevelCache->isEnabled()) {
119 1
            $regionsConfig = new RegionsConfiguration(
120 1
                $secondLevelCache->getDefaultLifetime(),
121 1
                $secondLevelCache->getDefaultLockLifetime()
122
            );
123
124 1
            foreach ($secondLevelCache->getRegions() as $regionName => $regionConfig) {
125 1
                if (isset($regionConfig['lifetime'])) {
126 1
                    $regionsConfig->setLifetime($regionName, $regionConfig['lifetime']);
127
                }
128
129 1
                if (! isset($regionConfig['lock_lifetime'])) {
130
                    continue;
131
                }
132
133 1
                $regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']);
134
            }
135
136
            // As Second Level Cache caches queries results, we reuse the result cache impl
137 1
            $cacheFactory = new DefaultCacheFactory($regionsConfig, $config->getResultCacheImpl());
0 ignored issues
show
Bug introduced by
It seems like $config->getResultCacheImpl() can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
138 1
            $cacheFactory->setFileLockRegionDirectory($secondLevelCache->getFileLockRegionDirectory());
139
140 1
            $cacheConfiguration = new CacheConfiguration();
141 1
            $cacheConfiguration->setCacheFactory($cacheFactory);
142 1
            $cacheConfiguration->setRegionsConfiguration($regionsConfig);
143
144 1
            $config->setSecondLevelCacheEnabled();
145 1
            $config->setSecondLevelCacheConfiguration($cacheConfiguration);
146
        }
147
148 86
        $filterSchemaAssetsExpression = $options->getFilterSchemaAssetsExpression();
149 86
        if ($filterSchemaAssetsExpression) {
150
            $config->setFilterSchemaAssetsExpression($filterSchemaAssetsExpression);
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated with message: Use Configuration::setSchemaAssetsFilter() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
151
        }
152
153 86
        $className = $options->getDefaultRepositoryClassName();
154 86
        if ($className) {
155 73
            $config->setDefaultRepositoryClassName($className);
156
        }
157
158 86
        $this->setupDBALConfiguration($container, $config);
159
160 86
        return $config;
161
    }
162
163
    /**
164
     * @return mixed
165
     */
166 88
    public function createService(ServiceLocatorInterface $container)
167
    {
168 88
        return $this($container, Configuration::class);
169
    }
170
171 88
    protected function getOptionsClass() : string
172
    {
173 88
        return DoctrineORMModuleConfiguration::class;
174
    }
175
}
176