Completed
Push — master ( 44b412...e96d03 )
by Andrey
02:04
created

AutowireItemBuilder   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 24
ccs 11
cts 12
cp 0.9167
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A build() 0 12 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Borodulin\Container\Autowire;
6
7
use Borodulin\Container\Autowire\Item\AliasItem;
8
use Borodulin\Container\Autowire\Item\CallableItem;
9
use Borodulin\Container\Autowire\Item\ClassItem;
10
use Borodulin\Container\ContainerException;
11
12
class AutowireItemBuilder
13
{
14
    /**
15
     * @var DependencyResolver
16
     */
17
    private $dependencyResolver;
18
19 13
    public function __construct(DependencyResolver $dependencyResolver)
20
    {
21 13
        $this->dependencyResolver = $dependencyResolver;
22 13
    }
23
24 11
    public function build(AutowireItemInterface $autowireItem): object
25
    {
26 11
        if ($autowireItem instanceof AliasItem) {
27 6
            return $this->dependencyResolver->resolveId($autowireItem->getAlias());
28 6
        } elseif ($autowireItem instanceof CallableItem) {
29 2
            $args = $this->dependencyResolver->resolveCallableArgs($autowireItem->getCallable());
30
31 2
            return \call_user_func_array($autowireItem->getCallable(), $args);
32 4
        } elseif ($autowireItem instanceof ClassItem) {
33 4
            return $this->dependencyResolver->resolveId($autowireItem->getClassName());
34
        } else {
35
            throw new ContainerException('Unsupported autowire item type.');
36
        }
37
    }
38
}
39