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

InjectingProviderProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A hp$0 ➔ get() 0 3 1
A __construct() 0 4 1
A hp$0 ➔ __construct() 0 4 1
get() 0 23 ?
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
            private InjectorInterface $injector;
37
            private Set $set;
38
39
            public function __construct(InjectorInterface $injector, Set $set)
40
            {
41
                $this->injector = $injector;
42
                $this->set = $set;
43
            }
44
45
            /**
46
             * @return mixed
47
             */
48
            public function get()
49
            {
50
                return $this->injector->getInstance($this->set->interface, $this->set->name);
51
            }
52
        };
53
    }
54
}
55