GraphWriter::drawGraphCycle()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
c 0
b 0
f 0
rs 8.439
cc 5
eloc 18
nc 3
nop 1
1
<?php
2
/**
3
 * @copyright 2018 Aleksander Stelmaczonek <[email protected]>
4
 * @license   MIT License, see license file distributed with this source code
5
 */
6
7
namespace Koriit\PHPDeps\Console;
8
9
use RuntimeException;
10
use Symfony\Component\Console\Output\OutputInterface;
11
12
class GraphWriter
13
{
14
    /** @var OutputInterface */
15
    private $output;
16
17
    public function __construct(OutputInterface $output)
18
    {
19
        $this->output = $output;
20
    }
21
22
    /**
23
     * @param string[] $nodes
24
     */
25
    public function drawGraphCycle(array $nodes)
26
    {
27
        if (\count($nodes) < 2) {
28
            throw new RuntimeException('Not enough nodes to draw a cycle');
29
        }
30
31
        $firstNode = \array_shift($nodes);
32
        $lastNode = \array_pop($nodes);
33
34
        $this->drawNode($firstNode);
35
36
        if (!empty($nodes)) {
37
            $this->drawLine('↑↘');
38
            $i = 0;
39
            foreach ($nodes as $node) {
40
                if ($i++) {
41
                    $this->drawLine('↑ ↓');
42
                }
43
                $this->drawLine('↑', false);
44
                $this->drawNode($node);
45
            }
46
            $this->drawLine('↑↙');
47
        } else {
48
            $this->drawLine('↕');
49
        }
50
51
        $this->drawNode($lastNode);
52
    }
53
54
    /**
55
     * @param string $node
56
     */
57
    private function drawNode($node)
58
    {
59
        $this->output->writeln('<fg=red>*</> ' . $node);
60
    }
61
62
    /**
63
     * @param string $line
64
     * @param bool   $newLine
65
     */
66
    private function drawLine($line, $newLine = true)
67
    {
68
        $formatted = '<fg=yellow>' . $line . '</>';
69
70
        if ($newLine) {
71
            $this->output->writeln($formatted);
72
        } else {
73
            // Draw additional space as we are not going to new line
74
            $this->output->write($formatted . ' ');
75
        }
76
    }
77
}
78