Completed
Push — 2.x ( e477da...5c5bb8 )
by Akihito
23s queued 12s
created

ProviderSetProvider.php$0 ➔ get()   A

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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