Completed
Push — master ( b7052b...e758c6 )
by Luis
05:32 queued 02:56
created

NodeLabelBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 42
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A labelForClass() 0 5 1
A labelForInterface() 0 5 1
A removeNewLinesFrom() 0 3 1
A buildLabel() 0 6 2
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\Graphviz;
9
10
use PhUml\Code\ClassDefinition;
11
use PhUml\Code\InterfaceDefinition;
12
use Twig_Environment as TemplateEngine;
13
use Twig_Error_Loader as LoaderError;
14
use Twig_Error_Runtime as RuntimeError;
15
use Twig_Error_Syntax as SyntaxError;
16
17
class NodeLabelBuilder
18
{
19
    /** @var TemplateEngine */
20
    private $engine;
21
22
    /** @var HtmlLabelStyle */
23
    private $style;
24
25 18
    public function __construct(TemplateEngine $engine, HtmlLabelStyle $style)
26
    {
27 18
        $this->engine = $engine;
28 18
        $this->style = $style;
29 18
    }
30
31 12
    public function labelForClass(ClassDefinition $class): string
32
    {
33 12
        return $this->buildLabel('class.html.twig', [
34 12
            'class' => $class,
35 12
            'style' => $this->style,
36
        ]);
37
    }
38
39 6
    public function labelForInterface(InterfaceDefinition $interface): string
40
    {
41 6
        return $this->buildLabel('interface.html.twig', [
42 6
            'interface' => $interface,
43 6
            'style' => $this->style,
44
        ]);
45
    }
46
47 18
    private function buildLabel(string $template, array $options): string
48
    {
49
        try {
50 18
            return "<{$this->removeNewLinesFrom($this->engine->render($template, $options))}>";
51 3
        } catch (LoaderError | RuntimeError | SyntaxError $e) {
52 3
            throw new NodeLabelError($e);
53
        }
54
    }
55
56 15
    private function removeNewLinesFrom(string $label): string
57
    {
58 15
        return trim(preg_replace('/\s\s+|\n/', '', $label));
59
    }
60
}
61