Passed
Push — master ( 947e69...7e4879 )
by Dāvis
04:17
created

XmlFormatter::getChecks()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 8
nop 1
dl 0
loc 19
rs 9.2
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\SitemapIndex;
7
use Sludio\HelperBundle\Sitemap\Entity\Url;
8
use Sludio\HelperBundle\Sitemap\Entity\Video;
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
    private function getChecks(Video $video)
65
    {
66
        return [
67
            'getContentLoc' => "\t\t".'<video:content_loc>'.$this->escape($video->getContentLoc()).'</video:content_loc>'."\n",
68
            'getDuration' => "\t\t".'<video:duration>'.$this->escape($video->getDuration()).'</video:duration>'."\n",
69
            'getExpirationDate' => "\t\t".'<video:expiration_date>'.$this->escape($video->getExpirationDate()).'</video:expiration_date>'."\n",
70
            'getRating' => "\t\t".'<video:rating>'.$this->escape($video->getRating()).'</video:rating>'."\n",
71
            'getViewCount' => "\t\t".'<video:view_count>'.$this->escape($video->getViewCount()).'</video:view_count>'."\n",
72
            'getPublicationDate' => "\t\t".'<video:publication_date>'.$this->escape($video->getPublicationDate()).'</video:publication_date>'."\n",
73
            'getCategory' => "\t\t".'<video:category>'.$this->escape($video->getCategory()).'</video:category>'."\n",
74
            'getRequiresSubscription' => "\t\t".'<video:requires_subscription>'.($video->getRequiresSubscription() ? 'yes' : 'no').'</video:requires_subscription>'."\n",
75
            'getLive' => "\t\t".'<video:live>'.($video->getLive() ? 'yes' : 'no').'</video:live>'."\n",
76
            'getFamilyFriendly' => "\t\t".'<video:family_friendly>'.($video->getFamilyFriendly() ? 'yes' : 'no').'</video:family_friendly>'."\n",
77
            'getPlayerLoc' => $this->checkVideoPlayerLoc($video),
78
            'getTags' => $this->checkVideoTags($video),
79
            'getRestrictions' => $this->checkVideoRestrictions($video),
80
            'getGalleryLoc' => $this->checkVideoGalleryLoc($video),
81
            'getUploader' => $this->checkVideoUploader($video),
82
            'getPlatforms' => $this->checkVideoPlatforms($video),
83
        ];
84
    }
85
86
    protected function formatVideo(Video $video)
87
    {
88
        $buffer = $this->videoHeader($video);
89
        $checks = $this->getChecks($video);
90
91
        foreach (\array_keys($checks) as $key) {
92
            if ($video->{$key}() !== null) {
93
                $buffer .= $checks[$key];
94
            }
95
        }
96
97
        return $buffer."\t".'</video:video>'."\n";
98
    }
99
100
    protected function videoHeader($video)
101
    {
102
        $buffer = "\t".'<video:video>'."\n";
103
104
        $buffer .= "\t\t".'<video:title>'.$this->escape($video->getTitle()).'</video:title>'."\n";
105
        $buffer .= "\t\t".'<video:description>'.$this->escape($video->getDescription()).'</video:description>'."\n";
106
        $buffer .= "\t\t".'<video:thumbnail_loc>'.$this->escape($video->getThumbnailLoc()).'</video:thumbnail_loc>'."\n";
107
108
        return $buffer;
109
    }
110
111
    protected function formatImage(Image $image)
