Passed
Pull Request — master (#3)
by Simone
02:13
created

Container::simpleResolver()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A Container::contains() 0 5 1
1
<?php
2
3
namespace Sensorario\Container;
4
5
use ReflectionClass;
6
7
class Container
8
{
9
    private $services = [];
10
11
    private $builder;
12
13
    private $instances = [];
0 ignored issues
show
Unused Code introduced by
The property $instances is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
14
15
    private $resolver;
16
17 9
    public function __construct()
18
    {
19 9
        $this->resolver = new Resolver();
20 9
    }
21
22 2
    public function setArgumentBuilder(ArgumentBuilder $builder)
23
    {
24 2
        $this->builder = $builder;
25 2
        $this->builder->setContainer($this);
26 2
    }
27
28 8
    public function loadServices(array $services)
29
    {
30 8
        $this->services = $services;
31 8
    }
32
33 1
    public function getServicesConfiguration()
34
    {
35 1
        return $this->services;
36
    }
37
38 6
    private function ensureServiceIsDefined($serviceName)
39
    {
40 6
        if (!$this->contains($serviceName)) {
41 1
            throw new \RuntimeException(
42 1
                'Oops! Service ' . $serviceName . ' not defined'
43
            );
44
        }
45 5
    }
46
47 3
    private function ensureBuilderIsDefined()
48
    {
49 3
        if (!$this->builder) {
50 1
            throw new \RuntimeException(
51 1
                'Oops! No builder, no party!'
52
            );
53
        }
54 2
    }
55
56 6
    public function get(string $serviceName)
57
    {
58 6
        $this->ensureServiceIsDefined($serviceName);
59
60 5
        $service = Objects\Service::box([
61 5
            'name' => $serviceName,
62 5
            'services' => $this->services,
63
        ]);
64
65 5
        if ($service->isConstructorEmpty()) {
66 4
            return $this->resolver->simpleResolver($service);
67
        }
68
69 3
        $this->ensureBuilderIsDefined();
70
71 2
        return $this->resolver->resolve($service, $this->builder);
72
    }
73
74 7
    public function contains(string $serviceName)
75
    {
76 7
        $service = Objects\Argument::fromString($serviceName);
77 7
        return isset($this->services[$service->getServiceName()]);
78
    }
79
80 1
    public function hasArguments($serviceName)
81
    {
82 1
        return isset($this->services[$serviceName]['params']);
83
    }
84
}
85