Completed
Push — master ( 82ed85...e127d4 )
by Pavel
03:01
created

DynamicSeoPageBuilder::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Bankiru\Seo\Page;
4
5
class DynamicSeoPageBuilder implements PageBuilder
6
{
7
    /** @var  array */
8
    protected $context = [];
9
    /** @var \Twig_Environment */
10
    protected $twig;
11
    /** @var  SeoPageBuilder */
12
    protected $builder;
13
14
    /**
15
     * DynamicSeoPageBuilder constructor.
16
     *
17
     * @param \Twig_Environment $twig
18
     * @param SeoPageBuilder    $builder
19
     */
20 2
    public function __construct(\Twig_Environment $twig = null, SeoPageBuilder $builder = null)
21
    {
22 2
        $this->twig    = $twig ?: new \Twig_Environment(new \Twig_Loader_Array([]));
23 2
        $this->builder = $builder ?: new SeoPageBuilder();
24 2
    }
25
26
    /**
27
     * @param array $context
28
     *
29
     * @return $this
30
     */
31 2
    public function setContext(array $context)
32
    {
33 2
        $this->context = $context;
34
35 2
        return $this;
36
    }
37
38
    /** {@inheritdoc} */
39 2
    public function getSeoPage()
40
    {
41 2
        $builder = new SeoPageBuilder();
42 2
        $this->build($builder);
43
44 2
        return $builder->getSeoPage();
45
    }
46
47
    /** {@inheritdoc} */
48 1
    public function setMeta(array $meta)
49
    {
50 1
        $this->builder->setMeta($meta);
51
52 1
        return $this;
53
    }
54
55
    /** {@inheritdoc} */
56 2
    public function setTitle($title)
57
    {
58 2
        $this->builder->setTitle($title);
59
60 2
        return $this;
61
    }
62
63
    /** {@inheritdoc} */
64 1
    public function setCanonicalLink($canonical = null)
65
    {
66 1
        $this->builder->setCanonicalLink($canonical);
67
68 1
        return $this;
69
    }
70
71
    /** {@inheritdoc} */
72
    public function setHtmlAttributes(array $htmlAttributes)
73
    {
74
        $this->builder->setHtmlAttributes($htmlAttributes);
75
76
        return $this;
77
    }
78
79
    /** {@inheritdoc} */
80
    public function setBreadcrumbs(array $breadcrumbs)
81
    {
82
        $this->builder->setBreadcrumbs($breadcrumbs);
83
84
        return $this;
85
    }
86
87 2
    public static function create()
88
    {
89 2
        return new static();
90
    }
91
92
    /**
93
     * @param PageBuilder $builder
94
     */
95 2
    protected function build(PageBuilder $builder)
96
    {
97 2
        $builder->setBreadcrumbs(array_map([$this, 'render'], $this->builder->getBreadcrumbs()));
98 2
        $builder->setCanonicalLink($this->render($this->builder->getCanonicalLink()));
99 2
        $builder->setTitle($this->render($this->builder->getTitle()));
100 2
        $builder->setHtmlAttributes(array_map([$this, 'render'], $this->builder->getHtmlAttributes()));
101 2
        $builder->setMeta(array_map([$this, 'renderMeta'], $this->builder->getMeta()));
102 2
    }
103
104 2
    protected function render($string)
105
    {
106 2
        return $this->twig->createTemplate((string)$string)->render($this->context);
107
    }
108
109 1
    protected function renderMeta(HtmlMetaInterface $meta)
110
    {
111 1
        if (null !== $meta->getName()) {
112 1
            return HtmlMeta::createNameMeta(
113 1
                $meta->getName(),
114 1
                $this->render($meta->getContent()),
115 1
                $meta->getAttributes()
116 1
            );
117
        }
118
119
        return HtmlMeta::createHttpEquivMeta(
120
            $meta->getHttpEquiv(),
121 1
            $this->render($meta->getContent()),
122 1
            $meta->getAttributes()
123 1
        );
124 1
    }
125
}
126