Completed
Push — master ( 4966ad...2cf3a8 )
by Derek Stephen
02:07
created

ContainerService::addEntityPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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