Test Failed
Push — graphviz-refactoring ( 3f1b14 )
by Luis
02:18
created

plNodeLabelBuilder::labelForClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
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
use Twig_Environment as TemplateEngine;
9
use Twig_Error_Loader as LoaderError;
10
use Twig_Error_Runtime as RuntimeError;
11
use Twig_Error_Syntax as SyntaxError;
12
13
class plNodeLabelBuilder
14
{
15
    /** @var TemplateEngine */
16
    private $engine;
17
18
    /** @var plGraphvizProcessorStyle */
19
    private $style;
20
21
    public function __construct(TemplateEngine $engine, plGraphvizProcessorStyle $style)
22
    {
23
        $this->engine = $engine;
24
        $this->style = $style;
25
    }
26
27
    public function labelForClass(plPhpClass $class): string
28
    {
29
        return $this->buildLabel('class.html.twig', [
30
            'class' => $class,
31
            'style' => $this->style,
32
        ]);
33
    }
34
35
    public function labelForInterface(plPhpInterface $interface): string
36
    {
37
        return $this->buildLabel('interface.html.twig', [
38
            'interface' => $interface,
39
            'style' => $this->style,
40
        ]);
41
    }
42
43
    private function buildLabel(string $template, array $options): string
44
    {
45
        try {
46
            return "<{$this->removeNewLinesFrom($this->engine->render($template, $options))}>";
47
        } catch (LoaderError | RuntimeError | SyntaxError $e) {
48
            throw new plNodeLabelError($e);
49
        }
50
    }
51
52
    private function removeNewLinesFrom(string $label): string
53
    {
54
        return trim(preg_replace('/\s\s+|\n/', '', $label));
55
    }
56
}
57