Completed
Pull Request — master (#459)
by Vytautas
13:28 queued 09:14
created

ConfigurationFactory::__invoke()   F

Complexity

Conditions 18
Paths 1792

Size

Total Lines 112
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 67
CRAP Score 19.3902

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 112
ccs 67
cts 80
cp 0.8375
rs 2
cc 18
eloc 65
nc 1792
nop 3
crap 19.3902

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
 * 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 DoctrineORMModule\Service;
21
22
use Doctrine\ORM\Cache\CacheConfiguration;
23
use Doctrine\ORM\Cache\DefaultCacheFactory;
24
use Doctrine\ORM\Cache\RegionsConfiguration;
25
use Doctrine\ORM\Mapping\EntityListenerResolver;
26
use DoctrineORMModule\Service\DBALConfigurationFactory as DoctrineConfigurationFactory;
27
use Interop\Container\ContainerInterface;
28
use Zend\ServiceManager\ServiceLocatorInterface;
29
use Zend\ServiceManager\Exception\InvalidArgumentException;
30
use Doctrine\ORM\Configuration;
31
32
class ConfigurationFactory extends DoctrineConfigurationFactory
33
{
34
    /**
35
     * {@inheritDoc}
36
     *
37
     * @return Configuration
38
     */
39 69
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
40
    {
41
        /** @var $options \DoctrineORMModule\Options\Configuration */
42 69
        $options = $this->getOptions($container);
43 69
        $config  = new Configuration();
44
45 69
        $config->setAutoGenerateProxyClasses($options->getGenerateProxies());
46 69
        $config->setProxyDir($options->getProxyDir());
47 69
        $config->setProxyNamespace($options->getProxyNamespace());
48
49 69
        $config->setEntityNamespaces($options->getEntityNamespaces());
50
51 69
        $config->setCustomDatetimeFunctions($options->getDatetimeFunctions());
52 69
        $config->setCustomStringFunctions($options->getStringFunctions());
53 69
        $config->setCustomNumericFunctions($options->getNumericFunctions());
54
55 69
        $config->setClassMetadataFactoryName($options->getClassMetadataFactoryName());
56
57 69
        foreach ($options->getNamedQueries() as $name => $query) {
58
            $config->addNamedQuery($name, $query);
59 69
        }
60
61 69
        foreach ($options->getNamedNativeQueries() as $name => $query) {
62
            $config->addNamedNativeQuery($name, $query['sql'], new $query['rsm']);
63 69
        }
64
65 69
        foreach ($options->getCustomHydrationModes() as $modeName => $hydrator) {
66
            $config->addCustomHydrationMode($modeName, $hydrator);
67 69
        }
68
69 69
        foreach ($options->getFilters() as $name => $class) {
70
            $config->addFilter($name, $class);
71 69
        }
72
73 69
        $config->setMetadataCacheImpl($container->get($options->getMetadataCache()));
74 69
        $config->setQueryCacheImpl($container->get($options->getQueryCache()));
75 69
        $config->setResultCacheImpl($container->get($options->getResultCache()));
76 69
        $config->setHydrationCacheImpl($container->get($options->getHydrationCache()));
77 69
        $config->setMetadataDriverImpl($container->get($options->getDriver()));
78
79 69
        if ($namingStrategy = $options->getNamingStrategy()) {
80 69
            if (is_string($namingStrategy)) {
81 2
                if (!$container->has($namingStrategy)) {
82 69
                    throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy));
83
                }
84
85 1
                $config->setNamingStrategy($container->get($namingStrategy));
86 1
            } else {
87 1
                $config->setNamingStrategy($namingStrategy);
88 68
            }
89 2
        }
90
91 68
        if ($repositoryFactory = $options->getRepositoryFactory()) {
92
            if (is_string($repositoryFactory)) {
93
                if (!$container->has($repositoryFactory)) {
94
                    throw new InvalidArgumentException(
95
                        sprintf('Repository factory "%s" not found', $repositoryFactory)
96
                    );
97
                }
98
99
                $config->setRepositoryFactory($container->get($repositoryFactory));
100
            } else {
101
                $config->setRepositoryFactory($repositoryFactory);
102
            }
103
        }
104
105 68
        if ($entityListenerResolver = $options->getEntityListenerResolver()) {
106 2
            if ($entityListenerResolver instanceof EntityListenerResolver) {
107 1
                $config->setEntityListenerResolver($entityListenerResolver);
108 1
            } else {
109 1
                $config->setEntityListenerResolver($container->get($entityListenerResolver));
110
            }
111 2
        }
112
113 68
        $secondLevelCache = $options->getSecondLevelCache();
114
115 68
        if ($secondLevelCache->isEnabled()) {
116 1
            $regionsConfig = new RegionsConfiguration(
117 1
                $secondLevelCache->getDefaultLifetime(),
118 1
                $secondLevelCache->getDefaultLockLifetime()
119 2
            );
120
121 1
            foreach ($secondLevelCache->getRegions() as $regionName => $regionConfig) {
122 1
                if (isset($regionConfig['lifetime'])) {
123 1
                    $regionsConfig->setLifetime($regionName, $regionConfig['lifetime']);
124 1
                }
125
126 1
                if (isset($regionConfig['lock_lifetime'])) {
127 1
                    $regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']);
128 1
                }
129 1
            }
130
131
            // As Second Level Cache caches queries results, we reuse the result cache impl
132 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...
133 1
            $cacheFactory->setFileLockRegionDirectory($secondLevelCache->getFileLockRegionDirectory());
134
135 1
            $cacheConfiguration = new CacheConfiguration();
136 1
            $cacheConfiguration->setCacheFactory($cacheFactory);
137 1
            $cacheConfiguration->setRegionsConfiguration($regionsConfig);
138
139 1
            $config->setSecondLevelCacheEnabled();
140 1
            $config->setSecondLevelCacheConfiguration($cacheConfiguration);
141 1
        }
142
143 68
        if ($className = $options->getDefaultRepositoryClassName()) {
144 57
            $config->setDefaultRepositoryClassName($className);
145 57
        }
146
147 68
        $this->setupDBALConfiguration($container, $config);
148
149 68
        return $config;
150
    }
151
152 69
    public function createService(ServiceLocatorInterface $container)
153
    {
154 69
        return $this($container, Configuration::class);
155
    }
156
157 69
    protected function getOptionsClass()
158
    {
159 69
        return 'DoctrineORMModule\Options\Configuration';
160
    }
161
}
162