| Total Complexity | 48 |
| Total Lines | 210 |
| 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::useHttps($request) ? 'https' : 'http').'://'.$request->server->get('HTTP_HOST'); |
||
| 35 | } |
||
| 36 | $this->limit = $limit; |
||
| 37 | if ($this->isSitemapIndexable()) { |
||
| 38 | if ($dumper instanceof DumperFileInterface) { |
||
| 39 | $this->originalFilename = $dumper->getFilename(); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | protected function isSitemapIndexable() |
||
| 45 | { |
||
| 46 | return ($this->limit > 0 && $this->dumper instanceof DumperFileInterface && $this->formatter instanceof SitemapIndexFormatterInterface); |
||
| 47 | } |
||
| 48 | |||
| 49 | public function addProvider(ProviderInterface $provider) |
||
| 50 | { |
||
| 51 | $this->providers[] = $provider; |
||
| 52 | |||
| 53 | return $this; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function setDumper(DumperInterface $dumper) |
||
| 57 | { |
||
| 58 | $this->dumper = $dumper; |
||
| 59 | |||
| 60 | return $this; |
||
| 61 | } |
||
| 62 | |||
| 63 | public function build() |
||
| 64 | { |
||
| 65 | if ($this->isSitemapIndexable()) { |
||
| 66 | $this->addSitemapIndex($this->createSitemapIndex()); |
||
| 67 | } |
||
| 68 | |||
| 69 | $this->dumper->dump($this->formatter->getSitemapStart()); |
||
| 70 | |||
| 71 | foreach ($this->providers as $provider) { |
||
| 72 | $provider->populate($this); |
||
| 73 | } |
||
| 74 | |||
| 75 | $sitemapContent = $this->dumper->dump($this->formatter->getSitemapEnd()); |
||
| 76 | |||
| 77 | if (!$this->isSitemapIndexable()) { |
||
| 78 | return $sitemapContent; |
||
| 79 | } |
||
| 80 | |||
| 81 | if (\count($this->sitemapIndexes)) { |
||
| 82 | $this->dumper->setFilename($this->originalFilename); |
||
| 83 | |||
| 84 | $this->dumper->dump($this->formatter->getSitemapIndexStart()); |
||
| 85 | foreach ($this->sitemapIndexes as $sitemapIndex) { |
||
| 86 | $this->dumper->dump($this->formatter->formatSitemapIndex($sitemapIndex)); |
||
| 87 | } |
||
| 88 | |||
| 89 | $this->dumper->dump($this->formatter->getSitemapIndexEnd()); |
||
| 90 | } |
||
| 91 | |||
| 92 | return null; |
||
| 93 | } |
||
| 94 | |||
| 95 | protected function addSitemapIndex(SitemapIndex $sitemapIndex) |
||
| 96 | { |
||
| 97 | $nbSitemapIndexs = \count($this->sitemapIndexes); |
||
| 98 | |||
| 99 | if ($nbSitemapIndexs > 0) { |
||
| 100 | $this->dumper->dump($this->formatter->getSitemapEnd()); |
||
| 101 | } |
||
| 102 | $sitemapIndexFilename = $this->getSitemapIndexFilename($this->originalFilename); |
||
| 103 | $this->dumper->setFilename($sitemapIndexFilename); |
||
|
|
|||
| 104 | |||
| 105 | $this->sitemapIndexes[] = $sitemapIndex; |
||
| 106 | if ($nbSitemapIndexs > 0) { |
||
| 107 | $this->dumper->dump($this->formatter->getSitemapStart()); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | protected function getSitemapIndexFilename($filename) |
||
| 125 | } |
||
| 126 | |||
| 127 | protected function createSitemapIndex() |
||
| 128 | { |
||
| 133 | } |
||
| 134 | |||
| 135 | private function addIndex() |
||
| 136 | { |
||
| 137 | if ($this->isSitemapIndexable() && $this->getCurrentSitemapIndex()->getUrlCount() >= $this->limit) { |
||
| 138 | $this->addSitemapIndex($this->createSitemapIndex()); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | private function parseBaseHost(Url $url, $loc) |
||
| 143 | { |
||
| 144 | if ($this->baseHost !== null) { |
||
| 145 | if ($this->needHost($loc)) { |
||
| 146 | $url->setLoc($this->baseHost.$loc); |
||
| 147 | } |
||
| 148 | |||
| 149 | $this->parseVideo($url); |
||
| 150 | $this->parseImage($url); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | private function setEndIndex() |
||
| 155 | { |
||
| 156 | if ($this->isSitemapIndexable()) { |
||
| 157 | $this->getCurrentSitemapIndex()->incrementUrl(); |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | public function add(Url $url) |
||
| 162 | { |
||
| 163 | $this->addIndex(); |
||
| 164 | |||
| 165 | $loc = $url->getLoc(); |
||
| 166 | if (empty($loc)) { |
||
| 167 | throw new \InvalidArgumentException('The url MUST have a loc attribute'); |
||
| 168 | } |
||
| 169 | |||
| 170 | $this->parseBaseHost($url, $loc); |
||
| 171 | $this->dumper->dump($this->formatter->formatUrl($url)); |
||
| 172 | $this->setEndIndex(); |
||
| 173 | |||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | protected function parseVideo(Url $url) |
||
| 197 | } |
||
| 198 | } |
||
| 199 | } |
||
| 200 | |||
| 201 | protected function parseImage(Url $url) |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | protected function getCurrentSitemapIndex() |
||
| 216 | { |
||
| 217 | return end($this->sitemapIndexes); |
||
| 218 | } |
||
| 219 | |||
| 220 | protected function needHost($url) |
||
| 227 | } |
||
| 228 | } |
||
| 229 |