| Total Complexity | 47 |
| Total Lines | 198 |
| 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 |
||
| 16 | class Sitemap |
||
| 17 | { |
||
| 18 | protected $providers = []; |
||
| 19 | protected $dumper; |
||
| 20 | protected $formatter; |
||
| 21 | protected $baseHost; |
||
| 22 | protected $limit = 0; |
||
| 23 | protected $sitemapIndexes = []; |
||
| 24 | protected $originalFilename; |
||
| 25 | |||
| 26 | public function __construct(DumperInterface $dumper, FormatterInterface $formatter, $baseHost = null, $limit = 0, RequestStack $requestStack) |
||
| 27 | { |
||
| 28 | $this->dumper = $dumper; |
||
| 29 | $this->formatter = $formatter; |
||
| 30 | $this->baseHost = $baseHost; |
||
| 31 | if ($this->baseHost === null && PHP_SAPI !== 'cli') { |
||
| 32 | $request = $requestStack->getCurrentRequest(); |
||
| 33 | $useHttps = $request->server->get('HTTPS') || ($request->server->get('HTTP_X_FORWARDED_PROTO') && $request->server->get('HTTP_X_FORWARDED_PROTO') === 'https'); |
||
| 34 | $this->baseHost = ($useHttps ? '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) |
||
| 112 | { |
||
| 113 | $sitemapIndexFilename = basename($filename); |
||
| 114 | $index = \count($this->sitemapIndexes) + 1; |
||
| 115 | $extPosition = strrpos($sitemapIndexFilename, '.'); |
||
| 116 | if ($extPosition !== false) { |
||
| 117 | $sitemapIndexFilename = substr($sitemapIndexFilename, 0, $extPosition).'-'.$index.substr($sitemapIndexFilename, $extPosition); |
||
| 118 | } else { |
||
| 119 | $sitemapIndexFilename .= '-'.$index; |
||
| 120 | } |
||
| 121 | |||
| 122 | $sitemapIndexFilename = \dirname($filename).DIRECTORY_SEPARATOR.$sitemapIndexFilename; |
||
| 123 | |||
| 124 | return $sitemapIndexFilename; |
||
| 125 | } |
||
| 126 | |||
| 127 | protected function createSitemapIndex() |
||
| 128 | { |
||
| 129 | $sitemapIndex = new SitemapIndex(); |
||
| 130 | $sitemapIndex->setLastmod(new \DateTime()); |
||
| 131 | |||
| 132 | return $sitemapIndex; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function add(Url $url) |
||
| 136 | { |
||
| 137 | if ($this->isSitemapIndexable() && $this->getCurrentSitemapIndex()->getUrlCount() >= $this->limit) { |
||
| 138 | $this->addSitemapIndex($this->createSitemapIndex()); |
||
| 139 | } |
||
| 140 | |||
| 141 | $loc = $url->getLoc(); |
||
| 142 | if (empty($loc)) { |
||
| 143 | throw new \InvalidArgumentException('The url MUST have a loc attribute'); |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($this->baseHost !== null) { |
||
| 147 | if ($this->needHost($loc)) { |
||
| 148 | $url->setLoc($this->baseHost.$loc); |
||
| 149 | } |
||
| 150 | |||
| 151 | $this->parseVideo($url); |
||
| 152 | $this->parseImage($url); |
||
| 153 | } |
||
| 154 | |||
| 155 | $this->dumper->dump($this->formatter->formatUrl($url)); |
||
| 156 | |||
| 157 | if ($this->isSitemapIndexable()) { |
||
| 158 | $this->getCurrentSitemapIndex()->incrementUrl(); |
||
| 159 | } |
||
| 160 | |||
| 161 | return $this; |
||
| 162 | } |
||
| 163 | |||
| 164 | protected function parseVideo(Url $url) |
||
| 184 | } |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | protected function parseImage(Url $url) |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 202 | protected function getCurrentSitemapIndex() |
||
| 203 | { |
||
| 204 | return end($this->sitemapIndexes); |
||
| 205 | } |
||
| 206 | |||
| 207 | protected function needHost($url) |
||
| 214 | } |
||
| 215 | } |
||
| 216 |