Passed
Pull Request — 2.x (#264)
by Akihito
01:27
created

MultiBinder::toInstance()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di;
6
7
use Ray\Di\Exception\Unbound;
8
use Ray\Di\MultiBinding\LazyCollection;
9
use Ray\Di\MultiBinding\LazyInstance;
10
use Ray\Di\MultiBinding\LazyInteterface;
11
use Ray\Di\MultiBinding\LazyProvider;
12
use Ray\Di\MultiBinding\LazyTo;
13
14
final class MultiBinder
15
{
16
    /** @var Container */
17
    private $container;
18
19
    /** @var array<string, array<int|string, LazyInteterface>> */
20
    private $lazyCollection = [];
21
22
    /** @var string */
23
    private $interface;
24
25
    /** @var ?string  */
26
    private $key;
27
28
    private function __construct(AbstractModule $module, string $interface)
29
    {
30
        $this->container = $module->getContainer();
31
        $this->interface = $interface;
32
    }
33
34
    public static function newInstance(AbstractModule $module, string $interface): self
35
    {
36
        return new self($module, $interface);
37
    }
38
39
    public function addBinding(?string $key = null): self
40
    {
41
        $this->key = $key;
42
43
        return $this;
44
    }
45
46
    public function setBinding(?string $key = null): self
47
    {
48
        unset($this->lazyCollection[$this->interface]);
49
        $this->key = $key;
50
51
        return $this;
52
    }
53
54
    /**
55
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
56
     */
57
    public function to(string $class): void
58
    {
59
        $this->bind(new LazyTo($class), $this->key);
60
        $this->register();
61
    }
62
63
    /**
64
     * @param class-string<ProviderInterface> $provider
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<ProviderInterface> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<ProviderInterface>.
Loading history...
65
     */
66
    public function toProvider(string $provider): void
67
    {
68
        $this->bind(new LazyProvider($provider), $this->key);
69
        $this->register();
70
    }
71
72
    /**
73
     * @param mixed $instance
74
     */
75
    public function toInstance($instance): void
76
    {
77
        $this->bind(new LazyInstance($instance), $this->key);
78
        $this->register();
79
    }
80
81
    public function register(): void
82
    {
83
        try {
84
            $lazyCollection = $this->container->getInstance(LazyCollection::class);
85
            $this->lazyCollection += $lazyCollection->getArrayCopy();
86
        } catch (Unbound $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
87
        }
88
89
        $lazyCollection = new LazyCollection($this->lazyCollection);
90
        $bind = (new Bind($this->container, LazyCollection::class))->toInstance($lazyCollection);
91
        $this->container->add($bind);
92
    }
93
94
    /**
95
     * @param class-string $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
96
     */
97
    private function bind(LazyInteterface $lazy, ?string $key): void
98
    {
99
        if ($key === null) {
100
            $this->lazyCollection[$this->interface][] = $lazy;
101
102
            return;
103
        }
104
105
        $this->lazyCollection[$this->interface][$key] = $lazy;
106
    }
107
}
108