Completed
Push — master ( 759d7b...e0cdcf )
by Simone
02:12
created

Resolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.88%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 57
ccs 31
cts 32
cp 0.9688
rs 10
c 2
b 1
f 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 13 2
A simpleResolver() 0 14 2
A methods() 0 23 3
1
<?php
2
3
namespace Sensorario\Container;
4
5
use ReflectionClass;
6
7
class Resolver
8
{
9
    private $instances = array();
10
11 2
    public function resolve($service, ArgumentBuilder $builder)
12
    {
13 2
        $builder->setParams($service->getParams());
14
15 2
        $arguments = $builder->getArguments();
16
17 2
        if (!isset($this->instances[$service->getName()])) {
18 2
            $refObj = new ReflectionClass($service->getClass());
19 2
            $this->instances[$service->getName()] = $refObj->newInstanceArgs($arguments);
20 2
        }
21
22 2
        return $this->instances[$service->getName()];
23
    }
24
25 5
    public function simpleResolver($service)
26
    {
27 5
        if ($service->classNotExists()) {
28 1
            throw new \RuntimeException(
29 1
                'Oops! Class ' . $service->getClass() .
30 1
                ' defined as ' . $service->getName() .
31
                ' not found!!!'
32 1
            );
33
        }
34
35 4
        $serviceClass = $service->getClass();
36
37 4
        return new $serviceClass();
38
    }
39
40 1
    public function methods($service)
41
    {
42 1
        $resolution = $this->simpleResolver($service);
43
44 1
        foreach ($service->getMethods() as $methodName => $value) {
45 1
            $argument = Objects\Argument::fromString($value);
46
47 1
            if ($argument->isService()) {
48 1
                $collabortor = Objects\Service::box(array(
49 1
                    'name' => $argument->getServiceName(),
50 1
                    'services' => $service->getServicesConfiguration(),
51 1
                ));
52
53 1
                $resolution->$methodName(
54 1
                    $this->simpleResolver($collabortor)
55 1
                );
56 1
            } else {
57
                $resolution->$methodName($value);
58
            }
59 1
        }
60
61 1
        return $resolution;
62
    }
63
}
64