Passed
Branch feature/tell-dont-ask/master (82958c)
by Simone
02:17
created

Container::services()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Sensorario\Container;
4
5
use Sensorario\Container\Objects\Argument;
6
use Sensorario\Container\Objects\Service;
7
use Sensorario\Container\Resolver\ConstructorResolver;
8
use Sensorario\Container\Resolver\MethodResolver;
9
use Sensorario\Container\Resolver\Resolver;
10
11
class Container
12
{
13
    private $services = [];
14
15
    private $builder;
16
17
    private $resolver;
18
19
    private $construcrtorResolver;
20
21
    private $methodResolver;
22
23
    private $register;
24
25 10
    public function __construct()
26
    {
27 10
        $this->register = new Register();
28
29 10
        $this->construcrtorResolver = new ConstructorResolver();
30
31 10
        $this->resolver = new Resolver();
32 10
        $this->resolver->acquire($this->construcrtorResolver);
33
34 10
        $this->methodResolver = new MethodResolver();
35
36 10
        $this->builder = new ArgumentBuilder();
37 10
        $this->builder->setContainer($this);
38
39 10
        $this->methodResolver->acquire(
40 10
            $this->builder
41
        );
42 10
    }
43
44 9
    public function loadServices(array $services) : void
45
    {
46 9
        $this->services = $services;
47 9
    }
48
49 1
    public function services() : array
50
    {
51 1
        return $this->services;
52
    }
53
54 7
    private function ensureServiceIsDefined(string $serviceName) : bool
55
    {
56 7
        if (!$this->contains($serviceName)) {
57 1
            throw new \RuntimeException(
58 1
                'Oops! Service ' . $serviceName . ' not defined'
59
            );
60
        }
61
62 6
        return true;
63
    }
64
65 7
    public function get(string $serviceName)
66
    {
67 7
        $this->ensureServiceIsDefined($serviceName);
68
69 6
        $service = Service::box([
70 6
            'name' => $serviceName,
71 6
            'services' => $this->services,
72
        ]);
73
74 6
        if (!$this->register->has($service)) {
75 6
            $this->register->store(
76
                $service,
77 6
                $this->getResolver($service)
78
            );
79
        }
80
81 5
        return $this->register->get($service);
82
    }
83
84 6
    public function getResolver(Service $service)
85
    {
86 6
        if ($service->hasMethodInjection()) {
87 2
            return $this->methodResolver;
88
        }
89
90 6
        if ($service->hasSimpleInjection()) {
91 2
            return $this->resolver;
92
        }
93
94 4
        return $this->construcrtorResolver;
95
    }
96
97 8
    public function contains(string $serviceName) : bool
98
    {
99 8
        $service = Argument::fromString($serviceName);
100 8
        return isset($this->services[$service->name()]);
101
    }
102
103 1
    public function hasArguments(string $serviceName) : bool
104
    {
105 1
        return isset($this->services[$serviceName]['params']);
106
    }
107
}
108