112
    {
113
        $buffer = "\t".'<image:image>'."\n\t\t".'<image:loc>'.$this->escape($image->getLoc()).'</image:loc>'."\n";
114
115
        $checks = [
116
            'getCaption' => "\t\t".'<image:caption>'.$this->escape($image->getCaption()).'</image:caption>'."\n",
117
            'getGeoLocation' => "\t\t".'<image:geo_location>'.$this->escape($image->getGeoLocation()).'</image:geo_location>'."\n",
118
            'getTitle' => "\t\t".'<image:title>'.$this->escape($image->getTitle()).'</image:title>'."\n",
119
            'getLicense' => "\t\t".'<image:license>'.$this->escape($image->getLicense()).'</image:license>'."\n",
120
        ];
121
122
        foreach ($checks as $check => $text) {
123
            if ($image->{$check}() !== null) {
124
                $buffer .= $text;
125
            }
126
        }
127
128
        return $buffer."\t".'</image:image>'."\n";
129
    }
130
131
    public function formatSitemapIndex(SitemapIndex $sitemapIndex)
132
    {
133
        return '<sitemap>'."\n".$this->formatSitemapIndexBody($sitemapIndex).'</sitemap>'."\n";
134
    }
135
136
    protected function formatSitemapIndexBody(SitemapIndex $sitemapIndex)
137
    {
138
        $buffer = "\t".'<loc>'.$this->escape($sitemapIndex->getLoc()).'</loc>'."\n";
139
140
        if ($sitemapIndex->getLastmod() !== null) {
141
            $buffer .= "\t".'<lastmod>'.$this->escape($sitemapIndex->getLastmod()).'</lastmod>'."\n";
142
        }
143
144
        return $buffer;
145
    }
146
147
    protected function checkVideoPlayerLoc(Video $video)
148
    {
149
        $playerLoc = $video->getPlayerLoc();
150
        $allowEmbed = $playerLoc['allow_embed'] ? 'yes' : 'no';
151
        $autoplay = $playerLoc['autoplay'] !== null ? sprintf(' autoplay="%s"', $this->escape($playerLoc['autoplay'])) : '';
152
153
        return "\t\t".sprintf('<video:player_loc allow_embed="%s"%s>', $allowEmbed, $autoplay).$this->escape($playerLoc['loc']).'</video:player_loc>'."\n";
154
    }
155
156
    protected function checkVideoTags(Video $video)
157
    {
158
        $text = '';
159
        $tags = $video->getTags();
160
        /** @var $tags array */
161
        foreach ($tags as $tag) {
162
            $text .= "\t\t".'<video:tag>'.$this->escape($tag).'</video:tag>'."\n";
163
        }
164
165
        return $text;
166
    }
167
168
    protected function checkVideoRestrictions(Video $video)
169
    {
170
        $restrictions = $video->getRestrictions();
171
        $relationship = $this->escape($restrictions['relationship']);
172
173
        return "\t\t".'<video:restriction relationship="'.$relationship.'">'.$this->escape(implode(' ', $restrictions['countries'])).'</video:restriction>'."\n";
174
    }
175
176
    protected function checkVideoGalleryLoc(Video $video)
177
    {
178
        $galleryLoc = $video->getGalleryLoc();
179
        $title = $galleryLoc['title'] !== null ? sprintf(' title="%s"', $this->escape($galleryLoc['title'])) : '';
180
181
        return "\t\t".sprintf('<video:gallery_loc%s>', $title).$this->escape($galleryLoc['loc']).'</video:gallery_loc>'."\n";
182
    }
183
184
    protected function checkVideoUploader(Video $video)
185
    {
186
        $uploader = $video->getUploader();
187
        $info = $uploader['info'] !== null ? sprintf(' info="%s"', $this->escape($uploader['info'])) : '';
188
189
        return "\t\t".sprintf('<video:uploader%s>', $info).$this->escape($uploader['name']).'</video:uploader>'."\n";
190
    }
191
192
    protected function checkVideoPlatforms(Video $video)
193
    {
194
        $text = '';
195
        $platforms = $video->getPlatforms();
196
        /** @var $platforms array */
197
        foreach ($platforms as $platform => $relationship) {
198
            $text .= "\t\t".'<video:platform relationship="'.$this->escape($relationship).'">'.$this->escape($platform).'</video:platform>'."\n";
199
        }
200
201
        return $text;
202
    }
203
}
204