Completed
Push — master ( 16afda...c8a1ca )
by Derek Stephen
02:37
created

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