Completed
Pull Request — master (#479)
by Michał
09:53
created

ConfigurationFactory   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 86.67%

Importance

Changes 12
Bugs 1 Features 4
Metric Value
wmc 22
c 12
b 1
f 4
lcom 0
cbo 9
dl 0
loc 137
ccs 78
cts 90
cp 0.8667
rs 10

2 Methods

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