Completed
Pull Request — master (#8)
by Ricardo
01:45
created

ServicesContainer::getServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
namespace RicardoFiorani\Container;
4
5
use RicardoFiorani\Adapter\CallableServiceAdapterFactoryInterface;
6
use RicardoFiorani\Container\Exception\DuplicatedServiceNameException;
7
use RicardoFiorani\Renderer\EmbedRendererInterface;
8
9
class ServicesContainer
10
{
11
    private $services = array();
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...
12
    private $patterns = array();
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...
13
    private $factories = array();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 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...
14
    private $instantiatedFactories = array();
15
    private $renderer;
16
17
    /**
18
     * @throws DuplicatedServiceNameException
19
     */
20 1
    public function __construct(array $config = [])
21
    {
22 1
        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...
23
            $this->registerFromConfig($config);
24
        }
25 1
    }
26
27
    /**
28
     * @throws DuplicatedServiceNameException
29
     */
30
    private function registerFromConfig(array $config)
31
    {
32
        foreach ($config['services'] as $serviceName => $serviceConfig) {
33
            $this->registerService($serviceName, $serviceConfig['patterns'], new $serviceConfig['factory']);
34
        }
35
36
        $this->setRenderer($config['renderer']['name'], new $config['renderer']['factory']);
0 ignored issues
show
Unused Code introduced by
The call to ServicesContainer::setRenderer() has too many arguments starting with new $config['renderer']['factory']().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
37
    }
38
39
    /**
40
     * @throws DuplicatedServiceNameException
41
     */
42 1
    public function registerService(string $serviceName, array $regex, callable $factory)
43
    {
44 1
        if ($this->hasService($serviceName)) {
45 1
            throw new DuplicatedServiceNameException(
46 1
                sprintf(
47 1
                    'The service "%s" is already registered in the container.',
48 1
                    $serviceName
49
                )
50
            );
51
        }
52
53 1
        $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...
54 1
        $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...
55 1
        $this->factories[$serviceName] = $factory;
56 1
    }
57
58
    public function setRenderer(callable $rendererFactory)
59
    {
60
        $this->renderer = $rendererFactory();
61
    }
62
63
    public function getRenderer(): EmbedRendererInterface
64
    {
65
        return $this->renderer;
66
    }
67
68 1
    public function getServiceNameList(): array
69
    {
70 1
        return $this->services;
71
    }
72
73 1
    public function hasService($serviceName): bool
74
    {
75 1
        return in_array($serviceName, $this->services);
76
    }
77
78
    public function getServices(): array
79
    {
80
        return $this->services;
81
    }
82
83
    public function getPatterns(): array
84
    {
85
        return $this->patterns;
86
    }
87
88
    public function getFactory($serviceName): CallableServiceAdapterFactoryInterface
89
    {
90
        $factory = new $this->factories[$serviceName]();
91
        if (isset($this->instantiatedFactories[$serviceName])) {
92
            return $this->instantiatedFactories[$serviceName];
93
        }
94
95
        return $this->instantiatedFactories[$serviceName] = $factory;
96
    }
97
}
98