| Total Complexity | 47 |
| Total Lines | 208 |
| 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 |
||
| 17 | class Sitemap |
||
| 18 | { |
||
| 19 | protected $providers = []; |
||
| 20 | protected $dumper; |
||
| 21 | protected $formatter; |
||
| 22 | protected $baseHost; |
||
| 23 | protected $limit = 0; |
||
| 24 | protected $sitemapIndexes = []; |
||
| 25 | protected $originalFilename; |
||
| 26 | |||
| 27 | public function __construct(DumperInterface $dumper, FormatterInterface $formatter, $baseHost = null, $limit = 0, RequestStack $requestStack) |
||
| 28 | { |
||
| 29 | $this->dumper = $dumper; |
||
| 30 | $this->formatter = $formatter; |
||
| 31 | $this->baseHost = $baseHost; |
||
| 32 | if ($this->baseHost === null && PHP_SAPI !== 'cli') { |
||
| 33 | $request = $requestStack->getCurrentRequest(); |
||
| 34 | $this->baseHost = Helper::getSchema($request).$request->server->get('HTTP_HOST'); |
||
| 35 | } |
||
| 36 | $this->limit = $limit; |
||
| 37 | if ($this->isSitemapIndexable() && $dumper instanceof DumperFileInterface) { |
||
| 38 | $this->originalFilename = $dumper->getFilename(); |
||
| 39 | } |
||
| 40 | } |
||
| 41 | |||
| 42 | protected function isSitemapIndexable() |
||
| 45 | } |
||
| 46 | |||
| 47 | public function addProvider(ProviderInterface $provider) |
||
| 48 | { |
||
| 49 | $this->providers[] = $provider; |
||
| 50 | |||
| 51 | return $this; |
||
| 52 | } |
||
| 53 | |||
| 54 | public function setDumper(DumperInterface $dumper) |
||
| 55 | { |
||
| 56 | $this->dumper = $dumper; |
||
| 57 | |||
| 58 | return $this; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function build() |
||
| 62 | { |
||
| 63 | if ($this->isSitemapIndexable()) { |
||
| 64 | $this->addSitemapIndex($this->createSitemapIndex()); |
||
| 65 | } |
||
| 66 | |||
| 67 | $this->dumper->dump($this->formatter->getSitemapStart()); |
||
| 68 | |||
| 69 | foreach ($this->providers as $provider) { |
||
| 70 | $provider->populate($this); |
||
| 71 | } |
||
| 72 | |||
| 73 | $sitemapContent = $this->dumper->dump($this->formatter->getSitemapEnd()); |
||
| 74 | |||
| 75 | if (!$this->isSitemapIndexable()) { |
||
| 76 | return $sitemapContent; |
||
| 77 | } |
||
| 78 | |||
| 79 | if (\count($this->sitemapIndexes)) { |
||
| 80 | $this->dumper->setFilename($this->originalFilename); |
||
| 81 | |||
| 82 | $this->dumper->dump($this->formatter->getSitemapIndexStart()); |
||
| 83 | foreach ($this->sitemapIndexes as $sitemapIndex) { |
||
| 84 | $this->dumper->dump($this->formatter->formatSitemapIndex($sitemapIndex)); |
||
| 85 | } |
||
| 86 | |||
| 87 | $this->dumper->dump($this->formatter->getSitemapIndexEnd()); |
||
| 88 | } |
||
| 89 | |||
| 90 | return null; |
||
| 91 | } |
||
| 92 | |||
| 93 | protected function addSitemapIndex(SitemapIndex $sitemapIndex) |
||
| 94 | { |
||
| 95 | $nbSitemapIndexs = \count($this->sitemapIndexes); |
||
| 96 | |||
| 97 | if ($nbSitemapIndexs > 0) { |
||
| 98 | $this->dumper->dump($this->formatter->getSitemapEnd()); |
||
| 99 | } |
||
| 100 | $sitemapIndexFilename = $this->getSitemapIndexFilename($this->originalFilename); |
||
| 101 | $this->dumper->setFilename($sitemapIndexFilename); |
||
|
|
|||
| 102 | |||
| 103 | $this->sitemapIndexes[] = $sitemapIndex; |
||
| 104 | if ($nbSitemapIndexs > 0) { |
||
| 105 | $this->dumper->dump($this->formatter->getSitemapStart()); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | protected function getSitemapIndexFilename($filename) |
||
| 110 | { |
||
| 111 | $sitemapIndexFilename = basename($filename); |
||
| 112 | $index = \count($this->sitemapIndexes) + 1; |
||
| 113 | $extPosition = strrpos($sitemapIndexFilename, '.'); |
||
| 114 | if ($extPosition !== false) { |
||
| 115 | $sitemapIndexFilename = substr($sitemapIndexFilename, 0, $extPosition).'-'.$index.substr($sitemapIndexFilename, $extPosition); |
||
| 116 | } else { |
||
| 117 | $sitemapIndexFilename .= '-'.$index; |
||
| 118 | } |
||
| 119 | |||
| 120 | $sitemapIndexFilename = \dirname($filename).DIRECTORY_SEPARATOR.$sitemapIndexFilename; |
||
| 121 | |||
| 122 | return $sitemapIndexFilename; |
||
| 123 | } |
||
| 124 | |||
| 125 | protected function createSitemapIndex() |
||
| 126 | { |
||
| 127 | $sitemapIndex = new SitemapIndex(); |
||
| 128 | $sitemapIndex->setLastmod(new \DateTime()); |
||
| 129 | |||
| 130 | return $sitemapIndex; |
||
| 131 | } |
||
| 132 | |||
| 133 | private function addIndex() |
||
| 134 | { |
||
| 135 | if ($this->isSitemapIndexable() && $this->getCurrentSitemapIndex()->getUrlCount() >= $this->limit) { |
||
| 136 | $this->addSitemapIndex($this->createSitemapIndex()); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | private function parseBaseHost(Url $url, $loc) |
||
| 141 | { |
||
| 142 | if ($this->baseHost !== null) { |
||
| 143 | if ($this->needHost($loc)) { |
||
| 144 | $url->setLoc($this->baseHost.$loc); |
||
| 145 | } |
||
| 146 | |||
| 147 | $this->parseVideo($url); |
||
| 148 | $this->parseImage($url); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | private function setEndIndex() |
||
| 153 | { |
||
| 154 | if ($this->isSitemapIndexable()) { |
||
| 155 | $this->getCurrentSitemapIndex()->incrementUrl(); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | public function add(Url $url) |
||
| 160 | { |
||
| 161 | $this->addIndex(); |
||
| 162 | |||
| 163 | $loc = $url->getLoc(); |
||
| 164 | if (empty($loc)) { |
||
| 165 | throw new \InvalidArgumentException('The url MUST have a loc attribute'); |
||
| 166 | } |
||
| 167 | |||
| 168 | $this->parseBaseHost($url, $loc); |
||
| 169 | $this->dumper->dump($this->formatter->formatUrl($url)); |
||
| 170 | $this->setEndIndex(); |
||
| 171 | |||
| 172 | return $this; |
||
| 173 | } |
||
| 174 | |||
| 175 | protected function parseVideo(Url $url) |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | protected function parseImage(Url $url) |
||
| 200 | { |
||
| 201 | foreach ($url->getImages() as $image) { |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | protected function getCurrentSitemapIndex() |
||
| 214 | { |
||
| 215 | return end($this->sitemapIndexes); |
||
| 216 | } |
||
| 217 | |||
| 218 | protected function needHost($url) |
||
| 225 | } |
||
| 226 | } |
||
| 227 |