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

ContainerService   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 72.73%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 12
c 3
b 0
f 2
lcom 1
cbo 1
dl 0
loc 86
ccs 24
cts 33
cp 0.7273
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 1 1
A __clone() 0 1 1
A getInstance() 0 9 2
B getContainer() 0 25 2
A addEntityPath() 0 8 2
A getEntityPaths() 0 4 1
A getDbCredentials() 0 4 2
A setDbCredentials() 0 5 1
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