Completed
Push — master ( 415647...d02380 )
by Ricardo
02:50
created

ServicesContainer::setRenderer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Ricardo Fiorani
5
 * Date: 31/08/2015
6
 * Time: 21:06.
7
 */
8
namespace RicardoFiorani\Container;
9
10
use RicardoFiorani\Adapter\CallableServiceAdapterFactoryInterface;
11
use RicardoFiorani\Exception\DuplicatedServiceNameException;
12
use RicardoFiorani\Renderer\EmbedRendererInterface;
13
14
class ServicesContainer
15
{
16
    /**
17
     * @var array
18
     */
19
    private $services = array();
20
21
    /**
22
     * @var array
23
     */
24
    private $patterns = array();
25
26
    /**
27
     * @var array
28
     */
29
    private $factories = array();
30
31
    /**
32
     * @var array
33
     */
34
    private $instantiatedFactories = array();
35
36
    /**
37
     * @var EmbedRendererInterface
38
     */
39
    private $renderer;
40
41
    /**
42
     * @var string
43
     */
44
    private $rendererName;
45
46
    /**
47
     * ServicesContainer constructor.
48
     *
49
     * @param array $config
50
     */
51
    public function __construct(array $config = array())
52
    {
53
        if (false == empty($config)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
54
            $this->registerFromConfig($config);
55
        }
56
    }
57
58
    /**
59
     * Loads de default config file.
60
     *
61
     * @param array $config
62
     *
63
     * @throws DuplicatedServiceNameException
64
     */
65
    private function registerFromConfig(array $config)
66
    {
67
        foreach ($config['services'] as $serviceName => $serviceConfig) {
68
            $this->registerService($serviceName, $serviceConfig['patterns'], $serviceConfig['factory']);
69
        }
70
        $this->setRenderer($config['renderer']['name'], $config['renderer']['factory']);
71
    }
72
73
    /**
74
     * Register a Service.
75
     *
76
     * @param string          $serviceName
77
     * @param array           $regex
78
     * @param string|callable $factory
79
     *
80
     * @throws DuplicatedServiceNameException
81
     */
82
    public function registerService($serviceName, array $regex, $factory)
83
    {
84
        if ($this->hasService($serviceName)) {
85
            throw new DuplicatedServiceNameException();
86
        }
87
88
        $this->services[] = $serviceName;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
89
        $this->patterns[$serviceName] = $regex;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
90
        $this->factories[$serviceName] = $factory;
91
    }
92
93
    /**
94
     * @param string $rendererName
95
     * @param string $rendererFactory
96
     */
97
    public function setRenderer($rendererName, $rendererFactory)
98
    {
99
        $this->rendererName = $rendererName;
100
        $factory = new $rendererFactory();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
101
        $this->renderer = $factory();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
102
    }
103
104
    /**
105
     * @return EmbedRendererInterface
106
     */
107
    public function getRenderer()
108
    {
109
        return $this->renderer;
110
    }
111
112
    /**
113
     * @return array
114
     */
115
    public function getServiceNameList()
116
    {
117
        return $this->services;
118
    }
119
120
    /**
121
     * @param string $serviceName
122
     *
123
     * @return bool
124
     */
125
    public function hasService($serviceName)
126
    {
127
        return in_array($serviceName, $this->services);
128
    }
129
130
    /**
131
     * @return array
132
     */
133
    public function getServices()
134
    {
135
        return $this->services;
136
    }
137
138
    /**
139
     * @return array
140
     */
141
    public function getPatterns()
142
    {
143
        return $this->patterns;
144
    }
145
146
    /**
147
     * @param string $serviceName
148
     *
149
     * @return CallableServiceAdapterFactoryInterface
150
     */
151
    public function getFactory($serviceName)
152
    {
153
        $factory = new $this->factories[$serviceName]();
154
        if (isset($this->instantiatedFactories[$serviceName])) {
155
            return $this->instantiatedFactories[$serviceName];
156
        }
157
158
        return $this->instantiatedFactories[$serviceName] = $factory;
159
    }
160
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
161