Passed
Pull Request — 2.x (#264)
by Akihito
10:34 queued 13s
created

MultiBinder::to()   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\MultiBinding\LazyInstance;
8
use Ray\Di\MultiBinding\LazyInteterface;
9
use Ray\Di\MultiBinding\LazyProvider;
10
use Ray\Di\MultiBinding\LazyTo;
11
use Ray\Di\MultiBinding\MultiBindings;
12
13
final class MultiBinder
14
{
15
    /** @var Container */
16
    private $container;
17
18
    /** @var MultiBindings */
19
    private $multiBindings;
20
21
    /** @var string */
22
    private $interface;
23
24
    /** @var ?string  */
25
    private $key;
26
27
    private function __construct(AbstractModule $module, string $interface)
28
    {
29
        $this->container = $module->getContainer();
30
        $this->multiBindings = $this->container->multiBindings;
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
        $this->container->multiBindings->exchangeArray([]);
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
        $bind = (new Bind($this->container, MultiBindings::class))->toInstance($this->multiBindings);
84
        $this->container->add($bind);
85
    }
86
87
    private function bind(LazyInteterface $lazy, ?string $key): void
88
    {
89
        $bindings = [];
90
        if ($this->multiBindings->offsetExists($this->interface)) {
91
            $bindings = $this->multiBindings->offsetGet($this->interface);
92
        }
93
94
        if ($key === null) {
95
            $bindings[] = $lazy;
96
            $this->multiBindings->offsetSet($this->interface, $bindings); // @phpstan-ignore-line
97
98
            return;
99
        }
100
101
        $bindings[$key] = $lazy;
102
        $this->multiBindings->offsetSet($this->interface, $bindings);
103
    }
104
}
105