Completed
Push — master ( e9d9a3...16afda )
by Derek Stephen
03:14
created

ContainerService::getContainer()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 22
rs 9.2
ccs 12
cts 12
cp 1
cc 2
eloc 11
nc 1
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 5
17
    /**
18 5
     * @var DbCredentials
19 5
     */
20 5
    private $credentials;
21 1
22 1
    /**
23 5
     * @var array
24
     */
25
    private $paths;
26
27
    public function __construct(){}
28
    public function __clone(){}
29
30 3
    public static function getInstance()
31
    {
32 3
        static $inst = null;
33
        if($inst === null)
34 3
        {
35
            $inst = new ContainerService();
36 3
            $inst->container = new PimpleContainer();
37
        }
38
        return $inst;
39 3
    }
40
41 2
42
    /**
43 2
     * @return PimpleContainer
44
     */
45 2
    public function getContainer()
46
    {
47 2
            $this->container['db.credentials'] = $this->getDbCredentials()->toArray();
48 2
49
            $this->container['entity.paths'] = $this->getEntityPaths();
50 2
51 3
            // The Doctrine Entity Manager
52
            $this->container['doctrine.entity_manager'] = $this->container->factory(function ($c) {
53 3
54
                $isDevMode = false;
55
56
                $paths = !empty($c['entity.paths']) ? $c['entity.paths'] : ['src/Entity'];
57
58
                $dbParams = $c['db.credentials'];
59 2
60
                $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
61 2
                $entityManager = EntityManager::create($dbParams, $config);
62 1
63 1
                return $entityManager;
64 2
            });
65 2
        return $this->container;
66
    }
67
68
69
70
    /**
71 4
     * @param string $path
72
     * @return $this
73 4
     */
74
    public function addEntityPath($path)
75
    {
76
        if(!isset($this->paths)) {
77
            $this->paths = [];
78
        }
79 4
        $this->paths[] = $path;
80
        return $this;
81 4
    }
82
83
    /**
84
     * @return array
85
     */
86
    public function getEntityPaths()
87
    {
88 2
        return $this->paths;
89
    }
90 2
91 2
    /**
92
     * @return DbCredentials
93
     */
94
    public function getDbCredentials()
95
    {
96
        return $this->credentials ? $this->credentials : new DbCredentials();
97
    }
98
99
    /**
100
     * @param DbCredentials $credentials
101
     * @return $this
102
     */
103
    public function setDbCredentials(DbCredentials $credentials)
104
    {
105
        $this->credentials = $credentials;
106
        return $this;
107
    }
108
109
110
    public function registerToContainer(RegistrationInterface $config)
111
    {
112
        $config->addToContainer($this->container);
113
    }
114
}
115