Passed
Pull Request — v3 (#27)
by Rias
05:55
created
src/models/SitemapTemplate.php 1 patch
Spacing   +77 added lines, -77 removed lines patch added patch discarded remove patch
@@ -63,7 +63,7 @@  discard block
 block discarded – undo
63 63
      *
64 64
      * @return null|SitemapTemplate
65 65
      */
66
-    public static function create(array $config = [])
66
+    public static function create(array $config = [ ])
67 67
     {
68 68
         $defaults = [
69 69
             'path'       => 'sitemaps/<groupId:\d+>/<type:[-\w\.*]+>/<handle:[-\w\.*]+>/<siteId:\d+>/<file:[-\w\.*]+>',
@@ -110,32 +110,32 @@  discard block
 block discarded – undo
110 110
     /**
111 111
      * @inheritdoc
112 112
      */
113
-    public function render($params = []): string
113
+    public function render($params = [ ]): string
114 114
     {
115 115
         $cache = Craft::$app->getCache();
116
-        $groupId = $params['groupId'];
116
+        $groupId = $params[ 'groupId' ];
117 117
         /** @var SiteGroup $siteGroup */
118 118
         $siteGroup = Craft::$app->getSites()->getGroupById($groupId);
119 119
         $groupSiteIds = $siteGroup->getSiteIds();
120
-        $type = $params['type'];
121
-        $handle = $params['handle'];
122
-        $siteId = $params['siteId'];
120
+        $type = $params[ 'type' ];
121
+        $handle = $params[ 'handle' ];
122
+        $siteId = $params[ 'siteId' ];
123 123
         $duration = Seomatic::$devMode ? $this::DEVMODE_SITEMAP_CACHE_DURATION : $this::SITEMAP_CACHE_DURATION;
124 124
         $dependency = new TagDependency([
125 125
             'tags' => [
126 126
                 $this::GLOBAL_SITEMAP_CACHE_TAG,
127
-                $this::SITEMAP_CACHE_TAG.$handle.$siteId,
127
+                $this::SITEMAP_CACHE_TAG . $handle . $siteId,
128 128
             ],
129 129
         ]);
130 130
 
131
-        return $cache->getOrSet($this::CACHE_KEY.$groupId.$handle.$siteId, function () use ($type, $handle, $siteId, $groupSiteIds) {
131
+        return $cache->getOrSet($this::CACHE_KEY . $groupId . $handle . $siteId, function() use ($type, $handle, $siteId, $groupSiteIds) {
132 132
             Craft::info(
133
-                'Sitemap cache miss: '.$handle.'/'.$siteId,
133
+                'Sitemap cache miss: ' . $handle . '/' . $siteId,
134 134
                 __METHOD__
135 135
             );
136
-            $lines = [];
136
+            $lines = [ ];
137 137
             // Sitemap index XML header and opening tag
138
-            $lines[] = '<?xml version="1.0" encoding="UTF-8"?>';
138
+            $lines[ ] = '<?xml version="1.0" encoding="UTF-8"?>';
139 139
             // One sitemap entry for each element
140 140
             $metaBundle = Seomatic::$plugin->metaBundles->getMetaBundleBySourceHandle($type, $handle, $siteId);
141 141
             $multiSite = count($metaBundle->sourceAltSiteSettings) > 1;
@@ -150,7 +150,7 @@  discard block
 block discarded – undo
150 150
                     $urlsetLine .= ' xmlns:xhtml="http://www.w3.org/1999/xhtml"';
151 151
                 }
152 152
                 $urlsetLine .= '>';
153
-                $lines[] = $urlsetLine;
153
+                $lines[ ] = $urlsetLine;
154 154
                 // Handle each element type separately
155 155
                 switch ($metaBundle->sourceBundleType) {
156 156
                     case MetaBundles::SECTION_META_BUNDLE:
@@ -176,25 +176,25 @@  discard block
 block discarded – undo
176 176
                     $path = ($element->uri === '__home__') ? '' : $element->uri;
177 177
                     $url = UrlHelper::siteUrl($path, null, null, $metaBundle->sourceSiteId);
178 178
                     $dateUpdated = $element->dateUpdated ?? $element->dateCreated ?? new \DateTime;
179
-                    $lines[] = '  <url>';
179
+                    $lines[ ] = '  <url>';
180 180
                     // Standard sitemap key/values
181
-                    $lines[] = '    <loc>';
182
-                    $lines[] = '      '.$url;
183
-                    $lines[] = '    </loc>';
184
-                    $lines[] = '    <lastmod>';
185
-                    $lines[] = '      '.$dateUpdated->format(\DateTime::W3C);
186
-                    $lines[] = '    </lastmod>';
187
-                    $lines[] = '    <changefreq>';
188
-                    $lines[] = '      '.$metaBundle->metaSitemapVars->sitemapChangeFreq;
189
-                    $lines[] = '    </changefreq>';
190
-                    $lines[] = '    <priority>';
191
-                    $lines[] = '      '.$metaBundle->metaSitemapVars->sitemapPriority;
192
-                    $lines[] = '    </priority>';
181
+                    $lines[ ] = '    <loc>';
182
+                    $lines[ ] = '      ' . $url;
183
+                    $lines[ ] = '    </loc>';
184
+                    $lines[ ] = '    <lastmod>';
185
+                    $lines[ ] = '      ' . $dateUpdated->format(\DateTime::W3C);
186
+                    $lines[ ] = '    </lastmod>';
187
+                    $lines[ ] = '    <changefreq>';
188
+                    $lines[ ] = '      ' . $metaBundle->metaSitemapVars->sitemapChangeFreq;
189
+                    $lines[ ] = '    </changefreq>';
190
+                    $lines[ ] = '    <priority>';
191
+                    $lines[ ] = '      ' . $metaBundle->metaSitemapVars->sitemapPriority;
192
+                    $lines[ ] = '    </priority>';
193 193
                     // Handle alternate URLs if this is multi-site
194 194
                     if ($multiSite && $metaBundle->metaSitemapVars->sitemapAltLinks) {
195 195
                         /** @var  $altSiteSettings */
196 196
                         foreach ($metaBundle->sourceAltSiteSettings as $altSiteSettings) {
197
-                            if (in_array($altSiteSettings['siteId'], $groupSiteIds)) {
197
+                            if (in_array($altSiteSettings[ 'siteId' ], $groupSiteIds)) {
198 198
                                 $altElement = null;
199 199
                                 // Handle each element type separately
200 200
                                 switch ($metaBundle->sourceBundleType) {
@@ -202,7 +202,7 @@  discard block
 block discarded – undo
202 202
                                         $altElement = Entry::find()
203 203
                                             ->section($metaBundle->sourceHandle)
204 204
                                             ->id($element->id)
205
-                                            ->siteId($altSiteSettings['siteId'])
205
+                                            ->siteId($altSiteSettings[ 'siteId' ])
206 206
                                             ->enabledForSite(true)
207 207
                                             ->limit(1)
208 208
                                             ->one();
@@ -211,7 +211,7 @@  discard block
 block discarded – undo
211 211
                                     case MetaBundles::CATEGORYGROUP_META_BUNDLE:
212 212
                                         $altElement = Category::find()
213 213
                                             ->id($element->id)
214
-                                            ->siteId($altSiteSettings['siteId'])
214
+                                            ->siteId($altSiteSettings[ 'siteId' ])
215 215
                                             ->limit(1)
216 216
                                             ->enabledForSite(true)
217 217
                                             ->one();
@@ -219,9 +219,9 @@  discard block
 block discarded – undo
219 219
                                     // @todo: handle Commerce products
220 220
                                 }
221 221
                                 if ($altElement) {
222
-                                    $lines[] = '    <xhtml:link rel="alternate"'
223
-                                        .' hreflang="'.$altSiteSettings['language'].'"'
224
-                                        .' href="'.$altElement->url.'"'
222
+                                    $lines[ ] = '    <xhtml:link rel="alternate"'
223
+                                        .' hreflang="' . $altSiteSettings[ 'language' ] . '"'
224
+                                        .' href="' . $altElement->url . '"'
225 225
                                         .' />';
226 226
                                 }
227 227
                             }
@@ -236,7 +236,7 @@  discard block
 block discarded – undo
236 236
                             true
237 237
                         );
238 238
                         foreach ($assetFields as $assetField) {
239
-                            $assets = $element[$assetField]->all();
239
+                            $assets = $element[ $assetField ]->all();
240 240
                             foreach ($assets as $asset) {
241 241
                                 $this->assetSitemapItem($asset, $metaBundle, $lines);
242 242
                             }
@@ -248,19 +248,19 @@  discard block
 block discarded – undo
248 248
                             true
249 249
                         );
250 250
                         foreach ($matrixFields as $matrixField) {
251
-                            $matrixBlocks = $element[$matrixField]->all();
251
+                            $matrixBlocks = $element[ $matrixField ]->all();
252 252
                             foreach ($matrixBlocks as $matrixBlock) {
253 253
                                 $assetFields = FieldHelper::matrixFieldsOfType($matrixBlock, AssetsField::class);
254 254
                                 foreach ($assetFields as $assetField) {
255 255
                                     /** @var MatrixBlock $matrixBlock */
256
-                                    foreach ($matrixBlock[$assetField]->all() as $asset) {
256
+                                    foreach ($matrixBlock[ $assetField ]->all() as $asset) {
257 257
                                         $this->assetSitemapItem($asset, $metaBundle, $lines);
258 258
                                     }
259 259
                                 }
260 260
                             }
261 261
                         }
262 262
                     }
263
-                    $lines[] = '  </url>';
263
+                    $lines[ ] = '  </url>';
264 264
                     // Include links to any known file types in the assets fields
265 265
                     if ($metaBundle->metaSitemapVars->sitemapFiles) {
266 266
                         // Regular Assets fields
@@ -270,7 +270,7 @@  discard block
 block discarded – undo
270 270
                             true
271 271
                         );
272 272
                         foreach ($assetFields as $assetField) {
273
-                            $assets = $element[$assetField]->all();
273
+                            $assets = $element[ $assetField ]->all();
274 274
                             foreach ($assets as $asset) {
275 275
                                 $this->assetFilesSitemapLink($asset, $metaBundle, $lines);
276 276
                             }
@@ -282,12 +282,12 @@  discard block
 block discarded – undo
282 282
                             true
283 283
                         );
284 284
                         foreach ($matrixFields as $matrixField) {
285
-                            $matrixBlocks = $element[$matrixField]->all();
285
+                            $matrixBlocks = $element[ $matrixField ]->all();
286 286
                             /** @var MatrixBlock $matrixBlock */
287 287
                             foreach ($matrixBlocks as $matrixBlock) {
288 288
                                 $assetFields = FieldHelper::matrixFieldsOfType($matrixBlock, AssetsField::class);
289 289
                                 foreach ($assetFields as $assetField) {
290
-                                    foreach ($matrixBlock[$assetField]->all() as $asset) {
290
+                                    foreach ($matrixBlock[ $assetField ]->all() as $asset) {
291 291
                                         $this->assetFilesSitemapLink($asset, $metaBundle, $lines);
292 292
                                     }
293 293
                                 }
@@ -296,7 +296,7 @@  discard block
 block discarded – undo
296 296
                     }
297 297
                 }
298 298
                 // Sitemap index closing tag
299
-                $lines[] = '</urlset>';
299
+                $lines[ ] = '</urlset>';
300 300
             }
301 301
 
302 302
             return implode("\r\n", $lines);
@@ -312,9 +312,9 @@  discard block
 block discarded – undo
312 312
     public function invalidateCache(string $handle, int $siteId)
313 313
     {
314 314
         $cache = Craft::$app->getCache();
315
-        TagDependency::invalidate($cache, $this::SITEMAP_CACHE_TAG.$handle.$siteId);
315
+        TagDependency::invalidate($cache, $this::SITEMAP_CACHE_TAG . $handle . $siteId);
316 316
         Craft::info(
317
-            'Sitemap cache cleared: '.$handle,
317
+            'Sitemap cache cleared: ' . $handle,
318 318
             __METHOD__
319 319
         );
320 320
     }
@@ -329,39 +329,39 @@  discard block
 block discarded – undo
329 329
         if ($asset->enabledForSite) {
330 330
             switch ($asset->kind) {
331 331
                 case 'image':
332
-                    $lines[] = '    <image:image>';
333
-                    $lines[] = '      <image:loc>';
334
-                    $lines[] = '        '.$asset->getUrl();
335
-                    $lines[] = '      </image:loc>';
332
+                    $lines[ ] = '    <image:image>';
333
+                    $lines[ ] = '      <image:loc>';
334
+                    $lines[ ] = '        ' . $asset->getUrl();
335
+                    $lines[ ] = '      </image:loc>';
336 336
                     // Handle the dynamic field => property mappings
337 337
                     foreach ($metaBundle->metaSitemapVars->sitemapImageFieldMap as $row) {
338
-                        $fieldName = $row['field'] ?? '';
339
-                        $propName = $row['property'] ?? '';
340
-                        if (!empty($asset[$fieldName]) && !empty($propName)) {
341
-                            $lines[] = '      <image:'.$propName.'>';
342
-                            $lines[] = '        '.$asset[$fieldName];
343
-                            $lines[] = '      </image:'.$propName.'>';
338
+                        $fieldName = $row[ 'field' ] ?? '';
339
+                        $propName = $row[ 'property' ] ?? '';
340
+                        if (!empty($asset[ $fieldName ]) && !empty($propName)) {
341
+                            $lines[ ] = '      <image:' . $propName . '>';
342
+                            $lines[ ] = '        ' . $asset[ $fieldName ];
343
+                            $lines[ ] = '      </image:' . $propName . '>';
344 344
                         }
345 345
                     }
346
-                    $lines[] = '    </image:image>';
346
+                    $lines[ ] = '    </image:image>';
347 347
                     break;
348 348
 
349 349
                 case 'video':
350
-                    $lines[] = '    <video:video>';
351
-                    $lines[] = '      <video:content_loc>';
352
-                    $lines[] = '        '.$asset->getUrl();
353
-                    $lines[] = '      </video:content_loc>';
350
+                    $lines[ ] = '    <video:video>';
351
+                    $lines[ ] = '      <video:content_loc>';
352
+                    $lines[ ] = '        ' . $asset->getUrl();
353
+                    $lines[ ] = '      </video:content_loc>';
354 354
                     // Handle the dynamic field => property mappings
355 355
                     foreach ($metaBundle->metaSitemapVars->sitemapVideoFieldMap as $row) {
356
-                        $fieldName = $row['field'] ?? '';
357
-                        $propName = $row['property'] ?? '';
358
-                        if (!empty($asset[$fieldName]) && !empty($propName)) {
359
-                            $lines[] = '      <video:'.$propName.'>';
360
-                            $lines[] = '        '.$asset[$fieldName];
361
-                            $lines[] = '      </video:'.$propName.'>';
356
+                        $fieldName = $row[ 'field' ] ?? '';
357
+                        $propName = $row[ 'property' ] ?? '';
358
+                        if (!empty($asset[ $fieldName ]) && !empty($propName)) {
359
+                            $lines[ ] = '      <video:' . $propName . '>';
360
+                            $lines[ ] = '        ' . $asset[ $fieldName ];
361
+                            $lines[ ] = '      </video:' . $propName . '>';
362 362
                         }
363 363
                     }
364
-                    $lines[] = '    </video:video>';
364
+                    $lines[ ] = '    </video:video>';
365 365
                     break;
366 366
             }
367 367
         }
@@ -377,20 +377,20 @@  discard block
 block discarded – undo
377 377
         if ($asset->enabledForSite) {
378 378
             if (in_array($asset->kind, $this::FILE_TYPES)) {
379 379
                 $dateUpdated = $asset->dateUpdated ?? $asset->dateCreated ?? new \DateTime;
380
-                $lines[] = '  <url>';
381
-                $lines[] = '    <loc>';
382
-                $lines[] = '      '.$asset->getUrl();
383
-                $lines[] = '    </loc>';
384
-                $lines[] = '    <lastmod>';
385
-                $lines[] = '      '.$dateUpdated->format(\DateTime::W3C);
386
-                $lines[] = '    </lastmod>';
387
-                $lines[] = '    <changefreq>';
388
-                $lines[] = '      '.$metaBundle->metaSitemapVars->sitemapChangeFreq;
389
-                $lines[] = '    </changefreq>';
390
-                $lines[] = '    <priority>';
391
-                $lines[] = '      '.$metaBundle->metaSitemapVars->sitemapPriority;
392
-                $lines[] = '    </priority>';
393
-                $lines[] = '  </url>';
380
+                $lines[ ] = '  <url>';
381
+                $lines[ ] = '    <loc>';
382
+                $lines[ ] = '      ' . $asset->getUrl();
383
+                $lines[ ] = '    </loc>';
384
+                $lines[ ] = '    <lastmod>';
385
+                $lines[ ] = '      ' . $dateUpdated->format(\DateTime::W3C);
386
+                $lines[ ] = '    </lastmod>';
387
+                $lines[ ] = '    <changefreq>';
388
+                $lines[ ] = '      ' . $metaBundle->metaSitemapVars->sitemapChangeFreq;
389
+                $lines[ ] = '    </changefreq>';
390
+                $lines[ ] = '    <priority>';
391
+                $lines[ ] = '      ' . $metaBundle->metaSitemapVars->sitemapPriority;
392
+                $lines[ ] = '    </priority>';
393
+                $lines[ ] = '  </url>';
394 394
             }
395 395
         }
396 396
     }
Please login to merge, or discard this patch.