Passed
Push — develop ( f71cfa...751359 )
by Andrew
06:45
created
src/services/Redirects.php 1 patch
Spacing   +69 added lines, -69 removed lines patch added patch discarded remove patch
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
                 Craft::t(
191 191
                     'retour',
192 192
                     '404 full URL: {fullUrl}, 404 path only: {pathOnly}',
193
-                    ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
193
+                    [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
194 194
                 ),
195 195
                 __METHOD__
196 196
             );
@@ -224,7 +224,7 @@  discard block
 block discarded – undo
224 224
         $response = Craft::$app->getResponse();
225 225
         if ($redirect !== null) {
226 226
             // Figure out what type of source matching was done
227
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
227
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
228 228
             switch ($redirectSrcMatch) {
229 229
                 case 'pathonly':
230 230
                     $url = $pathOnly;
@@ -236,13 +236,13 @@  discard block
 block discarded – undo
236 236
                     $url = $pathOnly;
237 237
                     break;
238 238
             }
239
-            $dest = $redirect['redirectDestUrl'];
239
+            $dest = $redirect[ 'redirectDestUrl' ];
240 240
             // If this isn't a full URL, make it one based on the appropriate site
241 241
             if (!UrlHelper::isFullUrl($dest)) {
242 242
                 try {
243
-                    $siteId = $redirect['siteId'] ?? null;
243
+                    $siteId = $redirect[ 'siteId' ] ?? null;
244 244
                     if ($siteId !== null) {
245
-                        $siteId = (int)$siteId;
245
+                        $siteId = (int) $siteId;
246 246
                     }
247 247
                     $dest = UrlHelper::siteUrl($dest, null, null, $siteId);
248 248
                 } catch (\yii\base\Exception $e) {
@@ -254,17 +254,17 @@  discard block
 block discarded – undo
254 254
                     $dest .= '?' . $request->getQueryStringWithoutPath();
255 255
                 }
256 256
             }
257
-            $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
257
+            $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
258 258
             // Parse reference tags for exact matches
259 259
             if ($redirectMatchType === 'exactmatch') {
260
-                $dest = Craft::$app->elements->parseRefs($dest, $redirect['siteId'] ?? null);
260
+                $dest = Craft::$app->elements->parseRefs($dest, $redirect[ 'siteId' ] ?? null);
261 261
             }
262
-            $status = $redirect['redirectHttpCode'];
262
+            $status = $redirect[ 'redirectHttpCode' ];
263 263
             Craft::info(
264 264
                 Craft::t(
265 265
                     'retour',
266 266
                     'Redirecting {url} to {dest} with status {status}',
267
-                    ['url' => $url, 'dest' => $dest, 'status' => $status]
267
+                    [ 'url' => $url, 'dest' => $dest, 'status' => $status ]
268 268
                 ),
269 269
                 __METHOD__
270 270
             );
@@ -288,7 +288,7 @@  discard block
 block discarded – undo
288 288
             // Add any additional headers (existing ones will be replaced)
289 289
             if (!empty(Retour::$settings->additionalHeaders)) {
290 290
                 foreach (Retour::$settings->additionalHeaders as $additionalHeader) {
291
-                    $response->headers->set($additionalHeader['name'], $additionalHeader['value']);
291
+                    $response->headers->set($additionalHeader[ 'name' ], $additionalHeader[ 'value' ]);
292 292
                 }
293 293
             }
294 294
             // Redirect the request away;
@@ -354,13 +354,13 @@  discard block
 block discarded – undo
354 354
     public function getRedirectFromCache($url, int $siteId = 0)
355 355
     {
356 356
         $cache = Craft::$app->getCache();
357
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
357
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
358 358
         $redirect = $cache->get($cacheKey);
359 359
         Craft::info(
360 360
             Craft::t(
361 361
                 'retour',
362 362
                 'Cached redirect hit for {url}',
363
-                ['url' => $url]
363
+                [ 'url' => $url ]
364 364
             ),
365 365
             __METHOD__
366 366
         );
@@ -382,12 +382,12 @@  discard block
 block discarded – undo
382 382
         } catch (SiteNotFoundException $e) {
383 383
             $siteId = 1;
384 384
         }
385
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
385
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
386 386
         // Create the dependency tags
387 387
         $dependency = new TagDependency([
388 388
             'tags' => [
389 389
                 $this::GLOBAL_REDIRECTS_CACHE_TAG,
390
-                $this::GLOBAL_REDIRECTS_CACHE_TAG.$siteId,
390
+                $this::GLOBAL_REDIRECTS_CACHE_TAG . $siteId,
391 391
             ],
392 392
         ]);
393 393
         $cache->set($cacheKey, $redirect, Retour::$cacheDuration, $dependency);
@@ -395,7 +395,7 @@  discard block
 block discarded – undo
395 395
             Craft::t(
396 396
                 'retour',
397 397
                 'Cached redirect saved for {url}',
398
-                ['url' => $url]
398
+                [ 'url' => $url ]
399 399
             ),
400 400
             __METHOD__
401 401
         );
