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

SeoPage   C

Complexity

Total Complexity 38

Size/Duplication

Total Lines 409
Duplicated Lines 0 %

Coupling/Cohesion

Components 7
Dependencies 0

Importance

Changes 0
Metric Value
wmc 38
lcom 7
cbo 0
dl 0
loc 409
rs 6.9999
c 0
b 0
f 0

33 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 1
A setTitle() 0 6 1
A addTitle() 0 6 1
A getTitle() 0 4 1
A getMetas() 0 4 1
A addMeta() 0 10 2
A hasMeta() 0 4 1
A removeMeta() 0 6 1
A setMetas() 0 18 4
A setHtmlAttributes() 0 6 1
A addHtmlAttributes() 0 6 1
A removeHtmlAttributes() 0 6 1
A getHtmlAttributes() 0 4 1
A hasHtmlAttribute() 0 4 1
A setHeadAttributes() 0 6 1
A addHeadAttribute() 0 6 1
A removeHeadAttribute() 0 6 1
A getHeadAttributes() 0 4 1
A hasHeadAttribute() 0 4 1
A setLinkCanonical() 0 6 1
A getLinkCanonical() 0 4 1
A removeLinkCanonical() 0 4 1
A setSeparator() 0 6 1
A setLangAlternates() 0 6 1
A addLangAlternate() 0 6 1
A removeLangAlternate() 0 6 1
A hasLangAlternate() 0 4 1
A getLangAlternates() 0 4 1
A addOEmbedLink() 0 6 1
A getOEmbedLinks() 0 4 1
A getStructuredData() 0 4 1
A setStructuredData() 0 4 1
A normalize() 0 8 2
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\Seo;
13
14
/**
15
 * http://en.wikipedia.org/wiki/Meta_element.
16
 */
17
class SeoPage implements SeoPageInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $title;
23
24
    /**
25
     * @var array
26
     */
27
    protected $metas;
28
29
    /**
30
     * @var array
31
     */
32
    protected $htmlAttributes;
33
34
    /**
35
     * @var string
36
     */
37
    protected $linkCanonical;
38
39
    /**
40
     * @var string
41
     */
42
    protected $separator;
43
44
    /**
45
     * @var array
46
     */
47
    protected $headAttributes;
48
49
    /**
50
     * @var array
51
     */
52
    protected $langAlternates;
53
54
    /**
55
     * @var array
56
     */
57
    protected $oembedLinks;
58
59
    /**
60
     * A JSON-LD string, which can be served as structured data.
61
     *
62
     * @var string
63
     */
64
    private $structuredData;
65
66
    /**
67
     * @param string $title
68
     */
69
    public function __construct($title = '')
