Completed
Pull Request — master (#478)
by Gianluca
10:10 queued 07:41
created

ConfigurationFactory   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 82.61%

Importance

Changes 11
Bugs 1 Features 5
Metric Value
wmc 23
c 11
b 1
f 5
lcom 0
cbo 9
dl 0
loc 142
ccs 76
cts 92
cp 0.8261
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
F __invoke() 0 124 21
A getOptionsClass() 0 4 1
A createService() 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\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 72
    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
40
    {
41
        /** @var $options \DoctrineORMModule\Options\Configuration */
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
            $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 70
            if (is_string($namingStrategy)) {
81 2
                if (!$container->has($namingStrategy)) {
82 70
                    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 69
            }
89 2
        }
90
91 71
        if ($quoteStrategy = $options->getQuoteStrategy()) {
92 3
            if (is_string($quoteStrategy)) {
93 2
                if (!$serviceLocator->has($quoteStrategy)) {
0 ignored issues
show
Bug introduced by
The variable $serviceLocator does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
94
                    throw new InvalidArgumentException(sprintf('Quote strategy "%s" not found', $quoteStrategy));
95
                }
96
97
                $config->setQuoteStrategy($serviceLocator->get($quoteStrategy));
98
            } else {
99 1
                $config->setQuoteStrategy($quoteStrategy);
100
            }
101 1
        }
102
103 69
        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
                }
110
111
                $config->setRepositoryFactory($container->get($repositoryFactory));
112
            } else {
113
                $config->setRepositoryFactory($repositoryFactory);
114
            }
115
        }
116
117 69
        if ($entityListenerResolver = $options->getEntityListenerResolver()) {
118 2
            if ($entityListenerResolver instanceof EntityListenerResolver) {
119 2
                $config->setEntityListenerResolver($entityListenerResolver);
120 1
            } else {
121 1
                $config->setEntityListenerResolver($container->get($entityListenerResolver));
122
            }
123 2
        }
124
125 69
        $secondLevelCache = $options->getSecondLevelCache();
126
127 69
        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 69
        if ($className = $options->getDefaultRepositoryClassName()) {
156 57
            $config->setDefaultRepositoryClassName($className);
157 57
        }
158
159 69
        $this->setupDBALConfiguration($container, $config);
160
161 69
        return $config;
162
    }
163
164 72
    public function createService(ServiceLocatorInterface $container)
165
    {
166 72
        return $this($container, Configuration::class);
167
    }
168
169 72
    protected function getOptionsClass()
170
    {
171 72
        return 'DoctrineORMModule\Options\Configuration';
172
    }
173
}
174