Completed
Pull Request — master (#562)
by Oliver
04:48 queued 02:59
created

ConfigurationFactory::__invoke()   F

Complexity

Conditions 22
Paths 10576

Size

Total Lines 128
Code Lines 74

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 59
CRAP Score 23.8766

Importance

Changes 0
Metric Value
dl 0
loc 128
ccs 59
cts 70
cp 0.8429
rs 2
c 0
b 0
f 0
cc 22
eloc 74
nc 10576
nop 3
crap 23.8766

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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