Passed
Push — master ( 896e22...61bd56 )
by Dāvis
03:36
created

XmlFormatter   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 185
Duplicated Lines 33.51 %

Importance

Changes 0
Metric Value
dl 62
loc 185
rs 8.3396
c 0
b 0
f 0
wmc 44

10 Methods

Rating   Name   Duplication   Size   Complexity  
F formatVideo() 44 90 25
B formatBody() 3 25 6
A formatUrl() 0 3 1
A formatSitemapIndex() 0 3 1
A getSitemapIndexEnd() 0 3 1
A formatSitemapIndexBody() 3 9 2
A getSitemapIndexStart() 0 3 1
A getSitemapEnd() 0 3 1
A getSitemapStart() 0 3 1
B formatImage() 12 23 5

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like XmlFormatter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use XmlFormatter, and based on these observations, apply Extract Interface, too.

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 View Code Duplication
        if ($url->getLastmod() !== 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...
42
            $buffer .= "\t".'<lastmod>'.$this->escape($url->getLastmod()).'</lastmod>'."\n";
43
        }
44
45
        if ($url->getChangefreq() !== null) {
46
            $buffer .= "\t".'<changefreq>'.$this->escape($url->getChangefreq()).'</changefreq>'."\n";
47
        }
48
49
        if ($url->getPriority() !== null) {
50
            $buffer .= "\t".'<priority>'.$this->escape($url->getPriority()).'</priority>'."\n";
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 View Code Duplication
        if ($sitemapIndex->getLastmod() !== 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...
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 View Code Duplication
        if ($video->getContentLoc() !== 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...
89
            $buffer .= "\t\t".'<video:content_loc>'.$this->escape($video->getContentLoc()).'</video:content_loc>'."\n";
90
        }
91
92
        if ($video->getPlayerLoc() !== null) {
93
            $playerLoc = $video->getPlayerLoc();
94
            $allowEmbed = $playerLoc['allow_embed'] ? 'yes' : 'no';
95
            $autoplay = $playerLoc['autoplay'] !== null ? sprintf(' autoplay="%s"', $this->escape($playerLoc['autoplay'])) : '';
96
97
            $buffer .= "\t\t".sprintf('<video:player_loc allow_embed="%s"%s>', $allowEmbed, $autoplay).$this->escape($playerLoc['loc']).'</video:player_loc>'."\n";
98
        }
99
100 View Code Duplication
        if ($video->getDuration() !== 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...
101
            $buffer .= "\t\t".'<video:duration>'.$this->escape($video->getDuration()).'</video:duration>'."\n";
102
        }
103
104 View Code Duplication
        if ($video->getExpirationDate() !== 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...
105
            $buffer .= "\t\t".'<video:expiration_date>'.$this->escape($video->getExpirationDate()).'</video:expiration_date>'."\n";
106
        }
107
108 View Code Duplication
        if ($video->getRating() !== 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...
109
            $buffer .= "\t\t".'<video:rating>'.$this->escape($video->getRating()).'</video:rating>'."\n";
110
        }
111
112 View Code Duplication
        if ($video->getViewCount() !== 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...
113
            $buffer .= "\t\t".'<video:view_count>'.$this->escape($video->getViewCount()).'</video:view_count>'."\n";
114
        }
115
116 View Code Duplication
        if ($video->getPublicationDate() !== 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...
117
            $buffer .= "\t\t".'<video:publication_date>'.$this->escape($video->getPublicationDate()).'</video:publication_date>'."\n";
118
        }
119
120
        if ($video->getFamilyFriendly() === false) {
121
            $buffer .= "\t\t".'<video:family_friendly>no</video:family_friendly>'."\n";
122
        }
123
124 View Code Duplication
        if ($video->getTags() !== 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...
125
            foreach ($video->getTags() as $tag) {
126
                $buffer .= "\t\t".'<video:tag>'.$this->escape($tag).'</video:tag>'."\n";
127
            }
128
        }
129
130 View Code Duplication
        if ($video->getCategory() !== 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...
131
            $buffer .= "\t\t".'<video:category>'.$this->escape($video->getCategory()).'</video:category>'."\n";
132
        }
133
134
        if ($video->getRestrictions() !== null) {
135
            $restrictions = $video->getRestrictions();
136
            $relationship = $this->escape($restrictions['relationship']);
137
138
            $buffer .= "\t\t".'<video:restriction relationship="'.$relationship.'">'.$this->escape(implode(' ', $restrictions['countries'])).'</video:restriction>'."\n";
139
        }
140
141 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...
142
            $galleryLoc = $video->getGalleryLoc();
143
            $title = $galleryLoc['title'] !== null ? sprintf(' title="%s"', $this->escape($galleryLoc['title'])) : '';
144
145
            $buffer .= "\t\t".sprintf('<video:gallery_loc%s>', $title).$this->escape($galleryLoc['loc']).'</video:gallery_loc>'."\n";
146
        }
147
148 View Code Duplication
        if ($video->getRequiresSubscription() !== 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...
149
            $buffer .= "\t\t".'<video:requires_subscription>'.($video->getRequiresSubscription() ? 'yes' : 'no').'</video:requires_subscription>'."\n";
150
        }
151
152 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...
153
            $uploader = $video->getUploader();
154
            $info = $uploader['info'] !== null ? sprintf(' info="%s"', $this->escape($uploader['info'])) : '';
155
156
            $buffer .= "\t\t".sprintf('<video:uploader%s>', $info).$this->escape($uploader['name']).'</video:uploader>'."\n";
157
        }
158
159
        if ($video->getPlatforms() !== null) {
160
            foreach ($video->getPlatforms() as $platform => $relationship) {
161
                $buffer .= "\t\t".'<video:platform relationship="'.$this->escape($relationship).'">'.$this->escape($platform).'</video:platform>'."\n";
162
            }
163
        }
164
165 View Code Duplication
        if ($video->getLive() !== 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...
166
            $buffer .= "\t\t".'<video:live>'.($video->getLive() ? 'yes' : 'no').'</video:live>'."\n";
167
        }
168
169
        return $buffer."\t".'</video:video>'."\n";
170
    }
171
172
    protected function formatImage(Image $image)
173
    {
174
        $buffer = "\t".'<image:image>'."\n";
175
176
        $buffer .= "\t\t".'<image:loc>'.$this->escape($image->getLoc()).'</image:loc>'."\n";
177
178 View Code Duplication
        if ($image->getCaption() !== 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...
179
            $buffer .= "\t\t".'<image:caption>'.$this->escape($image->getCaption()).'</image:caption>'."\n";
180
        }
181
182 View Code Duplication
        if ($image->getGeoLocation() !== 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...
183
            $buffer .= "\t\t".'<image:geo_location>'.$this->escape($image->getGeoLocation()).'</image:geo_location>'."\n";
184
        }
185
186 View Code Duplication
        if ($image->getTitle() !== 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...
187
            $buffer .= "\t\t".'<image:title>'.$this->escape($image->getTitle()).'</image:title>'."\n";
188
        }
189
190 View Code Duplication
        if ($image->getLicense() !== 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...
191
            $buffer .= "\t\t".'<image:license>'.$this->escape($image->getLicense()).'</image:license>'."\n";
192
        }
193
194
        return $buffer."\t".'</image:image>'."\n";
195
    }
196
}