Passed
Push — develop ( a5d855...47c333 )
by Andrew
09:28 queued 04:43
created
src/services/Redirects.php 1 patch
Spacing   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -168,7 +168,7 @@  discard block
 block discarded – undo
168 168
                 Craft::t(
169 169
                     'retour',
170 170
                     '404 full URL: {fullUrl}, 404 path only: {pathOnly}',
171
-                    ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
171
+                    [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
172 172
                 ),
173 173
                 __METHOD__
174 174
             );
@@ -202,7 +202,7 @@  discard block
 block discarded – undo
202 202
         $response = Craft::$app->getResponse();
203 203
         if ($redirect !== null) {
204 204
             // Figure out what type of source matching was done
205
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
205
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
206 206
             switch ($redirectSrcMatch) {
207 207
                 case 'pathonly':
208 208
                     $url = $pathOnly;
@@ -214,11 +214,11 @@  discard block
 block discarded – undo
214 214
                     $url = $pathOnly;
215 215
                     break;
216 216
             }
217
-            $dest = $redirect['redirectDestUrl'];
217
+            $dest = $redirect[ 'redirectDestUrl' ];
218 218
             // If this isn't a full URL, make it one based on the appropriate site
219 219
             if (!UrlHelper::isFullUrl($dest)) {
220 220
                 try {
221
-                    $dest = UrlHelper::siteUrl($dest, null, null, $redirect['siteId'] ?? null);
221
+                    $dest = UrlHelper::siteUrl($dest, null, null, $redirect[ 'siteId' ] ?? null);
222 222
                 } catch (\yii\base\Exception $e) {
223 223
                 }
224 224
             }
@@ -228,17 +228,17 @@  discard block
 block discarded – undo
228 228
                     $dest .= '?' . $request->getQueryStringWithoutPath();
229 229
                 }
230 230
             }
231
-            $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
231
+            $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
232 232
             // Parse reference tags for exact matches
233 233
             if ($redirectMatchType === 'exactmatch') {
234
-                $dest = Craft::$app->elements->parseRefs($dest, $redirect['siteId'] ?? null);
234
+                $dest = Craft::$app->elements->parseRefs($dest, $redirect[ 'siteId' ] ?? null);
235 235
             }
236
-            $status = $redirect['redirectHttpCode'];
236
+            $status = $redirect[ 'redirectHttpCode' ];
237 237
             Craft::info(
238 238
                 Craft::t(
239 239
                     'retour',
240 240
                     'Redirecting {url} to {dest} with status {status}',
241
-                    ['url' => $url, 'dest' => $dest, 'status' => $status]
241
+                    [ 'url' => $url, 'dest' => $dest, 'status' => $status ]
242 242
                 ),
243 243
                 __METHOD__
244 244
             );
@@ -320,13 +320,13 @@  discard block
 block discarded – undo
320 320
     public function getRedirectFromCache($url, int $siteId = 0)
321 321
     {
322 322
         $cache = Craft::$app->getCache();
323
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
323
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
324 324
         $redirect = $cache->get($cacheKey);
325 325
         Craft::info(
326 326
             Craft::t(
327 327
                 'retour',
328 328
                 'Cached redirect hit for {url}',
329
-                ['url' => $url]
329
+                [ 'url' => $url ]
330 330
             ),
331 331
             __METHOD__
332 332
         );
@@ -348,12 +348,12 @@  discard block
 block discarded – undo
348 348
         } catch (SiteNotFoundException $e) {
349 349
             $siteId = 1;
350 350
         }
351
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
351
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
352 352
         // Create the dependency tags
353 353
         $dependency = new TagDependency([
354 354
             'tags' => [
355 355
                 $this::GLOBAL_REDIRECTS_CACHE_TAG,
356
-                $this::GLOBAL_REDIRECTS_CACHE_TAG.$siteId,
356
+                $this::GLOBAL_REDIRECTS_CACHE_TAG . $siteId,
357 357
             ],
358 358
         ]);
359 359
         $cache->set($cacheKey, $redirect, Retour::$cacheDuration, $dependency);
@@ -361,7 +361,7 @@  discard block
 block discarded – undo
