Completed
Push — master ( 1b508f...644e50 )
by Gabriel
08:31
created

EntityManager::dimensions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Sinergi\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 Interop\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
        return [];
128
    }
129
}
130