Passed
Pull Request — 2.x (#262)
by Akihito
02:51 queued 01:30
created

InjectingProviderProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Di\Di\Set;
8
9
use function assert;
10
11
final class InjectingProviderProvider implements ProviderInterface
12
{
13
    /** @var InjectionPointInterface */
14
    private $ip;
15
16
    /** @var InjectorInterface */
17
    private $injector;
18
19
    public function __construct(InjectionPointInterface $ip, InjectorInterface $injector)
20
    {
21
        $this->ip = $ip;
22
        $this->injector = $injector;
23
    }
24
25
    /**
26
     * @return mixed
27
     */
28
    public function get()
29
    {
30
        $set = $this->ip->getParameter()->getAttributes(Set::class)[0];
31
        $instance = $set->newInstance();
32
        assert($instance instanceof Set);
33
34
        return new class ($this->injector, $instance) implements ProviderInterface
35
        {
36
            /** @var InjectorInterface  */
37
            private $injector;
38
39
            /** @var Set  */
40
            private $set;
41
42
            public function __construct(InjectorInterface $injector, Set $set)
43
            {
44
                $this->injector = $injector;
45
                $this->set = $set;
46
            }
47
48
            /**
49
             * @return mixed
50
             */
51
            public function get()
52
            {
53
                return $this->injector->getInstance($this->set->interface, $this->set->name);
54
            }
55
        };
56
    }
57
}
58