@@ -425,8 +425,8 @@  discard block
 block discarded – undo
425 425
         // Iterate through the redirects
426 426
         foreach ($redirects as $redirect) {
427 427
             // Figure out what type of source matching to do
428
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
429
-            $redirectEnabled = (bool)$redirect['enabled'];
428
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
429
+            $redirectEnabled = (bool) $redirect[ 'enabled' ];
430 430
             if ($redirectEnabled === true) {
431 431
                 switch ($redirectSrcMatch) {
432 432
                     case 'pathonly':
@@ -439,11 +439,11 @@  discard block
 block discarded – undo
439 439
                         $url = $pathOnly;
440 440
                         break;
441 441
                 }
442
-                $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
442
+                $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
443 443
                 switch ($redirectMatchType) {
444 444
                     // Do a straight up match
445 445
                     case 'exactmatch':
446
-                        if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
446
+                        if (strcasecmp($redirect[ 'redirectSrcUrlParsed' ], $url) === 0) {
447 447
                             $this->incrementRedirectHitCount($redirect);
448 448
                             $this->saveRedirectToCache($url, $redirect);
449 449
 
@@ -466,15 +466,15 @@  discard block
 block discarded – undo
466 466
 
467 467
                     // Do a regex match
468 468
                     case 'regexmatch':
469
-                        $matchRegEx = '`'.$redirect['redirectSrcUrlParsed'].'`i';
469
+                        $matchRegEx = '`' . $redirect[ 'redirectSrcUrlParsed' ] . '`i';
470 470
                         try {
471 471
                             if (preg_match($matchRegEx, $url) === 1) {
472 472
                                 $this->incrementRedirectHitCount($redirect);
473 473
                                 // If we're not associated with an EntryID, handle capture group replacement
474
-                                if ((int)$redirect['associatedElementId'] === 0) {
475
-                                    $redirect['redirectDestUrl'] = preg_replace(
474
+                                if ((int) $redirect[ 'associatedElementId' ] === 0) {
475
+                                    $redirect[ 'redirectDestUrl' ] = preg_replace(
476 476
                                         $matchRegEx,
477
-                                        $redirect['redirectDestUrl'],
477
+                                        $redirect[ 'redirectDestUrl' ],
478 478
                                         $url
479 479
                                     );
480 480
                                 }
@@ -498,7 +498,7 @@  discard block
 block discarded – undo
498 498
                             }
499 499
                         } catch (\Exception $e) {
500 500
                             // That's fine
501
-                            Craft::error('Invalid Redirect Regex: '.$matchRegEx, __METHOD__);
501
+                            Craft::error('Invalid Redirect Regex: ' . $matchRegEx, __METHOD__);
502 502
                         }
503 503
 
504 504
                         break;
@@ -512,7 +512,7 @@  discard block
 block discarded – undo
512 512
                                     'redirect' => &$redirect,
513 513
                                 ],
514 514
                             ];
515
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
515
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
516 516
                             if ($result) {
517 517
                                 $this->incrementRedirectHitCount($redirect);
518 518
                                 $this->saveRedirectToCache($url, $redirect);
@@ -552,7 +552,7 @@  discard block
 block discarded – undo
552 552
             Craft::t(
553 553
                 'retour',
554 554
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
555
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
555
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
556 556
             ),
557 557
             __METHOD__
558 558
         );
@@ -579,8 +579,8 @@  discard block
 block discarded – undo
579 579
 
580 580
             if ($url !== null && $redirect !== null) {
581 581
                 // Save the modified redirect to the cache
582
-                $redirect['redirectDestUrl'] = $event->redirectDestUrl;
583
-                $redirect['redirectHttpCode'] = $event->redirectHttpCode;
582
+                $redirect[ 'redirectDestUrl' ] = $event->redirectDestUrl;
583
+                $redirect[ 'redirectHttpCode' ] = $event->redirectHttpCode;
584 584
                 $this->saveRedirectToCache($url, $redirect);
585 585
             }
586 586
         }
@@ -604,7 +604,7 @@  discard block
 block discarded – undo
604 604
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
605 605
             /** @var Plugin $plugin */
606 606
             if (method_exists($plugin, 'retourMatch')) {
607
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
607
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
608 608
             }
609 609
         }
610 610
 
@@ -625,12 +625,12 @@  discard block
 block discarded – undo
625 625
         }
626 626
         // Query the db table
627 627
         $query = (new Query())
628
-            ->from(['{{%retour_static_redirects}}'])
628
+            ->from([ '{{%retour_static_redirects}}' ])
629 629
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
630 630
         if ($siteId) {
631 631
             $query
632
-                ->where(['siteId' => $siteId])
633
-                ->orWhere(['siteId' => null]);
632
+                ->where([ 'siteId' => $siteId ])
633
+                ->orWhere([ 'siteId' => null ]);
634 634
         }
635 635
         if ($limit) {
636 636
             $query->limit($limit);
@@ -653,8 +653,8 @@  discard block
 block discarded – undo
653 653
     {
654 654
         // Query the db table
655 655
         $redirect = (new Query())
656
-            ->from(['{{%retour_static_redirects}}'])
657
-            ->where(['id' => $id])
656
+            ->from([ '{{%retour_static_redirects}}' ])
657
+            ->where([ 'id' => $id ])
658 658
             ->one();
659 659
 
660 660
         return $redirect;
@@ -672,16 +672,16 @@  discard block
 block discarded – undo
672 672
     {
673 673
         // Query the db table
674 674
         $query = (new Query())
675
-            ->from(['{{%retour_static_redirects}}'])
676
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
675
+            ->from([ '{{%retour_static_redirects}}' ])
676
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
677 677
             ;
678 678
         if ($siteId) {
679 679
             $query
680
-                ->andWhere(['or', [
680
+                ->andWhere([ 'or', [
681 681
                     'siteId' => $siteId,
682 682
                 ], [
683 683
                     'siteId' => null,
684
-                ]]);
684
+                ] ]);
685 685
         }
686 686
         $redirect = $query->one();
687 687
 
@@ -723,13 +723,13 @@  discard block
 block discarded – undo
723 723
     {
724 724
         if ($redirectConfig !== null) {
725 725
             $db = Craft::$app->getDb();
726
-            $redirectConfig['hitCount']++;
727
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
726
+            $redirectConfig[ 'hitCount' ]++;
727
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
728 728
             Craft::debug(
729 729
                 Craft::t(
730 730
                     'retour',
731 731
                     'Incrementing statistics for: {redirect}',
732
-                    ['redirect' => print_r($redirectConfig, true)]
732
+                    [ 'redirect' => print_r($redirectConfig, true) ]
733 733
                 ),
734 734
                 __METHOD__
735 735
             );
@@ -738,14 +738,14 @@  discard block
 block discarded – undo
738 738
                 $rowsAffected = $db->createCommand()->update(
739 739
                     '{{%retour_static_redirects}}',
740 740
                     [
741
-                        'hitCount' => $redirectConfig['hitCount'],
742
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
741
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
742
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
743 743
                     ],
744 744
                     [
745
-                        'id' => $redirectConfig['id'],
745
+                        'id' => $redirectConfig[ 'id' ],
746 746
                     ]
747 747
                 )->execute();
748
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
748
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
749 749
             } catch (\Exception $e) {
750 750
                 Craft::error($e->getMessage(), __METHOD__);
751 751
             }
@@ -764,7 +764,7 @@  discard block
 block discarded – undo
764 764
                 Craft::t(
765 765
                     'retour',
766 766
                     'Error validating redirect {id}: {errors}',
767
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
767
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
768 768
                 ),
769 769
                 __METHOD__
770 770
             );
@@ -774,32 +774,32 @@  discard block
 block discarded – undo
774 774
         // Get the validated model attributes and save them to the db
775 775
         $redirectConfig = $redirect->getAttributes();
776 776
         // 0 for a siteId needs to be converted to null
777
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
778
-            $redirectConfig['siteId'] = null;
777
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
778
+            $redirectConfig[ 'siteId' ] = null;
779 779
         }
780 780
         // Throw an event to before saving the redirect
781 781
         $db = Craft::$app->getDb();
782 782
         // See if a redirect exists with this source URL already
783
-        if ((int)$redirectConfig['id'] === 0) {
783
+        if ((int) $redirectConfig[ 'id' ] === 0) {
784 784
             // Query the db table
785 785
             $redirect = (new Query())
786
-                ->from(['{{%retour_static_redirects}}'])
787
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
788
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
786
+                ->from([ '{{%retour_static_redirects}}' ])
787
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
788
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
789 789
                 ->one();
790 790
             // If it exists, update it rather than having duplicates
791 791
             if (!empty($redirect)) {
792
-                $redirectConfig['id'] = $redirect['id'];
792
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
793 793
             }
794 794
         }
795 795
         // Trigger a 'beforeSaveRedirect' event
796
-        $isNew = (int)$redirectConfig['id'] === 0;
796
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
797 797
         $event = new RedirectEvent([
798 798
             'isNew' => $isNew,
799
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
800
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
801
-            'matchType' => $redirectConfig['redirectSrcMatch'],
802
-            'redirectType' => $redirectConfig['redirectHttpCode'],
799
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
800
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
801
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
802
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
803 803
         ]);
804 804
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
805 805
         if (!$event->isValid) {
@@ -811,7 +811,7 @@  discard block
 block discarded – undo
811 811
                 Craft::t(
812 812
                     'retour',
813 813
                     'Updating existing redirect: {redirect}',
814
-                    ['redirect' => print_r($redirectConfig, true)]
814
+                    [ 'redirect' => print_r($redirectConfig, true) ]
815 815
                 ),
816 816
                 __METHOD__
817 817
             );
@@ -821,7 +821,7 @@  discard block
 block discarded – undo
821 821
                     '{{%retour_static_redirects}}',
822 822
                     $redirectConfig,
823 823
                     [
824
-                        'id' => $redirectConfig['id'],
824
+                        'id' => $redirectConfig[ 'id' ],
825 825
                     ]
826 826
                 )->execute();
827 827
             } catch (Exception $e) {
@@ -832,11 +832,11 @@  discard block
 block discarded – undo
832 832
                 Craft::t(
833 833
                     'retour',
834 834
                     'Creating new redirect: {redirect}',
835
-                    ['redirect' => print_r($redirectConfig, true)]
835
+                    [ 'redirect' => print_r($redirectConfig, true) ]
836 836
                 ),
837 837
                 __METHOD__
838 838
             );
839
-            unset($redirectConfig['id']);
839
+            unset($redirectConfig[ 'id' ]);
840 840
             // Create a new record
841 841
             try {
842 842
                 $db->createCommand()->insert(
@@ -849,15 +849,15 @@  discard block
 block discarded – undo
849 849
         }
850 850
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
851 851
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
852
-            $redirectConfig['redirectDestUrl'],
853
-            $redirectConfig['siteId']
852
+            $redirectConfig[ 'redirectDestUrl' ],
853
+            $redirectConfig[ 'siteId' ]
854 854
         );
855 855
         if ($testRedirectConfig !== null) {
856 856
             Craft::debug(
857 857
                 Craft::t(
858 858
                     'retour',
859 859
                     'Deleting redirect to prevent a loop: {redirect}',
860
-                    ['redirect' => print_r($testRedirectConfig, true)]
860
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
861 861
                 ),
862 862
                 __METHOD__
863 863
             );
@@ -865,7 +865,7 @@  discard block
 block discarded – undo
865 865
             try {
866 866
                 $db->createCommand()->delete(
867 867
                     '{{%retour_static_redirects}}',
868
-                    ['id' => $testRedirectConfig['id']]
868
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
869 869
                 )->execute();
870 870
             } catch (Exception $e) {
871 871
                 Craft::error($e->getMessage(), __METHOD__);
@@ -898,17 +898,17 @@  discard block
 block discarded – undo
898 898
      */
899 899
     public function excludeUri($uri): bool
900 900
     {
901
-        $uri = '/'.ltrim($uri, '/');
901
+        $uri = '/' . ltrim($uri, '/');
902 902
         if (!empty(Retour::$settings->excludePatterns)) {
903 903
             foreach (Retour::$settings->excludePatterns as $excludePattern) {
904
-                $pattern = '`'.$excludePattern['pattern'].'`i';
904
+                $pattern = '`' . $excludePattern[ 'pattern' ] . '`i';
905 905
                 try {
906 906
                     if (preg_match($pattern, $uri) === 1) {
907 907
                         return true;
908 908
                     }
909 909
                 } catch (\Exception $e) {
910 910
                     // That's fine
911
-                    Craft::error('Invalid exclude URI Regex: '.$pattern, __METHOD__);
911
+                    Craft::error('Invalid exclude URI Regex: ' . $pattern, __METHOD__);
912 912
                 }
913 913
             }
914 914
         }
Please login to merge, or discard this patch.