| Total Complexity | 44 |
| Total Lines | 185 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Sitemap 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 Sitemap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Sitemap |
||
| 15 | { |
||
| 16 | protected $providers = []; |
||
| 17 | protected $dumper = null; |
||
| 18 | protected $formatter = null; |
||
| 19 | protected $baseHost = null; |
||
| 20 | protected $limit = 0; |
||
| 21 | protected $sitemapIndexes = []; |
||
| 22 | protected $originalFilename = null; |
||
| 23 | |||
| 24 | public function __construct(DumperInterface $dumper, FormatterInterface $formatter, $baseHost = null, $limit = 0) |
||
| 25 | { |
||
| 26 | $this->dumper = $dumper; |
||
| 27 | $this->formatter = $formatter; |
||
| 28 | $this->baseHost = $baseHost; |
||
| 29 | if ($this->baseHost === null && php_sapi_name() !== 'cli') { |
||
| 30 | $request = Request::createFromGlobals(); |
||
| 31 | $useHttps = $request->server->get('HTTPS') || ($request->server->get('HTTP_X_FORWARDED_PROTO') && $request->server->get('HTTP_X_FORWARDED_PROTO') == 'https'); |
||
| 32 | $this->baseHost = ($useHttps ? 'https' : 'http').'://'.$request->server->get('HTTP_HOST'); |
||
| 33 | } |
||
| 34 | $this->limit = $limit; |
||
| 35 | if ($this->isSitemapIndexable()) { |
||
| 36 | $this->originalFilename = $dumper->getFilename(); |
||
|
|
|||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | public function addProvider(ProviderInterface $provider) |
||
| 41 | { |
||
| 42 | $this->providers[] = $provider; |
||
| 43 | |||
| 44 | return $this; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function setDumper(DumperInterface $dumper) |
||
| 48 | { |
||
| 49 | $this->dumper = $dumper; |
||
| 50 | |||
| 51 | return $this; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function build() |
||
| 55 | { |
||
| 56 | if ($this->isSitemapIndexable()) { |
||
| 57 | $this->addSitemapIndex($this->createSitemapIndex()); |
||
| 58 | } |
||
| 59 | |||
| 60 | $this->dumper->dump($this->formatter->getSitemapStart()); |
||
| 61 | |||
| 62 | foreach ($this->providers as $provider) { |
||
| 63 | $provider->populate($this); |
||
| 64 | } |
||
| 65 | |||
| 66 | $sitemapContent = $this->dumper->dump($this->formatter->getSitemapEnd()); |
||
| 67 | |||
| 68 | if (!$this->isSitemapIndexable()) { |
||
| 69 | return $sitemapContent; |
||
| 70 | } |
||
| 71 | |||
| 72 | if (count($this->sitemapIndexes)) { |
||
| 73 | $this->dumper->setFilename($this->originalFilename); |
||
| 74 | $this->dumper->dump($this->formatter->getSitemapIndexStart()); |
||
| 75 | foreach ($this->sitemapIndexes as $sitemapIndex) { |
||
| 76 | $this->dumper->dump($this->formatter->formatSitemapIndex($sitemapIndex)); |
||
| 77 | } |
||
| 78 | |||
| 79 | $this->dumper->dump($this->formatter->getSitemapIndexEnd()); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | public function add(Url $url) |
||
| 84 | { |
||
| 85 | if ($this->isSitemapIndexable() && $this->getCurrentSitemapIndex()->getUrlCount() >= $this->limit) { |
||
| 86 | $this->addSitemapIndex($this->createSitemapIndex()); |
||
| 87 | } |
||
| 88 | |||
| 89 | $loc = $url->getLoc(); |
||
| 90 | if (empty($loc)) { |
||
| 91 | throw new \InvalidArgumentException('The url MUST have a loc attribute'); |
||
| 92 | } |
||
| 93 | |||
| 94 | if ($this->baseHost !== null) { |
||
| 95 | if ($this->needHost($loc)) { |
||
| 96 | $url->setLoc($this->baseHost.$loc); |
||
| 97 | } |
||
| 98 | |||
| 99 | foreach ($url->getVideos() as $video) { |
||
| 100 | if ($this->needHost($video->getThumbnailLoc())) { |
||
| 101 | $video->setThumbnailLoc($this->baseHost.$video->getThumbnailLoc()); |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($this->needHost($video->getContentLoc())) { |
||
| 105 | $video->setContentLoc($this->baseHost.$video->getContentLoc()); |
||
| 106 | } |
||
| 107 | |||
| 108 | $player = $video->getPlayerLoc(); |
||
| 109 | if ($player !== null && $this->needHost($player['loc'])) { |
||
| 110 | $video->setPlayerLoc($this->baseHost.$player['loc'], $player['allow_embed'], $player['autoplay']); |
||
| 111 | } |
||
| 112 | |||
| 113 | $gallery = $video->getGalleryLoc(); |
||
| 114 | if ($gallery !== null && $this->needHost($gallery['loc'])) { |
||
| 115 | $video->setGalleryLoc($this->baseHost.$gallery['loc'], $gallery['title']); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | foreach ($url->getImages() as $image) { |
||
| 120 | if ($this->needHost($image->getLoc())) { |
||
| 121 | $image->setLoc($this->baseHost.$image->getLoc()); |
||
| 122 | } |
||
| 123 | |||
| 124 | if ($this->needHost($image->getLicense())) { |
||
| 125 | $image->setLicense($this->baseHost.$image->getLicense()); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->dumper->dump($this->formatter->formatUrl($url)); |
||
| 131 | |||
| 132 | if ($this->isSitemapIndexable()) { |
||
| 133 | $this->getCurrentSitemapIndex()->incrementUrl(); |
||
| 134 | } |
||
| 135 | |||
| 136 | return $this; |
||
| 137 | } |
||
| 138 | |||
| 139 | protected function needHost($url) |
||
| 140 | { |
||
| 141 | if ($url === null) { |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | return substr($url, 0, 4) !== 'http'; |
||
| 146 | } |
||
| 147 | |||
| 148 | protected function isSitemapIndexable() |
||
| 149 | { |
||
| 150 | return ($this->limit > 0 && $this->dumper instanceof DumperFileInterface && $this->formatter instanceof SitemapIndexFormatterInterface); |
||
| 151 | } |
||
| 152 | |||
| 153 | protected function createSitemapIndex() |
||
| 154 | { |
||
| 155 | $sitemapIndexFilename = $this->getSitemapIndexFilename($this->originalFilename); |
||
| 156 | $sitemapIndex = new SitemapIndex(); |
||
| 157 | $loc = DIRECTORY_SEPARATOR.basename($sitemapIndexFilename); |
||
| 158 | |||
| 159 | $sitemapIndex->setLastMod(new \DateTime()); |
||
| 160 | |||
| 161 | return $sitemapIndex; |
||
| 162 | } |
||
| 163 | |||
| 164 | protected function addSitemapIndex(SitemapIndex $sitemapIndex) |
||
| 165 | { |
||
| 166 | $nbSitemapIndexs = count($this->sitemapIndexes); |
||
| 167 | |||
| 168 | if ($nbSitemapIndexs > 0) { |
||
| 169 | $this->dumper->dump($this->formatter->getSitemapEnd()); |
||
| 170 | } |
||
| 171 | $sitemapIndexFilename = $this->getSitemapIndexFilename($this->originalFilename); |
||
| 172 | $this->dumper->setFilename($sitemapIndexFilename); |
||
| 173 | |||
| 174 | $this->sitemapIndexes[] = $sitemapIndex; |
||
| 175 | if ($nbSitemapIndexs > 0) { |
||
| 176 | $this->dumper->dump($this->formatter->getSitemapStart()); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | protected function getCurrentSitemapIndex() |
||
| 181 | { |
||
| 182 | return end($this->sitemapIndexes); |
||
| 183 | } |
||
| 184 | |||
| 185 | protected function getSitemapIndexFilename($filename) |
||
| 199 | } |
||
| 200 | } |