Completed
Pull Request — 2.x (#216)
by Akihito
09:35
created

GraphDumper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 48
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __invoke() 0 10 3
A write() 0 18 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Ray\Compiler;
6
7
use Koriym\Printo\Printo;
8
use Ray\Di\Container;
9
use Ray\Di\Name;
10
11
final class GraphDumper
12
{
13
    /**
14
     * @var Container
15
     */
16
    private $container;
17
18
    /**
19
     * @var string
20
     */
21
    private $scriptDir;
22
23
    public function __construct(Container $container, string $scriptDir)
24
    {
25
        $this->container = $container;
26
        $this->scriptDir = $scriptDir;
27
    }
28
29
    public function __invoke() : void
30
    {
31
        $container = $this->container->getContainer();
32
        foreach ($container as $dependencyIndex => $dependency) {
33
            $isNotInjector = $dependencyIndex !== 'Ray\Di\InjectorInterface-' . Name::ANY;
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $dependencyIndex (integer) and 'Ray\\Di\\InjectorInterface-' . \Ray\Di\Name::ANY (string) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
34
            if ($isNotInjector) {
35
                $this->write($dependencyIndex);
36
            }
37
        }
38
    }
39
40
    private function write(string $dependencyIndex) : void
41
    {
42
        if ($dependencyIndex === 'Ray\Aop\MethodInvocation-') {
43
            return;
44
        }
45
        list($interface, $name) = \explode('-', $dependencyIndex);
46
        $instance = (new ScriptInjector($this->scriptDir))->getInstance($interface, $name);
47
        $graph = (string) (new Printo($instance))
48
            ->setRange(Printo::RANGE_ALL)
49
            ->setLinkDistance(130)
50
            ->setCharge(-500);
51
        $graphDir = $this->scriptDir . '/graph/';
52
        if (! \file_exists($graphDir)) {
53
            \mkdir($graphDir);
54
        }
55
        $file = $graphDir . \str_replace('\\', '_', $dependencyIndex) . '.html';
56
        \file_put_contents($file, $graph, LOCK_EX);
57
    }
58
}
59