Completed
Pull Request — master (#358)
by Christian
01:29
created

SeoExtension   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 3
dl 0
loc 147
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getFunctions() 0 12 1
A __construct() 0 5 1
A getName() 0 4 1
A getTitle() 0 4 1
A getMetadatas() 0 24 4
A getHtmlAttributes() 0 9 2
A getHeadAttributes() 0 9 2
A getLinkCanonical() 0 6 2
A getLangAlternates() 0 9 2
A getOembedLinks() 0 9 2
A normalize() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\SeoBundle\Twig\Extension;
15
16
use Sonata\SeoBundle\Seo\SeoPageInterface;
17
use Twig\Extension\AbstractExtension;
18
use Twig\TwigFunction;
19
20
final class SeoExtension extends AbstractExtension
21
{
22
    /**
23
     * @var SeoPageInterface
24
     */
25
    protected $page;
26
27
    /**
28
     * @var string
29
     */
30
    protected $encoding;
31
32
    /**
33
     * @param string $encoding
34
     */
35
    public function __construct(SeoPageInterface $page, $encoding)
36
    {
37
        $this->page = $page;
38
        $this->encoding = $encoding;
39
    }
40
41
    public function getFunctions()
42
    {
43
        return [
44
            new TwigFunction('sonata_seo_title', [$this, 'getTitle'], ['is_safe' => ['html']]),
45
            new TwigFunction('sonata_seo_metadatas', [$this, 'getMetadatas'], ['is_safe' => ['html']]),
46
            new TwigFunction('sonata_seo_html_attributes', [$this, 'getHtmlAttributes'], ['is_safe' => ['html']]),
47
            new TwigFunction('sonata_seo_head_attributes', [$this, 'getHeadAttributes'], ['is_safe' => ['html']]),
48
            new TwigFunction('sonata_seo_link_canonical', [$this, 'getLinkCanonical'], ['is_safe' => ['html']]),
49
            new TwigFunction('sonata_seo_lang_alternates', [$this, 'getLangAlternates'], ['is_safe' => ['html']]),
50
            new TwigFunction('sonata_seo_oembed_links', [$this, 'getOembedLinks'], ['is_safe' => ['html']]),
51
        ];
52
    }
53
54
    public function getName()
55
    {
56
        return 'sonata_seo';
57
    }
58
59
    /**
60
     * @return string
61
     */
62
    public function getTitle()
63
    {
64
        return sprintf('<title>%s</title>', strip_tags($this->page->getTitle()));
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getMetadatas()
71
    {
72
        $html = '';
73
        foreach ($this->page->getMetas() as $type => $metas) {
74
            foreach ((array) $metas as $name => $meta) {
75
                list($content, $extras) = $meta;
0 ignored issues
show
Unused Code introduced by
The assignment to $extras is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
76
77
                if (!empty($content)) {
78
                    $html .= sprintf("<meta %s=\"%s\" content=\"%s\" />\n",
79
                        $type,
80
                        $this->normalize($name),
81
                        $this->normalize($content)
82
                    );
83
                } else {
84
                    $html .= sprintf("<meta %s=\"%s\" />\n",
85
                        $type,
86
                        $this->normalize($name)
87
                    );
88
                }
89
            }
90
        }
91
92
        return $html;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public function getHtmlAttributes()
99
    {
100
        $attributes = '';
101
        foreach ($this->page->getHtmlAttributes() as $name => $value) {
102
            $attributes .= sprintf('%s="%s" ', $name, $value);
103
        }
104
105
        return rtrim($attributes);
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    public function getHeadAttributes()
112
    {
113
        $attributes = '';
114
        foreach ($this->page->getHeadAttributes() as $name => $value) {
115
            $attributes .= sprintf('%s="%s" ', $name, $value);
116
        }
117
118
        return rtrim($attributes);
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getLinkCanonical()
125
    {
126
        if ($this->page->getLinkCanonical()) {
127
            return sprintf("<link rel=\"canonical\" href=\"%s\"/>\n", $this->page->getLinkCanonical());
128
        }
129
    }
130
131
    /**
132
     * @return string
133
     */
134
    public function getLangAlternates()
135
    {
136
        $html = '';
137
        foreach ($this->page->getLangAlternates() as $href => $hrefLang) {
138
            $html .= sprintf("<link rel=\"alternate\" href=\"%s\" hreflang=\"%s\"/>\n", $href, $hrefLang);
139
        }
140
141
        return $html;
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function getOembedLinks()
148
    {
149
        $html = '';
150
        foreach ($this->page->getOEmbedLinks() as $title => $link) {
151
            $html .= sprintf("<link rel=\"alternate\" type=\"application/json+oembed\" href=\"%s\" title=\"%s\" />\n", $link, $title);
152
        }
153
154
        return $html;
155
    }
156
157
    /**
158
     * @param string $string
159
     *
160
     * @return mixed
161
     */
162
    private function normalize($string)
163
    {
164
        return htmlentities(strip_tags((string) $string), ENT_COMPAT, $this->encoding);
165
    }
166
}
167