Passed
Push — master ( d093e3...81fa39 )
by Dāvis
03:13 queued 25s
created

XmlFormatter::formatVideo()   C

Complexity

Conditions 8
Paths 40

Size

Total Lines 41
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 28
nc 40
nop 1
dl 0
loc 41
rs 5.3846
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 checkVideoPlayerLoc($video)
81
    {
82
        $playerLoc = $video->getPlayerLoc();
83
        $allowEmbed = $playerLoc['allow_embed'] ? 'yes' : 'no';
84
        $autoplay = $playerLoc['autoplay'] !== null ? sprintf(' autoplay="%s"', $this->escape($playerLoc['autoplay'])) : '';
85
86
        return "\t\t".sprintf('<video:player_loc allow_embed="%s"%s>', $allowEmbed, $autoplay).$this->escape($playerLoc['loc']).'</video:player_loc>'."\n";
87
    }
88
89
    protected function checkVideoTags($video)
90
    {
91
        $text = '';
92
        foreach ($video->getTags() as $tag) {
93
            $text .= "\t\t".'<video:tag>'.$this->escape($tag).'</video:tag>'."\n";
94
        }
95
96
        return $text;
97
    }
98
99
    protected function checkVideoRestrictions($video)
100
    {
101
        $restrictions = $video->getRestrictions();
102
        $relationship = $this->escape($restrictions['relationship']);
103
104
        return "\t\t".'<video:restriction relationship="'.$relationship.'">'.$this->escape(implode(' ', $restrictions['countries'])).'</video:restriction>'."\n";
105
    }
106
107 View Code Duplication
    protected function checkVideoGalleryLoc($video)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
108
    {
109
        $galleryLoc = $video->getGalleryLoc();
110
        $title = $galleryLoc['title'] !== null ? sprintf(' title="%s"', $this->escape($galleryLoc['title'])) : '';
111
112
        return "\t\t".sprintf('<video:gallery_loc%s>', $title).$this->escape($galleryLoc['loc']).'</video:gallery_loc>'."\n";
113
    }
114
115 View Code Duplication
    protected function checkVideoUploader($video)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
116
    {
117
        $uploader = $video->getUploader();
118
        $info = $uploader['info'] !== null ? sprintf(' info="%s"', $this->escape($uploader['info'])) : '';
119
120
        return "\t\t".sprintf('<video:uploader%s>', $info).$this->escape($uploader['name']).'</video:uploader>'."\n";
121
    }
122
123
    protected function checkVideoPlatforms($video)
124
    {
125
        $text = '';
126
        foreach ($video->getPlatforms() as $platform => $relationship) {
127
            $text .= "\t\t".'<video:platform relationship="'.$this->escape($relationship).'">'.$this->escape($platform).'</video:platform>'."\n";
128
        }
129
130
        return $text;
131
    }
132
133
    protected function videoHeader($video)
134
    {
135
        $buffer = "\t".'<video:video>'."\n";
136
137
        $buffer .= "\t\t".'<video:title>'.$this->escape($video->getTitle()).'</video:title>'."\n";
138
        $buffer .= "\t\t".'<video:description>'.$this->escape($video->getDescription()).'</video:description>'."\n";
139
        $buffer .= "\t\t".'<video:thumbnail_loc>'.$this->escape($video->getThumbnailLoc()).'</video:thumbnail_loc>'."\n";
140
141
        return $buffer;
142
    }
143
144
    protected function formatVideo(Video $video)
145
    {
146
        $buffer = $this->videoHeader($video);
147
148
        if ($video->getFamilyFriendly() === false) {
149
            $buffer .= "\t\t".'<video:family_friendly>no</video:family_friendly>'."\n";
150
        }
151
152
        $checks = [
153
            'getContentLoc' => "\t\t".'<video:content_loc>'.$this->escape($video->getContentLoc()).'</video:content_loc>'."\n",
154
            'getDuration' => "\t\t".'<video:duration>'.$this->escape($video->getDuration()).'</video:duration>'."\n",
155
            'getExpirationDate' => "\t\t".'<video:expiration_date>'.$this->escape($video->getExpirationDate()).'</video:expiration_date>'."\n",
156
            'getRating' => "\t\t".'<video:rating>'.$this->escape($video->getRating()).'</video:rating>'."\n",
157
            'getViewCount' => "\t\t".'<video:view_count>'.$this->escape($video->getViewCount()).'</video:view_count>'."\n",
158
            'getPublicationDate' => "\t\t".'<video:publication_date>'.$this->escape($video->getPublicationDate()).'</video:publication_date>'."\n",
159
            'getCategory' => "\t\t".'<video:category>'.$this->escape($video->getCategory()).'</video:category>'."\n",
160
            'getRequiresSubscription' => "\t\t".'<video:requires_subscription>'.($video->getRequiresSubscription() ? 'yes' : 'no').'</video:requires_subscription>'."\n",
161
            'getLive' => "\t\t".'<video:live>'.($video->getLive() ? 'yes' : 'no').'</video:live>'."\n",
162
        ];
163
164
        $extended = [
165
            'getPlayerLoc' => 'checkVideoPlayerLoc',
166
            'getTags' => 'checkVideoTags',
167
            'getRestrictions' => 'checkVideoRestrictions',
168
            'getGalleryLoc' => 'checkVideoGalleryLoc',
169
            'getUploader' => 'checkVideoUploader',
170
            'getPlatforms' => 'checkVideoPlatforms',
171
        ];
172
173
        $keys = array_merge(array_keys($checks), array_keys($extended));
174
        foreach ($keys as $key) {
175
            if ($video->{$key}() !== null) {
176
                if (isset($checks[$key])) {
177
                    $buffer .= $checks[$key];
178
                } elseif (isset($extended[$key])) {
179
                    $buffer .= $this->{$extended[$key]}($video);
180
                }
181
            }
182
        }
183
184
        return $buffer."\t".'</video:video>'."\n";
185
    }
186
187
    protected function formatImage(Image $image)
188
    {
189
        $buffer = "\t".'<image:image>'."\n\t\t".'<image:loc>'.$this->escape($image->getLoc()).'</image:loc>'."\n";
190
191
        $checks = [
192
            'getCaption' => "\t\t".'<image:caption>'.$this->escape($image->getCaption()).'</image:caption>'."\n",
193
            'getGeoLocation' => "\t\t".'<image:geo_location>'.$this->escape($image->getGeoLocation()).'</image:geo_location>'."\n",
194
            'getTitle' => "\t\t".'<image:title>'.$this->escape($image->getTitle()).'</image:title>'."\n",
195
            'getLicense' => "\t\t".'<image:license>'.$this->escape($image->getLicense()).'</image:license>'."\n",
196
        ];
197
198
        foreach ($checks as $check => $text) {
199
            if ($image->{$check}() !== null) {
200
                $buffer .= $text;
201
            }
202
        }
203
204
        return $buffer."\t".'</image:image>'."\n";
205
    }
206
}
207