Completed
Push — master ( 389baa...c7c5a4 )
by Luis
14:17 queued 08:06
created

GraphvizProcessor::extractElements()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 3
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.1
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Processors;
9
10
use PhUml\Code\ClassDefinition;
11
use PhUml\Code\Definition;
12
use PhUml\Code\InterfaceDefinition;
13
use PhUml\Code\Codebase;
14
use PhUml\Graphviz\Builders\ClassGraphBuilder;
15
use PhUml\Graphviz\Builders\InterfaceGraphBuilder;
16
use PhUml\Graphviz\Digraph;
17
use PhUml\Graphviz\DigraphPrinter;
18
19
/**
20
 * It creates a digraph from a `Structure` and returns it as a string in DOT format
21
 */
22
class GraphvizProcessor extends Processor
23
{
24
    /** @var ClassGraphBuilder */
25
    private $classBuilder;
26
27
    /** @var InterfaceGraphBuilder */
28
    private $interfaceBuilder;
29
30
    /** @var DigraphPrinter */
31
    private $printer;
32
33 72
    public function __construct(
34
        ClassGraphBuilder $classBuilder = null,
35
        InterfaceGraphBuilder $interfaceBuilder = null,
36
        DigraphPrinter $printer = null
37
    ) {
38 72
        $this->classBuilder = $classBuilder ?? new ClassGraphBuilder();
39 72
        $this->interfaceBuilder = $interfaceBuilder ?? new InterfaceGraphBuilder();
40 72
        $this->printer = $printer ?? new DigraphPrinter();
41 72
    }
42
43 30
    public function name(): string
44
    {
45 30
        return 'Graphviz';
46
    }
47
48 57
    public function process(Codebase $codebase): string
49
    {
50 57
        $digraph = new Digraph();
51 57
        foreach ($codebase->definitions() as $definition) {
52 54
            $this->extractElements($definition, $codebase, $digraph);
53
        }
54 57
        return $this->printer->toDot($digraph);
55
    }
56
57 54
    protected function extractElements(
58
        Definition $definition,
59
        Codebase $codebase,
60
        Digraph $digraph
61
    ): void {
62 54
        if ($definition instanceof ClassDefinition) {
63 54
            $digraph->add($this->classBuilder->extractFrom($definition, $codebase));
64 36
        } elseif ($definition instanceof InterfaceDefinition) {
65 36
            $digraph->add($this->interfaceBuilder->extractFrom($definition));
66
        }
67 54
    }
68
}
69