ServiceLocator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 82
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getClient() 0 10 2
A getDocumentManager() 0 10 2
A getOptions() 0 9 2
A buildClient() 0 13 1
A buildDocumentManager() 0 6 1
1
<?php
2
/**
3
 * Ember Db - An embeddable document database for php.
4
 * Copyright (C) 2016 Alexander During
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * @link      http://github.com/alexanderduring/php-ember-db
20
 * @copyright Copyright (C) 2016 Alexander During
21
 * @license   http://www.gnu.org/licenses GNU General Public License v3.0
22
 */
23
24
namespace EmberDb\Client;
25
26
use EmberDb\DocumentManager;
27
28
class ServiceLocator
29
{
30
    private $instances = array();
31
32
33
34
    /**
35
     * @return \EmberDb\Client\Client
36
     */
37
    public function getClient()
38
    {
39
        $name = 'EmberDb\Client\Client';
40
        if (!array_key_exists($name, $this->instances)) {
41
            $client = $this->buildClient();
42
            $this->instances[$name] = $client;
43
        }
44
45
        return $this->instances[$name];
46
    }
47
48
49
50
    /**
51
     * @return \EmberDb\DocumentManager
52
     */
53
    public function getDocumentManager()
54
    {
55
        $name = 'EmberDb\DocumentManger';
56
        if (!array_key_exists($name, $this->instances)) {
57
            $documentManager = $this->buildDocumentManager();
58
            $this->instances[$name] = $documentManager;
59
        }
60
61
        return $this->instances[$name];
62
    }
63
64
65
66
    /**
67
     * @return \EmberDb\Client\Options
68
     */
69
    public function getOptions()
70
    {
71
        if (!array_key_exists('EmberDb\Client\Options', $this->instances)) {
72
            $options = new Options();
73
            $this->instances['EmberDb\Client\Options'] = $options;
74
        }
75
76
        return $this->instances['EmberDb\Client\Options'];
77
    }
78
79
80
81
    /**
82
     * @return \EmberDb\Client\Client
83
     */
84
    private function buildClient()
85
    {
86
        $client = new Client();
87
88
        // Inject this service locator
89
        $client->injectServiceLocator($this);
90
91
        // Inject options
92
        $options = $this->getOptions();
93
        $client->injectOptions($options);
94
95
        return $client;
96
    }
97
98
99
100
    /**
101
     * @return \EmberDb\DocumentManager
102
     */
103
    private function buildDocumentManager()
104
    {
105
        $documentManager = new DocumentManager();
106
107
        return $documentManager;
108
    }
109
}
110