Completed
Pull Request — 1.x (#352)
by Akihito
01:26
created

CompileDependencies   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 2
dl 0
loc 26
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 15 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Compiler;
6
7
use Ray\Di\AbstractModule;
8
9
use function array_keys;
10
use function assert;
11
use function is_int;
12
use function sort;
13
use function strpos;
14
use function substr;
15
16
final class CompileDependencies
17
{
18
    /** @var NewInstance */
19
    private $newInstance;
20
21
    public function __construct(NewInstance $newInstance)
22
    {
23
        $this->newInstance = $newInstance;
24
    }
25
26
    public function __invoke(AbstractModule $module): AbstractModule
27
    {
28
        $container = $module->getContainer()->getContainer();
29
        $dependencies = array_keys($container);
30
        sort($dependencies);
31
        foreach ($dependencies as $dependencyIndex) {
32
            $pos = strpos((string) $dependencyIndex, '-');
33
            assert(is_int($pos));
34
            $interface = substr((string) $dependencyIndex, 0, $pos);
35
            $name = substr((string) $dependencyIndex, $pos + 1);
36
            ($this->newInstance)($interface, $name);
37
        }
38
39
        return $module;
40
    }
41
}
42