Passed
Push — master ( c51b1a...8af9fd )
by Simone
02:14
created

Container::getResolver()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
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 = array();
14
15
    private $builder;
16
17
    private $resolver;
18
19
    private $construcrtorResolver;
20
21
    private $methodResolver;
22
23 10
    public function __construct()
24
    {
25 10
        $this->register = new Register();
0 ignored issues
show
Bug introduced by
The property register does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
26
27 10
        $this->construcrtorResolver = new ConstructorResolver();
28
29 10
        $this->resolver = new Resolver();
30 10
        $this->resolver->setConstructorResolver($this->construcrtorResolver);
31
32 10
        $this->methodResolver = new MethodResolver();
33
34 10
        $this->builder = new ArgumentBuilder();
35 10
        $this->builder->setContainer($this);
36
37 10
        $this->methodResolver->setArgumentBuilder(
38 10
            $this->builder
39 10
        );
40 10
    }
41
42 9
    public function loadServices(array $services)
43
    {
44 9
        $this->services = $services;
45 9
    }
46
47 1
    public function getServicesConfiguration()
48
    {
49 1
        return $this->services;
50
    }
51
52 7
    private function ensureServiceIsDefined($serviceName)
53
    {
54 7
        if (!$this->contains($serviceName)) {
55 1
            throw new \RuntimeException(
56 1
                'Oops! Service ' . $serviceName . ' not defined'
57 1
            );
58
        }
59 6
    }
60
61 7
    public function get($serviceName)
62
    {
63 7
        $this->ensureServiceIsDefined($serviceName);
64
65 6
        $service = Service::box(array(
66 6
            'name' => $serviceName,
67 6
            'services' => $this->services,
68 6
        ));
69
70 6
        if (!$this->register->has($service)) {
71 6
            $this->register->register(
72 6
                $service,
73 6
                $this->getResolver($service)
74 6
            );
75 5
        }
76
77 5
        return $this->register->get($service);
78
    }
79
80 6
    public function getResolver(Service $service)
81
    {
82 6
        if ($service->hasMethodInjection()) {
83 2
            return $this->methodResolver;
84
        }
85
86 6
        if ($service->hasSimpleInjection()) {
87 2
            return $this->resolver;
88
        }
89
90 4
        return $this->construcrtorResolver;
91
    }
92
93 8
    public function contains($serviceName)
94
    {
95 8
        $service = Argument::fromString($serviceName);
96 8
        return isset($this->services[$service->getServiceName()]);
97
    }
98
99 1
    public function hasArguments($serviceName)
100
    {
101 1
        return isset($this->services[$serviceName]['params']);
102
    }
103
}
104