Completed
Pull Request — 2.x (#266)
by Maximilian
01:31
created

SeoExtension::getStructuredData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\SeoBundle\Twig\Extension;
13
14
use Sonata\SeoBundle\Seo\SeoPageInterface;
15
16
class SeoExtension extends \Twig_Extension
17
{
18
    /**
19
     * @var SeoPageInterface
20
     */
21
    protected $page;
22
23
    /**
24
     * @var string
25
     */
26
    protected $encoding;
27
28
    /**
29
     * @param SeoPageInterface $page
30
     * @param string           $encoding
31
     */
32
    public function __construct(SeoPageInterface $page, $encoding)
33
    {
34
        $this->page = $page;
35
        $this->encoding = $encoding;
36
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41
    public function getFunctions()
42
    {
43
        return [
44
            new \Twig_SimpleFunction('sonata_seo_title', [$this, 'getTitle'], ['is_safe' => ['html']]),
45
            new \Twig_SimpleFunction('sonata_seo_metadatas', [$this, 'getMetadatas'], ['is_safe' => ['html']]),
46
            new \Twig_SimpleFunction('sonata_seo_html_attributes', [$this, 'getHtmlAttributes'], ['is_safe' => ['html']]),
47
            new \Twig_SimpleFunction('sonata_seo_head_attributes', [$this, 'getHeadAttributes'], ['is_safe' => ['html']]),
48
            new \Twig_SimpleFunction('sonata_seo_link_canonical', [$this, 'getLinkCanonical'], ['is_safe' => ['html']]),
49
            new \Twig_SimpleFunction('sonata_seo_lang_alternates', [$this, 'getLangAlternates'], ['is_safe' => ['html']]),
50
            new \Twig_SimpleFunction('sonata_seo_oembed_links', [$this, 'getOembedLinks'], ['is_safe' => ['html']]),
51
            new \Twig_SimpleFunction('sonata_seo_structured_data', [$this, 'getStructuredData'], ['is_safe' => ['html']]),
52
        ];
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getName()
59
    {
60
        return 'sonata_seo';
61
    }
62
63
    /**
64
     * NEXT_MAJOR: remove this method.
65
     *
66
     * @deprecated since 2.0, to be removed in 3.0
67
     */
68
    public function renderTitle()
69
    {
70
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
71
            'The '.__METHOD__.' method is deprecated since 2.0, to be removed in 3.0. '.
72
            'Use '.__NAMESPACE__.'::getTitle() instead.',
73
            E_USER_DEPRECATED
74
        );
75
76
        echo $this->getTitle();
77
    }
78
79
    /**
80
     * @return string
81
     */
82
    public function getTitle()
83
    {
84
        return sprintf('<title>%s</title>', strip_tags($this->page->getTitle()));
85
    }
86
87
    /**
88
     * NEXT_MAJOR: remove this method.
89
     *
90
     * @deprecated since 2.0, to be removed in 3.0
91
     */
92
    public function renderMetadatas()
93
    {
94
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
95
            'The '.__METHOD__.' method is deprecated since 2.0, to be removed in 3.0. '.
96
            'Use '.__NAMESPACE__.'::getMetadatas() instead.',
97
            E_USER_DEPRECATED
98
        );
99
100
        echo $this->getMetadatas();
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getMetadatas()
107
    {
108
        $html = '';
109
        foreach ($this->page->getMetas() as $type => $metas) {
110
            foreach ((array) $metas as $name => $meta) {
111
                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...
112
113
                if (!empty($content)) {
114
                    $html .= sprintf("<meta %s=\"%s\" content=\"%s\" />\n",
115
                        $type,
116
                        $this->normalize($name),
117
                        $this->normalize($content)
118
                    );
119
                } else {
120
                    $html .= sprintf("<meta %s=\"%s\" />\n",
121
                        $type,
122
                        $this->normalize($name)
123
                    );
124
                }
125
            }
126
        }
127
128
        return $html;
129
    }
130
131
    /**
132
     * NEXT_MAJOR: remove this method.
133
     *
134
     * @deprecated since 2.0, to be removed in 3.0
135
     */
136
    public function renderHtmlAttributes()
137
    {
138
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
139
            'The '.__METHOD__.' method is deprecated since 2.0, to be removed in 3.0. '.
140
            'Use '.__NAMESPACE__.'::getHtmlAttributes() instead.',
141
            E_USER_DEPRECATED
142
        );
143
144
        echo $this->getHtmlAttributes();
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getHtmlAttributes()
151
    {
152
        $attributes = '';
153
        foreach ($this->page->getHtmlAttributes() as $name => $value) {
154
            $attributes .= sprintf('%s="%s" ', $name, $value);
155
        }
156
157
        return rtrim($attributes);
158
    }
159
160
    /**
161
     * NEXT_MAJOR: remove this method.
162
     *
163
     * @deprecated since 2.0, to be removed in 3.0
164
     */
165
    public function renderHeadAttributes()
166
    {
167
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
168
            'The '.__METHOD__.' method is deprecated since 2.0, to be removed in 3.0. '.
169
            'Use '.__NAMESPACE__.'::getHeadAttributes() instead.',
170
            E_USER_DEPRECATED
171
        );
172
173
        echo $this->getHeadAttributes();
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getHeadAttributes()
180
    {
181
        $attributes = '';
182
        foreach ($this->page->getHeadAttributes() as $name => $value) {
183
            $attributes .= sprintf('%s="%s" ', $name, $value);
184
        }
185
186
        return rtrim($attributes);
187
    }
188
189
    /**
190
     * NEXT_MAJOR: remove this method.
191
     *
192
     * @deprecated since 2.0, to be removed in 3.0
193
     */
194
    public function renderLinkCanonical()
195
    {
196
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
197
            'The '.__METHOD__.' method is deprecated since 2.0, to be removed in 3.0. '.
198
            'Use '.__NAMESPACE__.'::getLinkCanonical() instead.',
199
            E_USER_DEPRECATED
200
        );
201
202
        echo $this->getLinkCanonical();
203
    }
204
205
    /**
206
     * @return string
207
     */
208
    public function getLinkCanonical()
209
    {
210
        if ($this->page->getLinkCanonical()) {
211
            return sprintf("<link rel=\"canonical\" href=\"%s\"/>\n", $this->page->getLinkCanonical());
212
        }
213
    }
214
215
    /**
216
     * NEXT_MAJOR: remove this method.
217
     *
218
     * @deprecated since 2.0, to be removed in 3.0
219
     */
220
    public function renderLangAlternates()
221
    {
222
        @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
223
            'The '.__METHOD__.' method is deprecated since 2.0, to be removed in 3.0. '.
224
            'Use '.__NAMESPACE__.'::getLangAlternates() instead.',
225
            E_USER_DEPRECATED
226
        );
227
228
        echo $this->getLangAlternates();
229
    }
230
231
    /**
232
     * @return string
233
     */
234
    public function getLangAlternates()
235
    {
236
        $html = '';
237
        foreach ($this->page->getLangAlternates() as $href => $hrefLang) {
238
            $html .= sprintf("<link rel=\"alternate\" href=\"%s\" hreflang=\"%s\"/>\n", $href, $hrefLang);
239
        }
240
241
        return $html;
242
    }
243
244
    /**
245
     * @return string
246
     */
247
    public function getOembedLinks()
248
    {
249
        $html = '';
250
        foreach ($this->page->getOEmbedLinks() as $title => $link) {
251
            $html .= sprintf("<link rel=\"alternate\" type=\"application/json+oembed\" href=\"%s\" title=\"%s\" />\n", $link, $title);
252
        }
253
254
        return $html;
255
    }
256
257
    /**
258
     * Creates a script tag with type 'json-ld' and the JSON-LD string stored in page object.
259
     *
260
     * @return string
261
     */
262
    public function getStructuredData()
263
    {
264
        if (empty($this->page->getStructuredData())) {
265
            return '';
266
        }
267
268
        return sprintf("<script type=\"application/ld+json\">%s</script>\n", $this->page->getStructuredData());
269
    }
270
271
    /**
272
     * @param string $string
273
     *
274
     * @return mixed
275
     */
276
    private function normalize($string)
277
    {
278
        return htmlentities(strip_tags($string), ENT_COMPAT, $this->encoding);
279
    }
280
}
281