| Total Complexity | 42 |
| Total Lines | 220 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 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) |
||
| 35 | } |
||
| 36 | |||
| 37 | protected function formatBody(Url $url) |
||
| 38 | { |
||
| 39 | $buffer = "\t".'<loc>'.$this->escape($url->getLoc()).'</loc>'."\n"; |
||
| 62 | } |
||
| 63 | |||
| 64 | private function getChecks(Video $video, $keys = false) |
||
| 65 | { |
||
| 66 | $checks = [ |
||
| 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 | ]; |
||
| 78 | |||
| 79 | if ($keys === true) { |
||
| 80 | return \array_keys($checks); |
||
| 81 | } |
||
| 82 | |||
| 83 | return $checks; |
||
| 84 | } |
||
| 85 | |||
| 86 | private static function getExtended($keys = false) |
||
| 87 | { |
||
| 88 | $checks = [ |
||
| 89 | 'getPlayerLoc' => 'checkVideoPlayerLoc', |
||
| 90 | 'getTags' => 'checkVideoTags', |
||
| 91 | 'getRestrictions' => 'checkVideoRestrictions', |
||
| 92 | 'getGalleryLoc' => 'checkVideoGalleryLoc', |
||
| 93 | 'getUploader' => 'checkVideoUploader', |
||
| 94 | 'getPlatforms' => 'checkVideoPlatforms', |
||
| 95 | ]; |
||
| 96 | |||
| 97 | if ($keys === true) { |
||
| 98 | return \array_keys($checks); |
||
| 99 | } |
||
| 100 | |||
| 101 | return $checks; |
||
| 102 | } |
||
| 103 | |||
| 104 | private function getCheckKeys(Video $video) |
||
| 105 | { |
||
| 106 | return array_merge($this->getChecks($video, true), self::getExtended(true)); |
||
| 107 | } |
||
| 108 | |||
| 109 | protected function formatVideo(Video $video) |
||
| 110 | { |
||
| 111 | $buffer = $this->videoHeader($video); |
||
| 112 | $checks = $this->getChecks($video); |
||
| 113 | $extended = self::getExtended(); |
||
| 114 | |||
| 115 | foreach ($this->getCheckKeys($video) as $key) { |
||
| 116 | if ($video->{$key}() !== null) { |
||
| 117 | if (isset($checks[$key])) { |
||
| 118 | $buffer .= $checks[$key]; |
||
| 119 | } elseif (isset($extended[$key])) { |
||
| 120 | $buffer .= $this->{$extended[$key]}($video); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | return $buffer."\t".'</video:video>'."\n"; |
||
| 126 | } |
||
| 127 | |||
| 128 | protected function videoHeader($video) |
||
| 129 | { |
||
| 130 | $buffer = "\t".'<video:video>'."\n"; |
||
| 131 | |||
| 132 | $buffer .= "\t\t".'<video:title>'.$this->escape($video->getTitle()).'</video:title>'."\n"; |
||
| 133 | $buffer .= "\t\t".'<video:description>'.$this->escape($video->getDescription()).'</video:description>'."\n"; |
||
| 134 | $buffer .= "\t\t".'<video:thumbnail_loc>'.$this->escape($video->getThumbnailLoc()).'</video:thumbnail_loc>'."\n"; |
||
| 135 | |||
| 136 | return $buffer; |
||
| 137 | } |
||
| 138 | |||
| 139 | protected function formatImage(Image $image) |
||
| 140 | { |
||
| 141 | $buffer = "\t".'<image:image>'."\n\t\t".'<image:loc>'.$this->escape($image->getLoc()).'</image:loc>'."\n"; |
||
| 142 | |||
| 143 | $checks = [ |
||
| 144 | 'getCaption' => "\t\t".'<image:caption>'.$this->escape($image->getCaption()).'</image:caption>'."\n", |
||
| 145 | 'getGeoLocation' => "\t\t".'<image:geo_location>'.$this->escape($image->getGeoLocation()).'</image:geo_location>'."\n", |
||
| 146 | 'getTitle' => "\t\t".'<image:title>'.$this->escape($image->getTitle()).'</image:title>'."\n", |
||
| 147 | 'getLicense' => "\t\t".'<image:license>'.$this->escape($image->getLicense()).'</image:license>'."\n", |
||
| 148 | ]; |
||
| 149 | |||
| 150 | foreach ($checks as $check => $text) { |
||
| 151 | if ($image->{$check}() !== null) { |
||
| 152 | $buffer .= $text; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | return $buffer."\t".'</image:image>'."\n"; |
||
| 157 | } |
||
| 158 | |||
| 159 | public function formatSitemapIndex(SitemapIndex $sitemapIndex) |
||
| 160 | { |
||
| 161 | return '<sitemap>'."\n".$this->formatSitemapIndexBody($sitemapIndex).'</sitemap>'."\n"; |
||
| 162 | } |
||
| 163 | |||
| 164 | protected function formatSitemapIndexBody(SitemapIndex $sitemapIndex) |
||
| 165 | { |
||
| 166 | $buffer = "\t".'<loc>'.$this->escape($sitemapIndex->getLoc()).'</loc>'."\n"; |
||
| 167 | |||
| 168 | if ($sitemapIndex->getLastmod() !== null) { |
||
| 169 | $buffer .= "\t".'<lastmod>'.$this->escape($sitemapIndex->getLastmod()).'</lastmod>'."\n"; |
||
| 170 | } |
||
| 171 | |||
| 172 | return $buffer; |
||
| 173 | } |
||
| 174 | |||
| 175 | protected function checkVideoPlayerLoc(Video $video) |
||
| 182 | } |
||
| 183 | |||
| 184 | protected function checkVideoTags(Video $video) |
||
| 185 | { |
||
| 186 | $text = ''; |
||
| 187 | $tags = $video->getTags(); |
||
| 188 | /** @var $tags array */ |
||
| 189 | foreach ($tags as $tag) { |
||
| 190 | $text .= "\t\t".'<video:tag>'.$this->escape($tag).'</video:tag>'."\n"; |
||
| 191 | } |
||
| 192 | |||
| 193 | return $text; |
||
| 194 | } |
||
| 195 | |||
| 196 | protected function checkVideoRestrictions(Video $video) |
||
| 202 | } |
||
| 203 | |||
| 204 | protected function checkVideoGalleryLoc(Video $video) |
||
| 205 | { |
||
| 206 | $galleryLoc = $video->getGalleryLoc(); |
||
| 207 | $title = $galleryLoc['title'] !== null ? sprintf(' title="%s"', $this->escape($galleryLoc['title'])) : ''; |
||
| 208 | |||
| 209 | return "\t\t".sprintf('<video:gallery_loc%s>', $title).$this->escape($galleryLoc['loc']).'</video:gallery_loc>'."\n"; |
||
| 210 | } |
||
| 211 | |||
| 212 | protected function checkVideoUploader(Video $video) |
||
| 213 | { |
||
| 214 | $uploader = $video->getUploader(); |
||
| 215 | $info = $uploader['info'] !== null ? sprintf(' info="%s"', $this->escape($uploader['info'])) : ''; |
||
| 216 | |||
| 217 | return "\t\t".sprintf('<video:uploader%s>', $info).$this->escape($uploader['name']).'</video:uploader>'."\n"; |
||
| 218 | } |
||
| 219 | |||
| 220 | protected function checkVideoPlatforms(Video $video) |
||
| 230 | } |
||
| 231 | } |
||
| 232 |