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

InjectingProviderProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 3
c 2
b 0
f 0
dl 0
loc 43
rs 10
eloc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A hp$0 ➔ get() 0 3 1
A hp$0 ➔ __construct() 0 4 1
get() 0 26 ?
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