Autowiring   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 28
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 3 1
A __construct() 0 4 1
A get() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cekta\DI\Strategy;
6
7
use Cekta\DI\Exception\NotFound;
8
use Cekta\DI\Reflection;
9
use Cekta\DI\Strategy\Definition\Factory;
10
use Psr\Container\ContainerInterface;
11
12
class Autowiring implements ContainerInterface
13
{
14
    /**
15
     * @var Reflection
16
     */
17
    private $reflection;
18
    /**
19
     * @var ContainerInterface
20
     */
21
    private $container;
22
23
    public function __construct(Reflection $reflection, ContainerInterface $container)
24
    {
25
        $this->reflection = $reflection;
26
        $this->container = $container;
27
    }
28
29
    public function get($id)
30
    {
31
        if (!$this->has($id)) {
32
            throw new NotFound($id);
33
        }
34
        return (new Factory($id, ...$this->reflection->getDependencies($id)))($this->container);
35
    }
36
37
    public function has($id): bool
38
    {
39
        return $this->reflection->isInstantiable($id);
40
    }
41
}
42