Completed
Push — master ( 68fe25...8e1e8c )
by Woody
11s
created

EntityManager::__invoke()   C

Complexity

Conditions 9
Paths 192

Size

Total Lines 55
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
cc 9
eloc 33
nc 192
nop 1
dl 0
loc 55
ccs 0
cts 43
cp 0
crap 90
rs 6.6291
c 0
b 0
f 0

How to fix   Long Method   

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 Northwoods\Config\Polyfill\Doctrine;
4
5
use Doctrine\Common\Annotations\AnnotationReader;
6
use Doctrine\ORM\Mapping\DefaultQuoteStrategy;
7
use Doctrine\ORM\Tools\Setup;
8
use Interop\Config\ConfigurationTrait;
9
use Interop\Config\RequiresMandatoryOptions;
10
use Interop\Config\RequiresConfigId;
11
use Psr\Container\ContainerInterface;
12
use Doctrine\ORM\EntityManager as DoctrineEntityManager;
13
use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySqlDriver;
14
15
class EntityManager implements RequiresMandatoryOptions, RequiresConfigId
16
{
17
    use ConfigurationTrait;
18
19
    public function __invoke(ContainerInterface $container)
20
    {
21
        $options = $this->options($container->get('config'));
22
        $driverClass = $options['driverClass'];
23
        $metadataDriverClass = $options['metadataDriverClass'];
24
        $cacheDriverClass = isset($options['cacheDriverClass']) ? $options['cacheDriverClass'] : null;
25
        $sqlLoggerDriverClass = $options['sqlLoggerDriverClass'];
26
27
        $doctrineConfig = Setup::createConfiguration($options['is_dev_mode']);
28
29
        $doctrineConfig->setMetadataDriverImpl(
30
            new $metadataDriverClass(new AnnotationReader(), $options['paths'])
31
        );
32
33
        $doctrineConfig->setQuoteStrategy(new DefaultQuoteStrategy());
34
35
        if ($cacheDriverClass) {
36
            $cache = new $cacheDriverClass;
37
            $doctrineConfig->setQueryCacheImpl($cache);
38
            $doctrineConfig->setMetadataCacheImpl($cache);
39
            $doctrineConfig->setHydrationCacheImpl($cache);
40
            $doctrineConfig->setResultCacheImpl($cache);
41
        }
42
43
        if (isset($options['proxy_dir'])) {
44
            $doctrineConfig->setProxyDir($options['proxy_dir']);
45
            if (isset($connectionConfig['proxy_namespace'])) {
0 ignored issues
show
Bug introduced by
The variable $connectionConfig seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
46
                $doctrineConfig->setProxyNamespace($options['proxy_namespace']);
47
            } else {
48
                $doctrineConfig->setProxyNamespace('Proxies');
49
            }
50
        }
51
52
        if (!isset($options['params']['charset'])) {
53
            $options['params']['charset'] = 'utf8';
54
        }
55
56
        if (!isset($options['params']['driver'])) {
57
            $options['params']['driver'] = (new $driverClass())->getName();
58
        }
59
60
        $entityManager = DoctrineEntityManager::create($options['params'], $doctrineConfig);
61
62
        $connection = $entityManager->getConnection();
63
64
        if ($driverClass === PDOMySqlDriver::class) {
65
            $connection->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
66
        }
67
68
        if (isset($sqlLoggerDriverClass)) {
69
            $connection->getConfiguration()->setSQLLogger(new $sqlLoggerDriverClass);
70
        }
71
72
        return $entityManager;
73
    }
74
75
    /**
76
     * @return string
77
     */
78
    public function vendorName()
79
    {
80
        return 'doctrine';
81
    }
82
83
    /**
84
     * @return string
85
     */
86
    public function packageName()
87
    {
88
        return 'connection';
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function containerId()
95
    {
96
        return 'default';
97
    }
98
99
    /**
100
     * @return string[] List with mandatory options
101
     */
102
    public function mandatoryOptions()
103
    {
104
        return [
105
            'driverClass',
106
            'params',
107
        ];
108
    }
109
110
    /**
111
     * @return string[] List with optional options
112
     */
113
    public function optionalOptions()
114
    {
115
        return [
116
            'metadataDriverClass',
117
            'is_dev_mode',
118
            'proxy_dir',
119
            'proxy_namespace',
120
            'paths',
121
            'cacheDriverClass',
122
            'sqlLoggerDriverClass',
123
        ];
124
    }
125
126
    public function dimensions()
127
    {
128
        return [];
129
    }
130
}
131