Completed
Push — master ( 2b3c09...5fab54 )
by Derek Stephen
10:41
created

ContainerService::getDbCredentials()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 6
1
<?php
2
namespace Del\Common;
3
4
use Pimple\Container as PimpleContainer;
5
use Doctrine\ORM\Tools\Setup;
6
use Doctrine\ORM\EntityManager;
7
8
class ContainerService
9
{
10
    private $credentials;
11
    private $paths;
12
13
    public function __construct(){}
14
    public function __clone(){}
15
16
    public static function getInstance()
17 3
    {
18
        static $inst = null;
19 3
        if($inst === null)
20 3
        {
21 3
            $inst = new ContainerService();
22 1
        }
23 1
        return $inst;
24 3
    }
25
26
27
    /**
28
     * @return PimpleContainer
29
     */
30
    public function getContainer()
31 2
    {
32
        $container = new PimpleContainer();
33 2
34
        $container['db.credentials'] = $this->getDbCredentials()->toArray();
35 2
36
        $container['entity.paths'] = $this->getEntityPaths();
37 2
38
        // The Doctrine Entity Manager
39
        $container['doctrine.entity_manager'] = $container->factory(function ($c) {
40 2
41
            $isDevMode = false;
42 1
43
            $paths = isset($c['entity.paths']) ? $c['entity.paths'] : ['src/Entity'];
44 1
45 1
            $dbParams = $c['db.credentials'];
46
47
            $config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
48
            $entityManager = EntityManager::create($dbParams, $config);
49 1
50 1
            return $entityManager;
51
        });
52
53
        return $container;
54 1
    }
55 1
    /**
56
     * @param string $path
57 1
     * @return $this
58 2
     */
59
    public function addEntityPath($path)
60 2
    {
61
        if(!isset($this->paths)) {
62
            $this->paths = [];
63
        }
64
        $this->paths[] = $path;
65
        return $this;
66
    }
67
68
    /**
69
     * @return array
70
     */
71
    public function getEntityPaths()
72
    {
73
        return $this->paths;
74
    }
75
76
    /**
77
     * @return DbCredentials
78
     */
79
    public function getDbCredentials()
80
    {
81
        return $this->credentials ? $this->credentials : new DbCredentials();
82
    }
83
84
    /**
85
     * @param DbCredentials $credentials
86
     * @return $this
87 2
     */
88
    public function setDbCredentials(DbCredentials $credentials)
89 2
    {
90
        $this->credentials = $credentials;
91
        return $this;
92
    }
93
}
94