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

AutowireItemBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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