Completed
Pull Request — master (#453)
by Bas
05:22
created

ConfigurationFactory::getOptionsClass()   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
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 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
        foreach ($options->getNamedNativeQueries() as $name => $query) {
56
            $config->addNamedNativeQuery($name, $query['sql'], new $query['rsm']);
57 69
        }
58
59 69
        foreach ($options->getCustomHydrationModes() as $modeName => $hydrator) {
60
            $config->addCustomHydrationMode($modeName, $hydrator);
61 69
        }
62
63 69
        foreach ($options->getFilters() as $name => $class) {
64
            $config->addFilter($name, $class);
65 69
        }
66
67 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...
68 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...
69 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...
70 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...
71 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...
72
73 69
        if ($namingStrategy = $options->getNamingStrategy()) {
74 3
            if (is_string($namingStrategy)) {
75 2
                if (!$serviceLocator->has($namingStrategy)) {
76 1
                    throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy));
77 68
                }
78
79 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...
80 1
            } else {
81 1
                $config->setNamingStrategy($namingStrategy);
82
            }
83 2
        }
84
85 68
        if ($quoteStrategy = $options->getQuoteStrategy()) {
86
            if (is_string($quoteStrategy)) {
87
                if (!$serviceLocator->has($quoteStrategy)) {
88
                    throw new InvalidArgumentException(sprintf('Quote strategy "%s" not found', $quoteStrategy));
89
                }
90
91
                $config->setQuoteStrategy($serviceLocator->get($quoteStrategy));
0 ignored issues
show
Documentation introduced by
$serviceLocator->get($quoteStrategy) is of type object|array, but the function expects a object<Doctrine\ORM\Mapping\QuoteStrategy>.

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...
92
            } else {
93
                $config->setQuoteStrategy($quoteStrategy);
94
            }
95
        }
96
97 68
        if ($repositoryFactory = $options->getRepositoryFactory()) {
98
            if (is_string($repositoryFactory)) {
99
                if (!$serviceLocator->has($repositoryFactory)) {
100
                    throw new InvalidArgumentException(
101
                        sprintf('Repository factory "%s" not found', $repositoryFactory)
102
                    );
103
                }
104
105
                $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...
106
            } else {
107
                $config->setRepositoryFactory($repositoryFactory);
108 1
            }
109
        }
110
111 68
        if ($entityListenerResolver = $options->getEntityListenerResolver()) {
112 2
            if ($entityListenerResolver instanceof EntityListenerResolver) {
113 1
                $config->setEntityListenerResolver($entityListenerResolver);
114 1
            } else {
115 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...
116
            }
117 2
        }
118
119 68
        $secondLevelCache = $options->getSecondLevelCache();
120
121 68
        if ($secondLevelCache->isEnabled()) {
122 1
            $regionsConfig = new RegionsConfiguration(
123 1
                $secondLevelCache->getDefaultLifetime(),
124 1
                $secondLevelCache->getDefaultLockLifetime()
125 1
            );
126
127 1
            foreach ($secondLevelCache->getRegions() as $regionName => $regionConfig) {
128 1
                if (isset($regionConfig['lifetime'])) {
129 1
                    $regionsConfig->setLifetime($regionName, $regionConfig['lifetime']);
130 1
                }
131
132 1
                if (isset($regionConfig['lock_lifetime'])) {
133 1
                    $regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']);
134 1
                }
135 1
            }
136
137
            // As Second Level Cache caches queries results, we reuse the result cache impl
138 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...
139 1
            $cacheFactory->setFileLockRegionDirectory($secondLevelCache->getFileLockRegionDirectory());
140
141 1
            $cacheConfiguration = new CacheConfiguration();
142 1
            $cacheConfiguration->setCacheFactory($cacheFactory);
143 1
            $cacheConfiguration->setRegionsConfiguration($regionsConfig);
144
145 1
            $config->setSecondLevelCacheEnabled();
146 1
            $config->setSecondLevelCacheConfiguration($cacheConfiguration);
147 1
        }
148
149 68
        if ($className = $options->getDefaultRepositoryClassName()) {
150 57
            $config->setDefaultRepositoryClassName($className);
151 57
        }
152
153 68
        $this->setupDBALConfiguration($serviceLocator, $config);
154
155 68
        return $config;
156
    }
157
158 69
    protected function getOptionsClass()
159
    {
160 69
        return 'DoctrineORMModule\Options\Configuration';
161
    }
162
}
163