Total Complexity | 45 |
Total Lines | 187 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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; |
||
18 | protected $formatter; |
||
19 | protected $baseHost; |
||
20 | protected $limit = 0; |
||
21 | protected $sitemapIndexes = []; |
||
22 | protected $originalFilename; |
||
23 | |||
24 | public function __construct(DumperInterface $dumper, FormatterInterface $formatter, $baseHost = null, $limit = 0, RequestStack $requestStack) |
||
38 | } |
||
39 | } |
||
40 | } |
||
41 | |||
42 | protected function isSitemapIndexable() |
||
43 | { |
||
44 | return ($this->limit > 0 && $this->dumper instanceof DumperFileInterface && $this->formatter instanceof SitemapIndexFormatterInterface); |
||
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) |
||
123 | } |
||
124 | |||
125 | protected function createSitemapIndex() |
||
126 | { |
||
127 | $sitemapIndex = new SitemapIndex(); |
||
128 | $sitemapIndex->setLastmod(new \DateTime()); |
||
129 | |||
130 | return $sitemapIndex; |
||
131 | } |
||
132 | |||
133 | public function add(Url $url) |
||
134 | { |
||
135 | if ($this->isSitemapIndexable() && $this->getCurrentSitemapIndex()->getUrlCount() >= $this->limit) { |
||
136 | $this->addSitemapIndex($this->createSitemapIndex()); |
||
137 | } |
||
138 | |||
139 | $loc = $url->getLoc(); |
||
140 | if (empty($loc)) { |
||
141 | throw new \InvalidArgumentException('The url MUST have a loc attribute'); |
||
142 | } |
||
143 | |||
144 | if ($this->baseHost !== null) { |
||
145 | if ($this->needHost($loc)) { |
||
146 | $url->setLoc($this->baseHost.$loc); |
||
147 | } |
||
148 | |||
149 | foreach ($url->getVideos() as $video) { |
||
150 | if ($this->needHost($video->getThumbnailLoc())) { |
||
151 | $video->setThumbnailLoc($this->baseHost.$video->getThumbnailLoc()); |
||
152 | } |
||
153 | |||
154 | if ($this->needHost($video->getContentLoc())) { |
||
155 | $video->setContentLoc($this->baseHost.$video->getContentLoc()); |
||
156 | } |
||
157 | |||
158 | $player = $video->getPlayerLoc(); |
||
159 | if ($player !== null && $this->needHost($player['loc'])) { |
||
160 | $video->setPlayerLoc($this->baseHost.$player['loc'], $player['allow_embed'], $player['autoplay']); |
||
161 | } |
||
162 | |||
163 | $gallery = $video->getGalleryLoc(); |
||
164 | if ($gallery !== null && $this->needHost($gallery['loc'])) { |
||
165 | $video->setGalleryLoc($this->baseHost.$gallery['loc'], $gallery['title']); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | foreach ($url->getImages() as $image) { |
||
170 | if ($this->needHost($image->getLoc())) { |
||
171 | $image->setLoc($this->baseHost.$image->getLoc()); |
||
172 | } |
||
173 | |||
174 | if ($this->needHost($image->getLicense())) { |
||
175 | $image->setLicense($this->baseHost.$image->getLicense()); |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | |||
180 | $this->dumper->dump($this->formatter->formatUrl($url)); |
||
181 | |||
182 | if ($this->isSitemapIndexable()) { |
||
183 | $this->getCurrentSitemapIndex()->incrementUrl(); |
||
184 | } |
||
185 | |||
186 | return $this; |
||
187 | } |
||
188 | |||
189 | protected function getCurrentSitemapIndex() |
||
190 | { |
||
191 | return end($this->sitemapIndexes); |
||
192 | } |
||
193 | |||
194 | protected function needHost($url) |
||
201 | } |
||
202 | } |
||
203 |