Completed
Pull Request — 2.x (#266)
by Maximilian
06:50 queued 01:48
created

StructuredDataExtension   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFunctions() 0 6 1
A getStructuredData() 0 8 2
1
<?php
2
3
namespace Sonata\SeoBundle\Twig\Extension;
4
5
use Sonata\SeoBundle\Seo\StructuredDataAwarePage;
6
7
/**
8
 * @author Maximilian Berghoff <[email protected]>
9
 */
10
class StructuredDataExtension extends \Twig_Extension
11
{
12
    /**
13
     * @var StructuredDataAwarePage
14
     */
15
    protected $page;
16
17
    /**
18
     * @var string
19
     */
20
    protected $encoding;
21
22
    /**
23
     * @param StructuredDataAwarePage $page
24
     */
25
    public function __construct(StructuredDataAwarePage $page)
26
    {
27
        $this->page = $page;
28
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function getFunctions()
34
    {
35
        return [
36
            new \Twig_SimpleFunction('sonata_seo_structured_data', [$this, 'getStructuredData'], ['is_safe' => ['html']]),
37
        ];
38
    }
39
40
    /**
41
     * Creates a script tag with type 'json-ld' and the JSON-LD string stored in page object.
42
     *
43
     * @return string
44
     */
45
    public function getStructuredData()
46
    {
47
        if (empty($this->page->getStructuredData())) {
48
            return '';
49
        }
50
51
        return sprintf("<script type=\"application/ld+json\">%s</script>\n", $this->page->getStructuredData());
52
    }
53
}
54