Passed
Push — dependabot/npm_and_yarn/npm-8.... ( a37e8a )
by
unknown
10:51
created

GraphvizProcessor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 48
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A process() 0 7 2
A fromConfiguration() 0 10 1
A extractElements() 0 11 4
A __construct() 0 6 1
1
<?php declare(strict_types=1);
2
/**
3
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
4
 */
5
6
namespace PhUml\Processors;
7
8
use PhUml\Code\ClassDefinition;
9
use PhUml\Code\Codebase;
10
use PhUml\Code\Definition;
11
use PhUml\Code\InterfaceDefinition;
12
use PhUml\Code\TraitDefinition;
13
use PhUml\Graphviz\Builders\ClassGraphBuilder;
14
use PhUml\Graphviz\Builders\InterfaceGraphBuilder;
15
use PhUml\Graphviz\Builders\TraitGraphBuilder;
16
use PhUml\Graphviz\Digraph;
17
use PhUml\Graphviz\DigraphPrinter;
18
use PhUml\Templates\TemplateEngine;
19
20
/**
21
 * It creates a digraph from a `Structure` and returns it as a string in DOT format
22
 */
23
final class GraphvizProcessor implements Processor
24
{
25 18
    public static function fromConfiguration(GraphvizConfiguration $configuration): GraphvizProcessor
26
    {
27 18
        $style = $configuration->digraphStyle();
28 18
        $associationsBuilder = $configuration->associationsBuilder();
29
30 18
        return new GraphvizProcessor(
31 18
            new ClassGraphBuilder($associationsBuilder),
32 18
            new InterfaceGraphBuilder(),
33 18
            new TraitGraphBuilder(),
34 18
            new DigraphPrinter(new TemplateEngine(), $style)
35
        );
36
    }
37
38 18
    private function __construct(
39
        private readonly ClassGraphBuilder $classBuilder,
40
        private readonly InterfaceGraphBuilder $interfaceBuilder,
41
        private readonly TraitGraphBuilder $traitBuilder,
42
        private readonly DigraphPrinter $printer
43
    ) {
44
    }
45
46 11
    public function name(): string
47
    {
48 11
        return 'Graphviz';
49
    }
50
51 13
    public function process(Codebase $codebase): OutputContent
52
    {
53 13
        $digraph = new Digraph();
54 13
        foreach ($codebase->definitions() as $definition) {
55 11
            $this->extractElements($definition, $codebase, $digraph);
56
        }
57 13
        return new OutputContent($this->printer->toDot($digraph));
58
    }
59
60 11
    private function extractElements(
61
        Definition $definition,
62
        Codebase $codebase,
63
        Digraph $digraph
64
    ): void {
65 11
        if ($definition instanceof ClassDefinition) {
66 11
            $digraph->add($this->classBuilder->extractFrom($definition, $codebase));
67 8
        } elseif ($definition instanceof InterfaceDefinition) {
68 8
            $digraph->add($this->interfaceBuilder->extractFrom($definition, $codebase));
69 8
        } elseif ($definition instanceof TraitDefinition) {
70 8
            $digraph->add($this->traitBuilder->extractFrom($definition, $codebase));
71
        }
72
    }
73
}
74