70
    {
71
        $this->title = $title;
72
        $this->metas = [
73
            'http-equiv' => [],
74
            'name' => [],
75
            'schema' => [],
76
            'charset' => [],
77
            'property' => [],
78
        ];
79
80
        $this->headAttributes = [];
81
        $this->linkCanonical = '';
82
        $this->separator = ' ';
83
        $this->langAlternates = [];
84
        $this->oembedLinks = [];
85
        $this->structuredData = '';
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function setTitle($title)
92
    {
93
        $this->title = $title;
94
95
        return $this;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function addTitle($title)
102
    {
103
        $this->title = $title.$this->separator.$this->title;
104
105
        return $this;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function getTitle()
112
    {
113
        return $this->title;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function getMetas()
120
    {
121
        return $this->metas;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function addMeta($type, $name, $content, array $extras = [])
128
    {
129
        if (!isset($this->metas[$type])) {
130
            $this->metas[$type] = [];
131
        }
132
133
        $this->metas[$type][$name] = [$content, $extras];
134
135
        return $this;
136
    }
137
138
    /**
139
     * @param string $type
140
     * @param string $name
141
     *
142
     * @return bool
143
     */
144
    public function hasMeta($type, $name)
145
    {
146
        return isset($this->metas[$type][$name]);
147
    }
148
149
    /**
150
     * @param string $type
151
     * @param string $name
152
     *
153
     * @return $this
154
     */
155
    public function removeMeta($type, $name)
156
    {
157
        unset($this->metas[$type][$name]);
158
159
        return $this;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function setMetas(array $metadatas)
166
    {
167
        $this->metas = [];
168
169
        foreach ($metadatas as $type => $metas) {
170
            if (!is_array($metas)) {
171
                throw new \RuntimeException('$metas must be an array');
172
            }
173
174
            foreach ($metas as $name => $meta) {
175
                list($content, $extras) = $this->normalize($meta);
176
177
                $this->addMeta($type, $name, $content, $extras);
178
            }
179
        }
180
181
        return $this;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187
    public function setHtmlAttributes(array $attributes)
188
    {
189
        $this->htmlAttributes = $attributes;
190
191
        return $this;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function addHtmlAttributes($name, $value)
198
    {
199
        $this->htmlAttributes[$name] = $value;
200
201
        return $this;
202
    }
203
204
    /**
205
     * @param string $name
206
     *
207
     * @return $this
208
     */
209
    public function removeHtmlAttributes($name)
210
    {
211
        unset($this->htmlAttributes[$name]);
212
213
        return $this;
214
    }
215
216
    /**
217
     * {@inheritdoc}
218
     */
219
    public function getHtmlAttributes()
220
    {
221
        return $this->htmlAttributes;
222
    }
223
224
    /**
225
     * @param string $name
226
     *
227
     * @return bool
228
     */
229
    public function hasHtmlAttribute($name)
230
    {
231
        return isset($this->htmlAttributes[$name]);
232
    }
233
234
    /**
235
     * @param array $attributes
236
     *
237
     * @return SeoPageInterface
238
     */
239
    public function setHeadAttributes(array $attributes)
240
    {
241
        $this->headAttributes = $attributes;
242
243
        return $this;
244
    }
245
246
    /**
247
     * @param string $name
248
     * @param string $value
249
     *
250
     * @return SeoPageInterface
251
     */
252
    public function addHeadAttribute($name, $value)
253
    {
254
        $this->headAttributes[$name] = $value;
255
256
        return $this;
257
    }
258
259
    /**
260
     * @param string $name
261
     *
262
     * @return $this
263
     */
264
    public function removeHeadAttribute($name)
265
    {
266
        unset($this->headAttributes[$name]);
267
268
        return $this;
269
    }
270
271
    /**
272
     * @return array
273
     */
274
    public function getHeadAttributes()
275
    {
276
        return $this->headAttributes;
277
    }
278
279
    /**
280
     * @param string $name
281
     *
282
     * @return array
283
     */
284
    public function hasHeadAttribute($name)
285
    {
286
        return isset($this->headAttributes[$name]);
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292
    public function setLinkCanonical($link)
293
    {
294
        $this->linkCanonical = $link;
295
296
        return $this;
297
    }
298
299
    /**
300
     * {@inheritdoc}
301
     */
302
    public function getLinkCanonical()
303
    {
304
        return $this->linkCanonical;
305
    }
306
307
    /**
308
     * {@inheritdoc}
309
     */
310
    public function removeLinkCanonical()
311
    {
312
        $this->linkCanonical = '';
313
    }
314
315
    /**
316
     * {@inheritdoc}
317
     */
318
    public function setSeparator($separator)
319
    {
320
        $this->separator = $separator;
321
322
        return $this;
323
    }
324
325
    /**
326
     * {@inheritdoc}
327
     */
328
    public function setLangAlternates(array $langAlternates)
329
    {
330
        $this->langAlternates = $langAlternates;
331
332
        return $this;
333
    }
334
335
    /**
336
     * {@inheritdoc}
337
     */
338
    public function addLangAlternate($href, $hrefLang)
339
    {
340
        $this->langAlternates[$href] = $hrefLang;
341
342
        return $this;
343
    }
344
345
    /**
346
     * @param string $href
347
     *
348
     * @return $this
349
     */
350
    public function removeLangAlternate($href)
351
    {
352
        unset($this->langAlternates[$href]);
353
354
        return $this;
355
    }
356
357
    /**
358
     * @param string $href
359
     *
360
     * @return $this
361
     */
362
    public function hasLangAlternate($href)
363
    {
364
        return isset($this->langAlternates[$href]);
365
    }
366
367
    /**
368
     * {@inheritdoc}
369
     */
370
    public function getLangAlternates()
371
    {
372
        return  $this->langAlternates;
373
    }
374
375
    /**
376
     * @param $title
377
     * @param $link
378
     *
379
     * @return SeoPageInterface
380
     */
381
    public function addOEmbedLink($title, $link)
382
    {
383
        $this->oembedLinks[$title] = $link;
384
385
        return $this;
386
    }
387
388
    /**
389
     * @return array
390
     */
391
    public function getOEmbedLinks()
392
    {
393
        return $this->oembedLinks;
394
    }
395
396
    /**
397
     * {@inheritdoc}
398
     */
399
    public function getStructuredData()
400
    {
401
        return $this->structuredData;
402
    }
403
404
    /**
405
     * {@inheritdoc}
406
     */
407
    public function setStructuredData($structuredData)
408
    {
409
        $this->structuredData = $structuredData;
410
    }
411
412
    /**
413
     * @param mixed $meta
414
     *
415
     * @return array
416
     */
417
    private function normalize($meta)
418
    {
419
        if (is_string($meta)) {
420
            return [$meta, []];
421
        }
422
423
        return $meta;
424
    }
425
}
426