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

plNodeLabelBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 42
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A removeNewLinesFrom() 0 3 1
A __construct() 0 4 1
A labelForClass() 0 5 1
A labelForInterface() 0 5 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
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