Completed
Push — master ( 163f30...851cef )
by Derek Stephen
01:37
created

ContainerService   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 17
c 4
b 0
f 0
lcom 1
cbo 6
dl 0
loc 134
ccs 43
cts 43
cp 1
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A __clone() 0 1 1
A getInstance() 0 11 2
A getContainer() 0 22 2
A addProxyPath() 0 6 2
A getEntityPaths() 0 4 1
A setProxyPath() 0 5 1
A getDbCredentials() 0 4 2
A setDbCredentials() 0 5 1
A registerToContainer() 0 7 2
A addEntityPath() 0 8 2
1
<?php
2
namespace Del\Common;
3
4
use Pimple\Container as PimpleContainer;
5
use Del\Common\Container\RegistrationInterface;
6
use Del\Common\Config\DbCredentials;
7
use Doctrine\ORM\Tools\Setup;
8
use Doctrine\ORM\EntityManager;
9
10
class ContainerService
11
{
12
    /**
13
     * @var PimpleContainer
14
     */
15
    private $container;
16
17
    /**
18
     * @var DbCredentials
19
     */
20
    private $credentials;
21
22
    /**
23
     * @var array
24
     */
25
    private $paths;
26
27
    /** @var string $proxyPath */
28
    private $proxyPath;
29
30
    /** @var \Doctrine\ORM\Configuration $config */
31
    private $config;
32
33
    public function __construct(){}
34
    public function __clone(){}
35
36 9
    public static function getInstance()
37
    {
38 9
        static $inst = null;
39 9
        if($inst === null)
40
        {
41 1
            $inst = new ContainerService();
42 1
            $inst->container = new PimpleContainer();
43 1
            $inst->paths = ['src/Entity'];
44
        }
45 9
        return $inst;
46
    }
47
48
49
    /**
50
     * @return PimpleContainer
51
     */
52 7
    public function getContainer()
53
    {
54 7
        if (!isset($this->dbInitialised)) {
55
56 7
            $this->container['db.credentials'] = $this->getDbCredentials()->toArray();
57 7
            $this->container['entity.paths'] = $this->getEntityPaths();
58
59 7
            $paths = $this->container['entity.paths'];
60 7
            $dbParams = $this->container['db.credentials'];
61 7
            $isDevMode = false;
62
63 7
            $this->config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
64 7
            $this->addProxyPath();
65 7
            $entityManager = EntityManager::create($dbParams, $this->config);
66
67
            // The Doctrine Entity Manager
68 7
            $this->container['doctrine.entity_manager'] = $entityManager;
69 7
            $this->container['dbInitialised'] = true;
70
71
        }
72 7
        return $this->container;
73
    }
74
75 7
    private function addProxyPath()
76
    {
77 7
        if(isset($this->proxyPath)) {
78 1
            $this->config->setProxyDir($this->proxyPath);
79
        }
80 7
    }
81
82
83
84
    /**
85
     * @param string $path
86
     * @return $this
87
     */
88 4
    public function addEntityPath($path)
89
    {
90 4
        if (is_dir($path)) {
91 2
            $this->paths[] = $path;
92
        }
93
94 4
        return $this;
95
    }
96
97
    /**
98
     * @return array
99
     */
100 8
    public function getEntityPaths()
101
    {
102 8
        return $this->paths;
103
    }
104
105
    /**
106
     * @param string $proxyPath
107
     * @return ContainerService
108
     */
109 1
    public function setProxyPath($proxyPath)
110
    {
111 1
        $this->proxyPath = $proxyPath;
112 1
        return $this;
113
    }
114
115
116
117
    /**
118
     * @return DbCredentials
119
     */
120 8
    public function getDbCredentials()
121
    {
122 8
        return $this->credentials ? $this->credentials : new DbCredentials();
123
    }
124
125
    /**
126
     * @param DbCredentials $credentials
127
     * @return $this
128
     */
129 3
    public function setDbCredentials(DbCredentials $credentials)
130
    {
131 3
        $this->credentials = $credentials;
132 3
        return $this;
133
    }
134
135
136 2
    public function registerToContainer(RegistrationInterface $config)
137
    {
138 2
        if($config->hasEntityPath()) {
139 1
            $this->addEntityPath($config->getEntityPath());
140
        }
141 2
        $config->addToContainer($this->container);
142 2
    }
143
}
144