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

Container   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 6
dl 0
loc 80
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A loadServices() 0 4 1
A getServicesConfiguration() 0 4 1
A ensureServiceIsDefined() 0 8 2
A get() 0 19 3
A contains() 0 5 1
A hasArguments() 0 4 1
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