Completed
Push — master ( 1aa7d8...c51b1a )
by Simone
02:15
created

Container   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 85
ccs 44
cts 44
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A loadServices() 0 4 1
A getServicesConfiguration() 0 4 1
A ensureServiceIsDefined() 0 8 2
B get() 0 23 4
A contains() 0 5 1
A hasArguments() 0 4 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 = 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
            if ($service->isConstructorInjection()) {
72 2
                $this->register->register($service, $this->methodResolver);
73 2
            } else {
74 6
                if ($service->isMethodInjection()) {
75 2
                    $this->register->register($service, $this->resolver);
76 2
                } else {
77 4
                    $this->register->register($service, $this->construcrtorResolver);
78
                }
79
            }
80 5
        }
81
82 5
        return $this->register->get($service);
83
    }
84
85 8
    public function contains($serviceName)
86
    {
87 8
        $service = Argument::fromString($serviceName);
88 8
        return isset($this->services[$service->getServiceName()]);
89
    }
90
91 1
    public function hasArguments($serviceName)
92
    {
93 1
        return isset($this->services[$serviceName]['params']);
94
    }
95
}
96