Completed
Push — master ( 9bb0cb...c02cb1 )
by Gianluca
05:06
created

ConfigurationFactory::createService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 13
Bugs 1 Features 7
Metric Value
c 13
b 1
f 7
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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
        $config->setDefaultQueryHints($options->getDefaultQueryHints());
62
63 72
        foreach ($options->getNamedNativeQueries() as $name => $query) {
64
            $config->addNamedNativeQuery($name, $query['sql'], new $query['rsm']);
65 72
        }
66
67 72
        foreach ($options->getCustomHydrationModes() as $modeName => $hydrator) {
68
            $config->addCustomHydrationMode($modeName, $hydrator);
69 72
        }
70
71 72
        foreach ($options->getFilters() as $name => $class) {
72
            $config->addFilter($name, $class);
73 72
        }
74
        
75 72
        $config->setDefaultQueryHints($options->getDefaultQueryHints());
76
77 72
        $config->setMetadataCacheImpl($container->get($options->getMetadataCache()));
78 72
        $config->setQueryCacheImpl($container->get($options->getQueryCache()));
79 72
        $config->setResultCacheImpl($container->get($options->getResultCache()));
80 72
        $config->setHydrationCacheImpl($container->get($options->getHydrationCache()));
81 72
        $config->setMetadataDriverImpl($container->get($options->getDriver()));
82
83 72
        if ($namingStrategy = $options->getNamingStrategy()) {
84 3
            if (is_string($namingStrategy)) {
85 2
                if (!$container->has($namingStrategy)) {
86 1
                    throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy));
87
                }
88
89 1
                $config->setNamingStrategy($container->get($namingStrategy));
90 1
            } else {
91 1
                $config->setNamingStrategy($namingStrategy);
92
            }
93 2
        }
94
95 71
        if ($quoteStrategy = $options->getQuoteStrategy()) {
96 3
            if (is_string($quoteStrategy)) {
97 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...
98
                    throw new InvalidArgumentException(sprintf('Quote strategy "%s" not found', $quoteStrategy));
99
                }
100
101
                $config->setQuoteStrategy($serviceLocator->get($quoteStrategy));
102
            } else {
103 1
                $config->setQuoteStrategy($quoteStrategy);
104
            }
105 1
        }
106
107 69
        if ($repositoryFactory = $options->getRepositoryFactory()) {
108
            if (is_string($repositoryFactory)) {
109
                if (!$container->has($repositoryFactory)) {
110
                    throw new InvalidArgumentException(
111
                        sprintf('Repository factory "%s" not found', $repositoryFactory)
112
                    );
113
                }
114
115
                $config->setRepositoryFactory($container->get($repositoryFactory));
116
            } else {
117
                $config->setRepositoryFactory($repositoryFactory);
118
            }
119 1
        }
120
121 69
        if ($entityListenerResolver = $options->getEntityListenerResolver()) {
122 2
            if ($entityListenerResolver instanceof EntityListenerResolver) {
123 1
                $config->setEntityListenerResolver($entityListenerResolver);
124 1
            } else {
125 1
                $config->setEntityListenerResolver($container->get($entityListenerResolver));
126
            }
127 2
        }
128
129 69
        $secondLevelCache = $options->getSecondLevelCache();
130
131 69
        if ($secondLevelCache->isEnabled()) {
132 1
            $regionsConfig = new RegionsConfiguration(
133 1
                $secondLevelCache->getDefaultLifetime(),
134 1
                $secondLevelCache->getDefaultLockLifetime()
135 1
            );
136
137 1
            foreach ($secondLevelCache->getRegions() as $regionName => $regionConfig) {
138 1
                if (isset($regionConfig['lifetime'])) {
139 1
                    $regionsConfig->setLifetime($regionName, $regionConfig['lifetime']);
140 1
                }
141
142 1
                if (isset($regionConfig['lock_lifetime'])) {
143 1
                    $regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']);
144 1
                }
145 1
            }
146
147
            // As Second Level Cache caches queries results, we reuse the result cache impl
148 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...
149 1
            $cacheFactory->setFileLockRegionDirectory($secondLevelCache->getFileLockRegionDirectory());
150
151 1
            $cacheConfiguration = new CacheConfiguration();
152 1
            $cacheConfiguration->setCacheFactory($cacheFactory);
153 1
            $cacheConfiguration->setRegionsConfiguration($regionsConfig);
154
155 1
            $config->setSecondLevelCacheEnabled();
156 1
            $config->setSecondLevelCacheConfiguration($cacheConfiguration);
157 1
        }
158
159 69
        if ($className = $options->getDefaultRepositoryClassName()) {
160 57
            $config->setDefaultRepositoryClassName($className);
161 57
        }
162
163 69
        $this->setupDBALConfiguration($container, $config);
164
165 69
        return $config;
166
    }
167
168 72
    public function createService(ServiceLocatorInterface $container)
169
    {
170 72
        return $this($container, Configuration::class);
171
    }
172
173 72
    protected function getOptionsClass()
174
    {
175 72
        return 'DoctrineORMModule\Options\Configuration';
176
    }
177
}
178