Passed
Push — master ( bc6688...f0a8fd )
by Dāvis
03:26
created

XmlFormatter::getSitemapIndexStart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sludio\HelperBundle\Sitemap\Formatter;
4
5
use Sludio\HelperBundle\Sitemap\Entity\Image;
6
use Sludio\HelperBundle\Sitemap\Entity\Url;
7
use Sludio\HelperBundle\Sitemap\Entity\Video;
8
use Sludio\HelperBundle\Sitemap\Entity\SitemapIndex;
9
10
class XmlFormatter extends BaseFormatter implements SitemapIndexFormatterInterface
11
{
12
    public function getSitemapStart()
13
    {
14
        return '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<urlset '.'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" '.'xmlns:video="http://www.google.com/schemas/sitemap-video/1.1" '.'xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">'."\n";
15
    }
16
17
    public function getSitemapEnd()
18
    {
19
        return '</urlset>';
20
    }
21
22
    public function getSitemapIndexStart()
23
    {
24
        return '<?xml version="1.0" encoding="UTF-8"?>'."\n".'<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
25
    }
26
27
    public function getSitemapIndexEnd()
28
    {
29
        return '</sitemapindex>';
30
    }
31
32
    public function formatUrl(Url $url)
33
    {
34
        return '<url>'."\n".$this->formatBody($url).'</url>'."\n";
35
    }
36
37
    protected function formatBody(Url $url)
38
    {
39
        $buffer = "\t".'<loc>'.$this->escape($url->getLoc()).'</loc>'."\n";
40
41
        $checks = [
42
            'getLastmod' => "\t".'<lastmod>'.$this->escape($url->getLastmod()).'</lastmod>'."\n",
43
            'getChangefreq' => "\t".'<changefreq>'.$this->escape($url->getChangefreq()).'</changefreq>'."\n",
44
            'getPriority' => "\t".'<priority>'.$this->escape($url->getPriority()).'</priority>'."\n",
45
        ];
46
47
        foreach ($checks as $check => $text) {
48
            if ($url->{$check}() !== null) {
49
                $buffer .= $text;
50
            }
51
        }
52
53
        foreach ($url->getVideos() as $video) {
54
            $buffer .= $this->formatVideo($video);
55
        }
56
57
        foreach ($url->getImages() as $image) {
58
            $buffer .= $this->formatImage($image);
59
        }
60
61
        return $buffer;
62
    }
63
64
    public function formatSitemapIndex(SitemapIndex $sitemapIndex)
65
    {
66
        return '<sitemap>'."\n".$this->formatSitemapIndexBody($sitemapIndex).'</sitemap>'."\n";
67
    }
68
69
    protected function formatSitemapIndexBody(SitemapIndex $sitemapIndex)
70
    {
71
        $buffer = "\t".'<loc>'.$this->escape($sitemapIndex->getLoc()).'</loc>'."\n";
72
73
        if ($sitemapIndex->getLastmod() !== null) {
74
            $buffer .= "\t".'<lastmod>'.$this->escape($sitemapIndex->getLastmod()).'</lastmod>'."\n";
75
        }
76
77
        return $buffer;
78
    }
79
80
    protected function formatVideo(Video $video)
81
    {
82
        $buffer = "\t".'<video:video>'."\n";
83
84
        $buffer .= "\t\t".'<video:title>'.$this->escape($video->getTitle()).'</video:title>'."\n";
85
        $buffer .= "\t\t".'<video:description>'.$this->escape($video->getDescription()).'</video:description>'."\n";
86
        $buffer .= "\t\t".'<video:thumbnail_loc>'.$this->escape($video->getThumbnailLoc()).'</video:thumbnail_loc>'."\n";
87
88
        $checks = [
89
            'getContentLoc' => "\t\t".'<video:content_loc>'.$this->escape($video->getContentLoc()).'</video:content_loc>'."\n",
90
            'getDuration' => "\t\t".'<video:duration>'.$this->escape($video->getDuration()).'</video:duration>'."\n",
91
            'getExpirationDate' => "\t\t".'<video:expiration_date>'.$this->escape($video->getExpirationDate()).'</video:expiration_date>'."\n",
92
            'getRating' => "\t\t".'<video:rating>'.$this->escape($video->getRating()).'</video:rating>'."\n",
93
            'getViewCount' => "\t\t".'<video:view_count>'.$this->escape($video->getViewCount()).'</video:view_count>'."\n",
94
            'getPublicationDate' => "\t\t".'<video:publication_date>'.$this->escape($video->getPublicationDate()).'</video:publication_date>'."\n",
95
            'getCategory' => "\t\t".'<video:category>'.$this->escape($video->getCategory()).'</video:category>'."\n",
96
            'getRequiresSubscription' => "\t\t".'<video:requires_subscription>'.($video->getRequiresSubscription() ? 'yes' : 'no').'</video:requires_subscription>'."\n",
97
            'getLive' => "\t\t".'<video:live>'.($video->getLive() ? 'yes' : 'no').'</video:live>'."\n",
98
            'getFamilyFriendly' => "\t\t".'<video:family_friendly>no</video:family_friendly>'."\n",
99
        ];
100
101
        foreach ($checks as $check => $text) {
102
            if ($video->{$check}() !== null) {
103
                $buffer .= $text;
104
            }
105
        }
106
107
        if ($video->getPlayerLoc() !== null) {
108
            $playerLoc = $video->getPlayerLoc();
109
            $allowEmbed = $playerLoc['allow_embed'] ? 'yes' : 'no';
110
            $autoplay = $playerLoc['autoplay'] !== null ? sprintf(' autoplay="%s"', $this->escape($playerLoc['autoplay'])) : '';
111
112
            $buffer .= "\t\t".sprintf('<video:player_loc allow_embed="%s"%s>', $allowEmbed, $autoplay).$this->escape($playerLoc['loc']).'</video:player_loc>'."\n";
113
        }
114
115
        if ($video->getTags() !== null) {
116
            foreach ($video->getTags() as $tag) {
117
                $buffer .= "\t\t".'<video:tag>'.$this->escape($tag).'</video:tag>'."\n";
118
            }
119
        }
120
121
        if ($video->getRestrictions() !== null) {
122
            $restrictions = $video->getRestrictions();
123
            $relationship = $this->escape($restrictions['relationship']);
124
125
            $buffer .= "\t\t".'<video:restriction relationship="'.$relationship.'">'.$this->escape(implode(' ', $restrictions['countries'])).'</video:restriction>'."\n";
126
        }
127
128 View Code Duplication
        if ($video->getGalleryLoc() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
            $galleryLoc = $video->getGalleryLoc();
130
            $title = $galleryLoc['title'] !== null ? sprintf(' title="%s"', $this->escape($galleryLoc['title'])) : '';
131
132
            $buffer .= "\t\t".sprintf('<video:gallery_loc%s>', $title).$this->escape($galleryLoc['loc']).'</video:gallery_loc>'."\n";
133
        }
134
135 View Code Duplication
        if ($video->getUploader() !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
            $uploader = $video->getUploader();
137
            $info = $uploader['info'] !== null ? sprintf(' info="%s"', $this->escape($uploader['info'])) : '';
138
139
            $buffer .= "\t\t".sprintf('<video:uploader%s>', $info).$this->escape($uploader['name']).'</video:uploader>'."\n";
140
        }
141
142
        if ($video->getPlatforms() !== null) {
143
            foreach ($video->getPlatforms() as $platform => $relationship) {
144
                $buffer .= "\t\t".'<video:platform relationship="'.$this->escape($relationship).'">'.$this->escape($platform).'</video:platform>'."\n";
145
            }
146
        }
147
148
        return $buffer."\t".'</video:video>'."\n";
149
    }
150
151
    protected function formatImage(Image $image)
152
    {
153
        $buffer = "\t".'<image:image>'."\n";
154
155
        $buffer .= "\t\t".'<image:loc>'.$this->escape($image->getLoc()).'</image:loc>'."\n";
156
157
        $checks = [
158
            'getCaption' => "\t\t".'<image:caption>'.$this->escape($image->getCaption()).'</image:caption>'."\n",
159
            'getGeoLocation' => "\t\t".'<image:geo_location>'.$this->escape($image->getGeoLocation()).'</image:geo_location>'."\n",
160
            'getTitle' => "\t\t".'<image:title>'.$this->escape($image->getTitle()).'</image:title>'."\n",
161
            'getLicense' => "\t\t".'<image:license>'.$this->escape($image->getLicense()).'</image:license>'."\n",
162
        ];
163
164
        foreach ($checks as $check => $text) {
165
            if ($image->{$check}() !== null) {
166
                $buffer .= $text;
167
            }
168
        }
169
170
        return $buffer."\t".'</image:image>'."\n";
171
    }
172
}