Passed
Push — cleanup ( 1df3c6 )
by Luis
14:39
created

NodeLabelBuilder::forInterface()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
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\Graphviz\Builders;
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
    public function __construct(TemplateEngine $engine, HtmlLabelStyle $style)
26
    {
27
        $this->engine = $engine;
28
        $this->style = $style;
29
    }
30
31
    public function forClass(ClassDefinition $class): string
32
    {
33
        return $this->buildLabel('class.html.twig', [
34
            'class' => $class,
35
            'style' => $this->style,
36
        ]);
37
    }
38
39
    public function forInterface(InterfaceDefinition $interface): string
40
    {
41
        return $this->buildLabel('interface.html.twig', [
42
            'interface' => $interface,
43
            'style' => $this->style,
44
        ]);
45
    }
46
47
    private function buildLabel(string $template, array $options): string
48
    {
49
        try {
50
            return "<{$this->removeNewLinesFrom($this->engine->render($template, $options))}>";
51
        } catch (LoaderError | RuntimeError | SyntaxError $e) {
52
            throw new NodeLabelError($e);
53
        }
54
    }
55
56
    private function removeNewLinesFrom(string $label): string
57
    {
58
        return trim(preg_replace('/\s\s+|\n/', '', $label));
59
    }
60
}
61