361 361
             Craft::t(
362 362
                 'retour',
363 363
                 'Cached redirect saved for {url}',
364
-                ['url' => $url]
364
+                [ 'url' => $url ]
365 365
             ),
366 366
             __METHOD__
367 367
         );
@@ -391,8 +391,8 @@  discard block
 block discarded – undo
391 391
         // Iterate through the redirects
392 392
         foreach ($redirects as $redirect) {
393 393
             // Figure out what type of source matching to do
394
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
395
-            $redirectEnabled = (bool)$redirect['enabled'];
394
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
395
+            $redirectEnabled = (bool) $redirect[ 'enabled' ];
396 396
             if ($redirectEnabled === true) {
397 397
                 switch ($redirectSrcMatch) {
398 398
                     case 'pathonly':
@@ -405,11 +405,11 @@  discard block
 block discarded – undo
405 405
                         $url = $pathOnly;
406 406
                         break;
407 407
                 }
408
-                $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
408
+                $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
409 409
                 switch ($redirectMatchType) {
410 410
                     // Do a straight up match
411 411
                     case 'exactmatch':
412
-                        if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
412
+                        if (strcasecmp($redirect[ 'redirectSrcUrlParsed' ], $url) === 0) {
413 413
                             $this->incrementRedirectHitCount($redirect);
414 414
                             $this->saveRedirectToCache($url, $redirect);
415 415
 
@@ -419,15 +419,15 @@  discard block
 block discarded – undo
419 419
 
420 420
                     // Do a regex match
421 421
                     case 'regexmatch':
422
-                        $matchRegEx = '`'.$redirect['redirectSrcUrlParsed'].'`i';
422
+                        $matchRegEx = '`' . $redirect[ 'redirectSrcUrlParsed' ] . '`i';
423 423
                         try {
424 424
                             if (preg_match($matchRegEx, $url) === 1) {
425 425
                                 $this->incrementRedirectHitCount($redirect);
426 426
                                 // If we're not associated with an EntryID, handle capture group replacement
427
-                                if ((int)$redirect['associatedElementId'] === 0) {
428
-                                    $redirect['redirectDestUrl'] = preg_replace(
427
+                                if ((int) $redirect[ 'associatedElementId' ] === 0) {
428
+                                    $redirect[ 'redirectDestUrl' ] = preg_replace(
429 429
                                         $matchRegEx,
430
-                                        $redirect['redirectDestUrl'],
430
+                                        $redirect[ 'redirectDestUrl' ],
431 431
                                         $url
432 432
                                     );
433 433
                                 }
@@ -437,7 +437,7 @@  discard block
 block discarded – undo
437 437
                             }
438 438
                         } catch (\Exception $e) {
439 439
                             // That's fine
440
-                            Craft::error('Invalid Redirect Regex: '.$matchRegEx, __METHOD__);
440
+                            Craft::error('Invalid Redirect Regex: ' . $matchRegEx, __METHOD__);
441 441
                         }
442 442
 
443 443
                         break;
@@ -451,7 +451,7 @@  discard block
 block discarded – undo
451 451
                                     'redirect' => &$redirect,
452 452
                                 ],
453 453
                             ];
454
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
454
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
455 455
                             if ($result) {
456 456
                                 $this->incrementRedirectHitCount($redirect);
457 457
                                 $this->saveRedirectToCache($url, $redirect);
@@ -478,7 +478,7 @@  discard block
 block discarded – undo
478 478
             Craft::t(
479 479
                 'retour',
480 480
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
481
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
481
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
482 482
             ),
483 483
             __METHOD__
484 484
         );
@@ -523,7 +523,7 @@  discard block
 block discarded – undo
523 523
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
524 524
             /** @var Plugin $plugin */
525 525
             if (method_exists($plugin, 'retourMatch')) {
526
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
526
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
527 527
             }
528 528
         }
529 529
 
@@ -544,12 +544,12 @@  discard block
 block discarded – undo
544 544
         }
545 545
         // Query the db table
546 546
         $query = (new Query())
547
-            ->from(['{{%retour_static_redirects}}'])
547
+            ->from([ '{{%retour_static_redirects}}' ])
548 548
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
549 549
         if ($siteId) {
550 550
             $query
551
-                ->where(['siteId' => $siteId])
552
-                ->orWhere(['siteId' => null]);
551
+                ->where([ 'siteId' => $siteId ])
552
+                ->orWhere([ 'siteId' => null ]);
553 553
         }
554 554
         if ($limit) {
555 555
             $query->limit($limit);
@@ -572,8 +572,8 @@  discard block
 block discarded – undo
572 572
     {
573 573
         // Query the db table
574 574
         $redirect = (new Query())
575
-            ->from(['{{%retour_static_redirects}}'])
576
-            ->where(['id' => $id])
575
+            ->from([ '{{%retour_static_redirects}}' ])
576
+            ->where([ 'id' => $id ])
577 577
             ->one();
578 578
 
579 579
         return $redirect;
@@ -591,16 +591,16 @@  discard block
 block discarded – undo
591 591
     {
592 592
         // Query the db table
593 593
         $query = (new Query())
594
-            ->from(['{{%retour_static_redirects}}'])
595
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
594
+            ->from([ '{{%retour_static_redirects}}' ])
595
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
596 596
             ;
597 597
         if ($siteId) {
598 598
             $query
599
-                ->andWhere(['or', [
599
+                ->andWhere([ 'or', [
600 600
                     'siteId' => $siteId,
601 601
                 ], [
602 602
                     'siteId' => null,
603
-                ]]);
603
+                ] ]);
604 604
         }
605 605
         $redirect = $query->one();
606 606
 
@@ -642,13 +642,13 @@  discard block
 block discarded – undo
642 642
     {
643 643
         if ($redirectConfig !== null) {
644 644
             $db = Craft::$app->getDb();
645
-            $redirectConfig['hitCount']++;
646
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
645
+            $redirectConfig[ 'hitCount' ]++;
646
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
647 647
             Craft::debug(
648 648
                 Craft::t(
649 649
                     'retour',
650 650
                     'Incrementing statistics for: {redirect}',
651
-                    ['redirect' => print_r($redirectConfig, true)]
651
+                    [ 'redirect' => print_r($redirectConfig, true) ]
652 652
                 ),
653 653
                 __METHOD__
654 654
             );
@@ -657,14 +657,14 @@  discard block
 block discarded – undo
657 657
                 $rowsAffected = $db->createCommand()->update(
658 658
                     '{{%retour_static_redirects}}',
659 659
                     [
660
-                        'hitCount' => $redirectConfig['hitCount'],
661
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
660
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
661
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
662 662
                     ],
663 663
                     [
664
-                        'id' => $redirectConfig['id'],
664
+                        'id' => $redirectConfig[ 'id' ],
665 665
                     ]
666 666
                 )->execute();
667
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
667
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
668 668
             } catch (\Exception $e) {
669 669
                 Craft::error($e->getMessage(), __METHOD__);
670 670
             }
@@ -683,7 +683,7 @@  discard block
 block discarded – undo
683 683
                 Craft::t(
684 684
                     'retour',
685 685
                     'Error validating redirect {id}: {errors}',
686
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
686
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
687 687
                 ),
688 688
                 __METHOD__
689 689
             );
@@ -693,32 +693,32 @@  discard block
 block discarded – undo
693 693
         // Get the validated model attributes and save them to the db
694 694
         $redirectConfig = $redirect->getAttributes();
695 695
         // 0 for a siteId needs to be converted to null
696
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
697
-            $redirectConfig['siteId'] = null;
696
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
697
+            $redirectConfig[ 'siteId' ] = null;
698 698
         }
699 699
         // Throw an event to before saving the redirect
700 700
         $db = Craft::$app->getDb();
701 701
         // See if a redirect exists with this source URL already
702
-        if ((int)$redirectConfig['id'] === 0) {
702
+        if ((int) $redirectConfig[ 'id' ] === 0) {
703 703
             // Query the db table
704 704
             $redirect = (new Query())
705
-                ->from(['{{%retour_static_redirects}}'])
706
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
707
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
705
+                ->from([ '{{%retour_static_redirects}}' ])
706
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
707
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
708 708
                 ->one();
709 709
             // If it exists, update it rather than having duplicates
710 710
             if (!empty($redirect)) {
711
-                $redirectConfig['id'] = $redirect['id'];
711
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
712 712
             }
713 713
         }
714 714
         // Trigger a 'beforeSaveRedirect' event
715
-        $isNew = (int)$redirectConfig['id'] === 0;
715
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
716 716
         $event = new RedirectEvent([
717 717
             'isNew' => $isNew,
718
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
719
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
720
-            'matchType' => $redirectConfig['redirectSrcMatch'],
721
-            'redirectType' => $redirectConfig['redirectHttpCode'],
718
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
719
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
720
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
721
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
722 722
         ]);
723 723
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
724 724
         if (!$event->isValid) {
@@ -730,7 +730,7 @@  discard block
 block discarded – undo
730 730
                 Craft::t(
731 731
                     'retour',
732 732
                     'Updating existing redirect: {redirect}',
733
-                    ['redirect' => print_r($redirectConfig, true)]
733
+                    [ 'redirect' => print_r($redirectConfig, true) ]
734 734
                 ),
735 735
                 __METHOD__
736 736
             );
@@ -740,7 +740,7 @@  discard block
 block discarded – undo
740 740
                     '{{%retour_static_redirects}}',
741 741
                     $redirectConfig,
742 742
                     [
743
-                        'id' => $redirectConfig['id'],
743
+                        'id' => $redirectConfig[ 'id' ],
744 744
                     ]
745 745
                 )->execute();
746 746
             } catch (Exception $e) {
@@ -751,11 +751,11 @@  discard block
 block discarded – undo
751 751
                 Craft::t(
752 752
                     'retour',
753 753
                     'Creating new redirect: {redirect}',
754
-                    ['redirect' => print_r($redirectConfig, true)]
754
+                    [ 'redirect' => print_r($redirectConfig, true) ]
755 755
                 ),
756 756
                 __METHOD__
757 757
             );
758
-            unset($redirectConfig['id']);
758
+            unset($redirectConfig[ 'id' ]);
759 759
             // Create a new record
760 760
             try {
761 761
                 $db->createCommand()->insert(
@@ -768,15 +768,15 @@  discard block
 block discarded – undo
768 768
         }
769 769
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
770 770
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
771
-            $redirectConfig['redirectDestUrl'],
772
-            $redirectConfig['siteId']
771
+            $redirectConfig[ 'redirectDestUrl' ],
772
+            $redirectConfig[ 'siteId' ]
773 773
         );
774 774
         if ($testRedirectConfig !== null) {
775 775
             Craft::debug(
776 776
                 Craft::t(
777 777
                     'retour',
778 778
                     'Deleting redirect to prevent a loop: {redirect}',
779
-                    ['redirect' => print_r($testRedirectConfig, true)]
779
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
780 780
                 ),
781 781
                 __METHOD__
782 782
             );
@@ -784,7 +784,7 @@  discard block
 block discarded – undo
784 784
             try {
785 785
                 $db->createCommand()->delete(
786 786
                     '{{%retour_static_redirects}}',
787
-                    ['id' => $testRedirectConfig['id']]
787
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
788 788
                 )->execute();
789 789
             } catch (Exception $e) {
790 790
                 Craft::error($e->getMessage(), __METHOD__);
@@ -817,17 +817,17 @@  discard block
 block discarded – undo
817 817
      */
818 818
     public function excludeUri($uri): bool
819 819
     {
820
-        $uri = '/'.ltrim($uri, '/');
820
+        $uri = '/' . ltrim($uri, '/');
821 821
         if (!empty(Retour::$settings->excludePatterns)) {
822 822
             foreach (Retour::$settings->excludePatterns as $excludePattern) {
823
-                $pattern = '`'.$excludePattern['pattern'].'`i';
823
+                $pattern = '`' . $excludePattern[ 'pattern' ] . '`i';
824 824
                 try {
825 825
                     if (preg_match($pattern, $uri) === 1) {
826 826
                         return true;
827 827
                     }
828 828
                 } catch (\Exception $e) {
829 829
                     // That's fine
830
-                    Craft::error('Invalid exclude URI Regex: '.$pattern, __METHOD__);
830
+                    Craft::error('Invalid exclude URI Regex: ' . $pattern, __METHOD__);
831 831
                 }
832 832
             }
833 833
         }
Please login to merge, or discard this patch.