Passed
Push — multi_binding ( 75a5e8 )
by Akihito
02:09
created

MultiBinder   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 5 1
A newInstance() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Di\MultiBinding;
6
7
use Ray\Di\AbstractModule;
8
use Ray\Di\Bind;
9
use Ray\Di\Container;
10
11
final class MultiBinder
12
{
13
    /** @var Container */
14
    private $container;
15
16
    /** @var array<string, array<string, Lazy>> */
17
    private $lazyCollection = [];
18
19
    /** @var string */
20
    private $interface;
21
22
    private function __construct(AbstractModule $module, string $interface)
23
    {
24
        $this->container = $module->getContainer();
25
        $this->interface = $interface;
26
    }
27
28
    public static function newInstance(AbstractModule $module, string $interface): self
29
    {
30
        return new self($module, $interface);
31
    }
32
33
    /** @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...
34
    public function add(string $key, string $class): void
35
    {
36
        $this->lazyCollection[$this->interface][$key] = new Lazy($class);
37
        $bind = (new Bind($this->container, LazyCollection::class))->toInstance(new LazyCollection($this->lazyCollection));
38
        $this->container->add($bind);
39
    }
40
}
41