Passed
Branch master (117216)
by Pavel
08:06
created

AbstractCompiler::compile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 13
nc 2
nop 3
1
<?php
2
3
namespace Bankiru\Seo\Destination\Compiler;
4
5
use Bankiru\Seo\CompiledLink;
6
use Bankiru\Seo\Context\ContextExtractor;
7
use Bankiru\Seo\Destination\CompilableLinkInterface;
8
use Bankiru\Seo\Destination\DestinationCompiler;
9
use Bankiru\Seo\Destination\DestinationNormalizer;
10
use Bankiru\Seo\DestinationInterface;
11
use Bankiru\Seo\Exception\DestinationException;
12
13
abstract class AbstractCompiler implements DestinationCompiler
14
{
15
    /** @var ContextExtractor */
16
    private $extractor;
17
    /** @var \Twig_Environment */
18
    private $twig;
19
    /** @var DestinationNormalizer */
20
    private $normalizer;
21
22
    /**
23
     * AbstractCompiler constructor.
24
     *
25
     * @param DestinationNormalizer $normalizer
26
     */
27
    public function __construct(DestinationNormalizer $normalizer)
28
    {
29
        $this->extractor  = new ContextExtractor();
30
        $this->twig       = new \Twig_Environment(new \Twig_Loader_Array([]));
31
        $this->normalizer = $normalizer;
32
    }
33
34
    /** {@inheritdoc} */
35
    final public function compile(DestinationInterface $destination, $title = null, array $attributes = [])
36
    {
37
        $href  = $this->getHrefTemplate($destination->getRoute());
38
        $items = array_map([$this, 'normalize'], iterator_to_array($destination));
39
40
        try {
41
            return new CompiledLink(
42
                $href->compile($items),
43
                $this->twig->createTemplate($title)->render($this->extractor->extractContext($destination)),
44
                $attributes
45
            );
46
        } catch (\Twig_Error $error) {
0 ignored issues
show
Bug introduced by
The class Twig_Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
47
            throw new DestinationException(
48
                'Error rendering destination title: ' . $error->getMessage(),
49
                $error->getCode(),
50
                $error
51
            );
52
        }
53
    }
54
55
    /**
56
     * Allows replace twig via DI
57
     *
58
     * @param \Twig_Environment $twig
59
     */
60
    public function setTwig(\Twig_Environment $twig)
61
    {
62
        $this->twig = $twig;
63
    }
64
65
    /**
66
     * @param string $route
67
     *
68
     * @return CompilableLinkInterface
69
     */
70
    abstract protected function getHrefTemplate($route);
71
72
    private function normalize($item)
73
    {
74
        if (!$this->normalizer->supports($item)) {
75
            throw DestinationException::normalizationFailed($item);
76
        }
77
78
        return $this->normalizer->normalize($item);
79
    }
80
}
81