ContainerService::getContainer()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 14
cts 14
cp 1
rs 9.552
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
1
<?php
2
namespace Del\Common;
3
4
use Barnacle\Container;
5
use Barnacle\EntityRegistrationInterface;
6
use Barnacle\RegistrationInterface;
7
use Del\Common\Config\DbCredentials;
8
use Doctrine\ORM\Tools\Setup;
9
use Doctrine\ORM\EntityManager;
10
11
class ContainerService
12
{
13
    /**
14
     * @var Container
15
     */
16
    private $container;
17
18
    /**
19
     * @var DbCredentials
20
     */
21
    private $credentials;
22
23
    /**
24
     * @var array
25
     */
26
    private $paths;
27
28
    /** @var string $proxyPath */
29
    private $proxyPath;
30
31
    /** @var \Doctrine\ORM\Configuration $config */
32
    private $config;
33
34
    private function __construct(){}
35
    private function __clone(){}
36
37
    /**
38
     * @param Container|null $container
39
     * @return ContainerService|null
40
     */
41 9
    public static function getInstance(Container $container = null)
42
    {
43 9
        static $inst = null;
44 9
        if($inst === null)
45
        {
46 1
            $inst = new ContainerService();
47 1
            $inst->container = $container ?: new Container();
48 1
            $inst->paths = $inst->initEntityPaths();
49
        }
50 9
        return $inst;
51
    }
52
53
    /**
54
     *  by default looks for src/Entity
55
     *  or src/anything/Entity
56
     *
57
     * @return array
58
     */
59 1
    private function initEntityPaths(): array
60
    {
61 1
        $paths = [];
62 1
        $paths = $this->addPathIfExists($paths, 'src');
63 1
        $possibleModules = glob('src/*');
64
65 1
        foreach ($possibleModules as $path) {
66 1
            $paths = $this->addPathIfExists($paths, $path);
67
        }
68
69 1
        return $paths;
70
    }
71
72
    /**
73
     * @param array $paths
74
     * @param string $path
75
     * @return array
76
     */
77 1
    private function addPathIfExists(array $paths, string $path): array
78
    {
79 1
        if (is_dir($path . '/Entity')) {
80
            $paths[] = $path . '/Entity';
81
        }
82
83 1
        return $paths;
84
    }
85
86
87
    /**
88
     * @return Container
89
     * @throws \Doctrine\ORM\ORMException
90
     */
91 7
    public function getContainer()
92
    {
93 7
        if (!isset($this->dbInitialised)) {
94
95 7
            $this->container['db.credentials'] = $this->getDbCredentials()->toArray();
96 7
            $this->container['entity.paths'] = $this->getEntityPaths();
97
98 7
            $paths = $this->container['entity.paths'];
99 7
            $dbParams = $this->container['db.credentials'];
100 7
            $isDevMode = false;
101
102 7
            $this->config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode, null, null, false);
103 7
            $this->addProxyPath();
104 7
            $entityManager = EntityManager::create($dbParams, $this->config);
105
106
            // The Doctrine Entity Manager
107 7
            $this->container['doctrine.entity_manager'] = $entityManager;
108 7
            $this->container[EntityManager::class] = $entityManager;
109 7
            $this->container['dbInitialised'] = true;
110
111
        }
112 7
        return $this->container;
113
    }
114
115 7
    private function addProxyPath()
116
    {
117 7
        if(isset($this->proxyPath)) {
118 1
            $this->config->setProxyDir($this->proxyPath);
119
        }
120 7
    }
121
122
123
    /**
124
     * @param string $path
125
     * @return $this
126
     */
127 3
    public function addEntityPath($path)
128
    {
129 3
        if (is_dir($path)) {
130 1
            $this->paths[] = $path;
131
        }
132
133 3
        return $this;
134
    }
135
136
    /**
137
     * @return array
138
     */
139 8
    public function getEntityPaths()
140
    {
141 8
        return $this->paths;
142
    }
143
144
    /**
145
     * @param string $proxyPath
146
     * @return ContainerService
147
     */
148 1
    public function setProxyPath($proxyPath)
149
    {
150 1
        $this->proxyPath = $proxyPath;
151 1
        return $this;
152
    }
153
154
155
    /**
156
     * @return DbCredentials
157
     */
158 8
    public function getDbCredentials()
159
    {
160 8
        return $this->credentials ? $this->credentials : new DbCredentials();
161
    }
162
163
    /**
164
     * @param DbCredentials $credentials
165
     * @return $this
166
     */
167 3
    public function setDbCredentials(DbCredentials $credentials)
168
    {
169 3
        $this->credentials = $credentials;
170 3
        return $this;
171
    }
172
173
    /**
174
     * @param RegistrationInterface $config
175
     */
176 2
    public function registerToContainer(RegistrationInterface $config)
177
    {
178 2
        if($config instanceof EntityRegistrationInterface) {
179
            $this->addEntityPath($config->getEntityPath());
180
        }
181
182 2
        $config->addToContainer($this->container);
183 2
    }
184
}
185