Completed
Push — master ( 448360...56edb4 )
by Simone
02:19
created

Container::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 1
crap 3
1
<?php
2
3
namespace Sensorario\Container;
4
5
use Sensorario\Container\Resolver\ConstructorResolver;
6
use Sensorario\Container\Resolver\MethodResolver;
7
use Sensorario\Container\Resolver\Resolver;
8
9
class Container
10
{
11
    private $services = array();
12
13
    private $builder;
14
15
    private $resolver;
16
17
    private $construcrtorResolver;
18
19
    private $methodResolver;
20
21 9
    public function __construct()
22
    {
23 9
        $this->resolver = new Resolver();
24
25 9
        $this->resolver->setConstructorResolver(
26 9
            $this->construcrtorResolver = new ConstructorResolver()
27 9
        );
28
29 9
        $this->methodResolver = new MethodResolver();
30
31 9
        $this->builder = new ArgumentBuilder();
32 9
        $this->builder->setContainer($this);
33
34 9
        $this->methodResolver->setArgumentBuilder(
35 9
            $this->builder
36 9
        );
37 9
    }
38
39 8
    public function loadServices(array $services)
40
    {
41 8
        $this->services = $services;
42 8
    }
43
44 1
    public function getServicesConfiguration()
45
    {
46 1
        return $this->services;
47
    }
48
49 6
    private function ensureServiceIsDefined($serviceName)
50
    {
51 6
        if (!$this->contains($serviceName)) {
52 1
            throw new \RuntimeException(
53 1
                'Oops! Service ' . $serviceName . ' not defined'
54 1
            );
55
        }
56 5
    }
57
58 6
    public function get($serviceName)
59
    {
60 6
        $this->ensureServiceIsDefined($serviceName);
61
62 5
        $service = Objects\Service::box(array(
63 5
            'name' => $serviceName,
64 5
            'services' => $this->services,
65 5
        ));
66
67 5
        if ($service->isConstructorInjection()) {
68 2
            return $this->methodResolver->resolve($service, $this->builder);
0 ignored issues
show
Unused Code introduced by
The call to MethodResolver::resolve() has too many arguments starting with $this->builder.

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...
69
        } else {
70 5
            if ($service->isMethodInjection()) {
71 1
                return $this->resolver->resolve($service);
72
            }
73
74 4
            return $this->construcrtorResolver->resolve($service);
75
        }
76
    }
77
78 7
    public function contains($serviceName)
79
    {
80 7
        $service = Objects\Argument::fromString($serviceName);
81 7
        return isset($this->services[$service->getServiceName()]);
82
    }
83
84 1
    public function hasArguments($serviceName)
85
    {
86 1
        return isset($this->services[$serviceName]['params']);
87
    }
88
}
89