Passed
Push — master ( 1416ae...b5a8b1 )
by Luis
54s queued 13s
created

GraphvizProcessor::extractElements()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 3
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 4
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 8.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\Codebase;
12
use PhUml\Code\Definition;
13
use PhUml\Code\InterfaceDefinition;
14
use PhUml\Code\TraitDefinition;
15
use PhUml\Graphviz\Builders\ClassGraphBuilder;
16
use PhUml\Graphviz\Builders\InterfaceGraphBuilder;
17
use PhUml\Graphviz\Builders\TraitGraphBuilder;
18
use PhUml\Graphviz\Digraph;
19
use PhUml\Graphviz\DigraphPrinter;
20
use PhUml\Templates\TemplateEngine;
21
22
/**
23
 * It creates a digraph from a `Structure` and returns it as a string in DOT format
24
 */
25
final class GraphvizProcessor implements Processor
26
{
27 18
    public static function fromConfiguration(GraphvizConfiguration $configuration): GraphvizProcessor
28
    {
29 18
        $style = $configuration->digraphStyle();
30 18
        $associationsBuilder = $configuration->associationsBuilder();
31
32 18
        return new GraphvizProcessor(
33 18
            new ClassGraphBuilder($associationsBuilder),
34 18
            new InterfaceGraphBuilder(),
35 18
            new TraitGraphBuilder(),
36 18
            new DigraphPrinter(new TemplateEngine(), $style)
37
        );
38
    }
39
40 18
    private function __construct(
41
        private readonly ClassGraphBuilder $classBuilder,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting T_VARIABLE on line 41 at column 25
Loading history...
42
        private readonly InterfaceGraphBuilder $interfaceBuilder,
43
        private readonly TraitGraphBuilder $traitBuilder,
44
        private readonly DigraphPrinter $printer
45
    ) {
46
    }
47
48 11
    public function name(): string
49
    {
50 11
        return 'Graphviz';
51
    }
52
53 13
    public function process(Codebase $codebase): OutputContent
54
    {
55 13
        $digraph = new Digraph();
56 13
        foreach ($codebase->definitions() as $definition) {
57 11
            $this->extractElements($definition, $codebase, $digraph);
58
        }
59 13
        return new OutputContent($this->printer->toDot($digraph));
60
    }
61
62 11
    private function extractElements(
63
        Definition $definition,
64
        Codebase $codebase,
65
        Digraph $digraph
66
    ): void {
67 11
        if ($definition instanceof ClassDefinition) {
68 11
            $digraph->add($this->classBuilder->extractFrom($definition, $codebase));
69 8
        } elseif ($definition instanceof InterfaceDefinition) {
70 8
            $digraph->add($this->interfaceBuilder->extractFrom($definition, $codebase));
71 8
        } elseif ($definition instanceof TraitDefinition) {
72 8
            $digraph->add($this->traitBuilder->extractFrom($definition, $codebase));
73
        }
74
    }
75
}
76