Completed
Pull Request — master (#459)
by Vytautas
14:01
created

ConfigurationFactory::__invoke()   F

Complexity

Conditions 18
Paths 1792

Size

Total Lines 112
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 66
CRAP Score 19.4448

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 112
ccs 66
cts 79
cp 0.8354
rs 2
cc 18
eloc 65
nc 1792
nop 3
crap 19.4448

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 69
{
34
    /**
35
     * {@inheritDoc}
36 69
     *
37 69
     * @return Configuration
38
     */
39 69
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
40 69
    {
41 69
        /** @var $options \DoctrineORMModule\Options\Configuration */
42
        $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
        $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 69
        foreach ($options->getFilters() as $name => $class) {
70 69
            $config->addFilter($name, $class);
71 69
        }
72
73 69
        $config->setMetadataCacheImpl($container->get($options->getMetadataCache()));
74 3
        $config->setQueryCacheImpl($container->get($options->getQueryCache()));
75 2
        $config->setResultCacheImpl($container->get($options->getResultCache()));
76 1
        $config->setHydrationCacheImpl($container->get($options->getHydrationCache()));
77 68
        $config->setMetadataDriverImpl($container->get($options->getDriver()));
78
79 1
        if ($namingStrategy = $options->getNamingStrategy()) {
80 1
            if (is_string($namingStrategy)) {
81 1
                if (!$container->has($namingStrategy)) {
82
                    throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy));
83 2
                }
84
85 68
                $config->setNamingStrategy($container->get($namingStrategy));
86
            } else {
87
                $config->setNamingStrategy($namingStrategy);
88
            }
89
        }
90
91
        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 68
                $config->setRepositoryFactory($container->get($repositoryFactory));
100 2
            } else {
101 1
                $config->setRepositoryFactory($repositoryFactory);
102 1
            }
103 1
        }
104
105 2
        if ($entityListenerResolver = $options->getEntityListenerResolver()) {
106
            if ($entityListenerResolver instanceof EntityListenerResolver) {
107 68
                $config->setEntityListenerResolver($entityListenerResolver);
108
            } else {
109 68
                $config->setEntityListenerResolver($container->get($entityListenerResolver));
110 1
            }
111 1
        }
112 1
113 1
        $secondLevelCache = $options->getSecondLevelCache();
114
115 1
        if ($secondLevelCache->isEnabled()) {
116 1
            $regionsConfig = new RegionsConfiguration(
117 1
                $secondLevelCache->getDefaultLifetime(),
118 1
                $secondLevelCache->getDefaultLockLifetime()
119
            );
120 1
121 1
            foreach ($secondLevelCache->getRegions() as $regionName => $regionConfig) {
122 1
                if (isset($regionConfig['lifetime'])) {
123 1
                    $regionsConfig->setLifetime($regionName, $regionConfig['lifetime']);
124
                }
125
126 1
                if (isset($regionConfig['lock_lifetime'])) {
127 1
                    $regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']);
128
                }
129 1
            }
130 1
131 1
            // As Second Level Cache caches queries results, we reuse the result cache impl
132
            $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 1
135 1
            $cacheConfiguration = new CacheConfiguration();
136
            $cacheConfiguration->setCacheFactory($cacheFactory);
137 68
            $cacheConfiguration->setRegionsConfiguration($regionsConfig);
138 57
139 57
            $config->setSecondLevelCacheEnabled();
140
            $config->setSecondLevelCacheConfiguration($cacheConfiguration);
141 68
        }
142
143 68
        if ($className = $options->getDefaultRepositoryClassName()) {
144
            $config->setDefaultRepositoryClassName($className);
145
        }
146 69
147
        $this->setupDBALConfiguration($container, $config);
148 69
149
        return $config;
150
    }
151
152
    public function createService(ServiceLocatorInterface $container)
153
    {
154
        return $this($container, Configuration::class);
155
    }
156
157
    protected function getOptionsClass()
158
    {
159
        return 'DoctrineORMModule\Options\Configuration';
160
    }
161
}
162