Completed
Pull Request — master (#410)
by
unknown
05:35
created

ConfigurationFactory   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 9

Test Coverage

Coverage 84.52%

Importance

Changes 11
Bugs 1 Features 6
Metric Value
wmc 19
c 11
b 1
f 6
lcom 0
cbo 9
dl 0
loc 124
ccs 71
cts 84
cp 0.8452
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
F createService() 0 116 18
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\Mapping\EntityListenerResolver;
26
use DoctrineORMModule\Service\DBALConfigurationFactory as DoctrineConfigurationFactory;
27
use Zend\ServiceManager\ServiceLocatorInterface;
28
use Zend\ServiceManager\Exception\InvalidArgumentException;
29
use Doctrine\ORM\Configuration;
30
31
class ConfigurationFactory extends DoctrineConfigurationFactory
32
{
33 69
    public function createService(ServiceLocatorInterface $serviceLocator)
34
    {
35
        /** @var $options \DoctrineORMModule\Options\Configuration */
36 69
        $options = $this->getOptions($serviceLocator);
37 69
        $config  = new Configuration();
38
39 69
        $config->setAutoGenerateProxyClasses($options->getGenerateProxies());
40 69
        $config->setProxyDir($options->getProxyDir());
41 69
        $config->setProxyNamespace($options->getProxyNamespace());
42
43 69
        $config->setEntityNamespaces($options->getEntityNamespaces());
44
45 69
        $config->setCustomDatetimeFunctions($options->getDatetimeFunctions());
46 69
        $config->setCustomStringFunctions($options->getStringFunctions());
47 69
        $config->setCustomNumericFunctions($options->getNumericFunctions());
48
49 69
        $config->setClassMetadataFactoryName($options->getClassMetadataFactoryName());
50
51 69
        foreach ($options->getNamedQueries() as $name => $query) {
52
            $config->addNamedQuery($name, $query);
53 69
        }
54
55 69
        $config->setDefaultQueryHints($options->getDefaultQueryHints());
56
57 69
        foreach ($options->getNamedNativeQueries() as $name => $query) {
58
            $config->addNamedNativeQuery($name, $query['sql'], new $query['rsm']);
59 69
        }
60
61 69
        foreach ($options->getCustomHydrationModes() as $modeName => $hydrator) {
62
            $config->addCustomHydrationMode($modeName, $hydrator);
63 69
        }
64
65 69
        foreach ($options->getFilters() as $name => $class) {
66
            $config->addFilter($name, $class);
67 69
        }
68
        
69 69
        $config->setDefaultQueryHints($options->getDefaultQueryHints());
70
71 69
        $config->setMetadataCacheImpl($serviceLocator->get($options->getMetadataCache()));
0 ignored issues
show
Documentation introduced by
$serviceLocator->get($op...ns->getMetadataCache()) is of type object|array, but the function expects a object<Doctrine\Common\Cache\Cache>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
72 69
        $config->setQueryCacheImpl($serviceLocator->get($options->getQueryCache()));
0 ignored issues
show
Documentation introduced by
$serviceLocator->get($options->getQueryCache()) is of type object|array, but the function expects a object<Doctrine\Common\Cache\Cache>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73 69
        $config->setResultCacheImpl($serviceLocator->get($options->getResultCache()));
0 ignored issues
show
Documentation introduced by
$serviceLocator->get($options->getResultCache()) is of type object|array, but the function expects a object<Doctrine\Common\Cache\Cache>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
74 69
        $config->setHydrationCacheImpl($serviceLocator->get($options->getHydrationCache()));
0 ignored issues
show
Documentation introduced by
$serviceLocator->get($op...s->getHydrationCache()) is of type object|array, but the function expects a object<Doctrine\Common\Cache\Cache>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
75 69
        $config->setMetadataDriverImpl($serviceLocator->get($options->getDriver()));
0 ignored issues
show
Documentation introduced by
$serviceLocator->get($options->getDriver()) is of type object|array, but the function expects a object<Doctrine\Common\P...g\Driver\MappingDriver>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
76
77 69
        if ($namingStrategy = $options->getNamingStrategy()) {
78 3
            if (is_string($namingStrategy)) {
79 2
                if (!$serviceLocator->has($namingStrategy)) {
80 1
                    throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy));
81
                }
82
83 1
                $config->setNamingStrategy($serviceLocator->get($namingStrategy));
0 ignored issues
show
Documentation introduced by
$serviceLocator->get($namingStrategy) is of type object|array, but the function expects a object<Doctrine\ORM\Mapping\NamingStrategy>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84 1
            } else {
85 1
                $config->setNamingStrategy($namingStrategy);
86
            }
87 2
        }
88
89 68
        if ($repositoryFactory = $options->getRepositoryFactory()) {
90
            if (is_string($repositoryFactory)) {
91
                if (!$serviceLocator->has($repositoryFactory)) {
92
                    throw new InvalidArgumentException(
93
                        sprintf('Repository factory "%s" not found', $repositoryFactory)
94
                    );
95
                }
96
97
                $config->setRepositoryFactory($serviceLocator->get($repositoryFactory));
0 ignored issues
show
Documentation introduced by
$serviceLocator->get($repositoryFactory) is of type object|array, but the function expects a object<Doctrine\ORM\Repository\RepositoryFactory>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
98
            } else {
99
                $config->setRepositoryFactory($repositoryFactory);
100
            }
101
        }
102
103 68
        if ($entityListenerResolver = $options->getEntityListenerResolver()) {
104 2
            if ($entityListenerResolver instanceof EntityListenerResolver) {
105 1
                $config->setEntityListenerResolver($entityListenerResolver);
106 1
            } else {
107 1
                $config->setEntityListenerResolver($serviceLocator->get($entityListenerResolver));
0 ignored issues
show
Documentation introduced by
$serviceLocator->get($entityListenerResolver) is of type object|array, but the function expects a object<Doctrine\ORM\Mapp...EntityListenerResolver>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108 1
            }
109 2
        }
110
111 68
        $secondLevelCache = $options->getSecondLevelCache();
112
113 68
        if ($secondLevelCache->isEnabled()) {
114 1
            $regionsConfig = new RegionsConfiguration(
115 1
                $secondLevelCache->getDefaultLifetime(),
116 1
                $secondLevelCache->getDefaultLockLifetime()
117 1
            );
118
119 1
            foreach ($secondLevelCache->getRegions() as $regionName => $regionConfig) {
120 1
                if (isset($regionConfig['lifetime'])) {
121 1
                    $regionsConfig->setLifetime($regionName, $regionConfig['lifetime']);
122 1
                }
123
124 1
                if (isset($regionConfig['lock_lifetime'])) {
125 1
                    $regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']);
126 1
                }
127 1
            }
128
129
            // As Second Level Cache caches queries results, we reuse the result cache impl
130 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...
131 1
            $cacheFactory->setFileLockRegionDirectory($secondLevelCache->getFileLockRegionDirectory());
132
133 1
            $cacheConfiguration = new CacheConfiguration();
134 1
            $cacheConfiguration->setCacheFactory($cacheFactory);
135 1
            $cacheConfiguration->setRegionsConfiguration($regionsConfig);
136
137 1
            $config->setSecondLevelCacheEnabled();
138 1
            $config->setSecondLevelCacheConfiguration($cacheConfiguration);
139 1
        }
140
141 68
        if ($className = $options->getDefaultRepositoryClassName()) {
142 57
            $config->setDefaultRepositoryClassName($className);
143 57
        }
144
145 68
        $this->setupDBALConfiguration($serviceLocator, $config);
146
147 68
        return $config;
148
    }
149
150 69
    protected function getOptionsClass()
151
    {
152 69
        return 'DoctrineORMModule\Options\Configuration';
153
    }
154
}
155