Completed
Pull Request — develop (#607)
by Tom
01:30
created

ConfigurationFactory::getOptionsClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineORMModule\Service;
6
7
use Doctrine\ORM\Cache\CacheConfiguration;
8
use Doctrine\ORM\Cache\DefaultCacheFactory;
9
use Doctrine\ORM\Cache\RegionsConfiguration;
10
use Doctrine\ORM\Configuration;
11
use Doctrine\ORM\Mapping\EntityListenerResolver;
12
use DoctrineORMModule\Options\Configuration as DoctrineORMModuleConfiguration;
13
use DoctrineORMModule\Service\DBALConfigurationFactory as DoctrineConfigurationFactory;
14
use Interop\Container\ContainerInterface;
15
use Laminas\ServiceManager\Exception\InvalidArgumentException;
16
use Laminas\ServiceManager\ServiceLocatorInterface;
17
use function is_string;
18
use function sprintf;
19
20
class ConfigurationFactory extends DoctrineConfigurationFactory
21
{
22
    /**
23
     * {@inheritDoc}
24
     *
25
     * @return Configuration
26
     */
27 16
    public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null)
28
    {
29 16
        $options = $this->getOptions($container);
30 16
        $config = new Configuration();
31
32 16
        $config->setAutoGenerateProxyClasses($options->getGenerateProxies());
33 16
        $config->setProxyDir($options->getProxyDir());
34 16
        $config->setProxyNamespace($options->getProxyNamespace());
35
36 16
        $config->setEntityNamespaces($options->getEntityNamespaces());
37
38 16
        $config->setCustomDatetimeFunctions($options->getDatetimeFunctions());
39 16
        $config->setCustomStringFunctions($options->getStringFunctions());
40 16
        $config->setCustomNumericFunctions($options->getNumericFunctions());
41
42 16
        $config->setClassMetadataFactoryName($options->getClassMetadataFactoryName());
43
44 16
        foreach ($options->getNamedQueries() as $name => $query) {
45
            $config->addNamedQuery($name, $query);
46
        }
47
48 16
        foreach ($options->getNamedNativeQueries() as $name => $query) {
49
            $config->addNamedNativeQuery($name, $query['sql'], new $query['rsm']());
50
        }
51
52 16
        foreach ($options->getCustomHydrationModes() as $modeName => $hydrator) {
53
            $config->addCustomHydrationMode($modeName, $hydrator);
54
        }
55
56 16
        foreach ($options->getFilters() as $name => $class) {
57
            $config->addFilter($name, $class);
58
        }
59
60 16
        $config->setMetadataCacheImpl($container->get($options->getMetadataCache()));
61 16
        $config->setQueryCacheImpl($container->get($options->getQueryCache()));
62 16
        $config->setResultCacheImpl($container->get($options->getResultCache()));
63 16
        $config->setHydrationCacheImpl($container->get($options->getHydrationCache()));
64 16
        $config->setMetadataDriverImpl($container->get($options->getDriver()));
65
66 16
        $namingStrategy = $options->getNamingStrategy();
67 16
        if ($namingStrategy) {
68 3
            if (is_string($namingStrategy)) {
69 2
                if (! $container->has($namingStrategy)) {
70 1
                    throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy));
71
                }
72
73 1
                $config->setNamingStrategy($container->get($namingStrategy));
74
            } else {
75 1
                $config->setNamingStrategy($namingStrategy);
76
            }
77
        }
78
79 15
        $quoteStrategy = $options->getQuoteStrategy();
80 15
        if ($quoteStrategy) {
81 3
            if (is_string($quoteStrategy)) {
82 2
                if (! $container->has($quoteStrategy)) {
83 1
                    throw new InvalidArgumentException(sprintf('Quote strategy "%s" not found', $quoteStrategy));
84
                }
85
86 1
                $config->setQuoteStrategy($container->get($quoteStrategy));
87
            } else {
88 1
                $config->setQuoteStrategy($quoteStrategy);
89
            }
90
        }
91
92 14
        $repositoryFactory = $options->getRepositoryFactory();
93 14
        if ($repositoryFactory) {
94
            if (is_string($repositoryFactory)) {
95
                if (! $container->has($repositoryFactory)) {
96
                    throw new InvalidArgumentException(
97
                        sprintf('Repository factory "%s" not found', $repositoryFactory)
98
                    );
99
                }
100
101
                $config->setRepositoryFactory($container->get($repositoryFactory));
102
            } else {
103
                $config->setRepositoryFactory($repositoryFactory);
104
            }
105
        }
106
107 14
        $entityListenerResolver = $options->getEntityListenerResolver();
108 14
        if ($entityListenerResolver) {
109 2
            if ($entityListenerResolver instanceof EntityListenerResolver) {
110 1
                $config->setEntityListenerResolver($entityListenerResolver);
111
            } else {
112 1
                $config->setEntityListenerResolver($container->get($entityListenerResolver));
113
            }
114
        }
115
116 14
        $secondLevelCache = $options->getSecondLevelCache();
117
118 14
        if ($secondLevelCache->isEnabled()) {
119 1
            $regionsConfig = new RegionsConfiguration(
120 1
                $secondLevelCache->getDefaultLifetime(),
121 1
                $secondLevelCache->getDefaultLockLifetime()
122
            );
123
124 1
            foreach ($secondLevelCache->getRegions() as $regionName => $regionConfig) {
125 1
                if (isset($regionConfig['lifetime'])) {
126 1
                    $regionsConfig->setLifetime($regionName, $regionConfig['lifetime']);
127
                }
128
129 1
                if (! isset($regionConfig['lock_lifetime'])) {
130
                    continue;
131
                }
132
133 1
                $regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']);
134
            }
135
136
            // As Second Level Cache caches queries results, we reuse the result cache impl
137 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...
138 1
            $cacheFactory->setFileLockRegionDirectory($secondLevelCache->getFileLockRegionDirectory());
139
140 1
            $cacheConfiguration = new CacheConfiguration();
141 1
            $cacheConfiguration->setCacheFactory($cacheFactory);
142 1
            $cacheConfiguration->setRegionsConfiguration($regionsConfig);
143
144 1
            $config->setSecondLevelCacheEnabled();
145 1
            $config->setSecondLevelCacheConfiguration($cacheConfiguration);
146
        }
147
148 14
        $filterSchemaAssetsExpression = $options->getFilterSchemaAssetsExpression();
149 14
        if ($filterSchemaAssetsExpression) {
150
            $config->setFilterSchemaAssetsExpression($filterSchemaAssetsExpression);
0 ignored issues
show
Deprecated Code introduced by
The method Doctrine\DBAL\Configurat...chemaAssetsExpression() has been deprecated with message: Use Configuration::setSchemaAssetsFilter() instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
151
        }
152
153 14
        $className = $options->getDefaultRepositoryClassName();
154 14
        if ($className) {
155 1
            $config->setDefaultRepositoryClassName($className);
156
        }
157
158 14
        $this->setupDBALConfiguration($container, $config);
159
160 14
        return $config;
161
    }
162
163
    /**
164
     * @return mixed
165
     */
166 16
    public function createService(ServiceLocatorInterface $container)
167
    {
168 16
        return $this($container, Configuration::class);
169
    }
170
171 16
    protected function getOptionsClass() : string
172
    {
173 16
        return DoctrineORMModuleConfiguration::class;
174
    }
175
}
176