Completed
Push — master ( e3a45f...174b79 )
by Derek Stephen
02:08
created

ContainerService::initEntityPaths()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 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
44 1
            $inst->paths = $inst->initEntityPaths();
45
        }
46 9
        return $inst;
47
    }
48
49
    /**
50
     *  by default looks for src/Entity
51
     *  or src/anything/Entity
52
     *
53
     * @return array
54
     */
55 1
    private function initEntityPaths(): array
56
    {
57 1
        $paths = [];
58 1
        $paths = $this->addPathIfExists($paths, 'src');
59 1
        $possibleModules = glob('src/*');
60
61 1
        foreach ($possibleModules as $path) {
62 1
            $paths = $this->addPathIfExists($paths, $path);
63
        }
64
65 1
        return $paths;
66
    }
67
68
    /**
69
     * @param array $paths
70
     * @param string $path
71
     * @return array
72
     */
73 1
    private function addPathIfExists(array $paths, string $path): array
74
    {
75 1
        if (is_dir($path . '/Entity')) {
76
            $paths[] = $path . '/Entity';
77
        }
78
79 1
        return $paths;
80
    }
81
82
83
    /**
84
     * @return PimpleContainer
85
     */
86 7
    public function getContainer()
87
    {
88 7
        if (!isset($this->dbInitialised)) {
89
90 7
            $this->container['db.credentials'] = $this->getDbCredentials()->toArray();
91 7
            $this->container['entity.paths'] = $this->getEntityPaths();
92
93 7
            $paths = $this->container['entity.paths'];
94 7
            $dbParams = $this->container['db.credentials'];
95 7
            $isDevMode = false;
96
97 7
            $this->config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
98 7
            $this->addProxyPath();
99 7
            $entityManager = EntityManager::create($dbParams, $this->config);
100
101
            // The Doctrine Entity Manager
102 7
            $this->container['doctrine.entity_manager'] = $entityManager;
103 7
            $this->container['dbInitialised'] = true;
104
105
        }
106 7
        return $this->container;
107
    }
108
109 7
    private function addProxyPath()
110
    {
111 7
        if(isset($this->proxyPath)) {
112 1
            $this->config->setProxyDir($this->proxyPath);
113
        }
114 7
    }
115
116
117
118
    /**
119
     * @param string $path
120
     * @return $this
121
     */
122 4
    public function addEntityPath($path)
123
    {
124 4
        if (is_dir($path)) {
125 2
            $this->paths[] = $path;
126
        }
127
128 4
        return $this;
129
    }
130
131
    /**
132
     * @return array
133
     */
134 8
    public function getEntityPaths()
135
    {
136 8
        return $this->paths;
137
    }
138
139
    /**
140
     * @param string $proxyPath
141
     * @return ContainerService
142
     */
143 1
    public function setProxyPath($proxyPath)
144
    {
145 1
        $this->proxyPath = $proxyPath;
146 1
        return $this;
147
    }
148
149
150
151
    /**
152
     * @return DbCredentials
153
     */
154 8
    public function getDbCredentials()
155
    {
156 8
        return $this->credentials ? $this->credentials : new DbCredentials();
157
    }
158
159
    /**
160
     * @param DbCredentials $credentials
161
     * @return $this
162
     */
163 3
    public function setDbCredentials(DbCredentials $credentials)
164
    {
165 3
        $this->credentials = $credentials;
166 3
        return $this;
167
    }
168
169
170 2
    public function registerToContainer(RegistrationInterface $config)
171
    {
172 2
        if($config->hasEntityPath()) {
173 1
            $this->addEntityPath($config->getEntityPath());
174
        }
175 2
        $config->addToContainer($this->container);
176 2
    }
177
}
178