Passed
Push — develop ( aa8cda...4b4073 )
by Andrew
06:14
created
src/services/Redirects.php 1 patch
Spacing   +72 added lines, -72 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;
@@ -359,13 +359,13 @@  discard block
 block discarded – undo
359 359
     public function getRedirectFromCache($url, int $siteId = 0)
360 360
     {
361 361
         $cache = Craft::$app->getCache();
362
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
362
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
363 363
         $redirect = $cache->get($cacheKey);
364 364
         Craft::info(
365 365
             Craft::t(
366 366
                 'retour',
367 367
                 'Cached redirect hit for {url}',
368
-                ['url' => $url]
368
+                [ 'url' => $url ]
369 369
             ),
370 370
             __METHOD__
371 371
         );
@@ -387,12 +387,12 @@  discard block
 block discarded – undo
387 387
         } catch (SiteNotFoundException $e) {
388 388
             $siteId = 1;
389 389
         }
390
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
390
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
391 391
         // Create the dependency tags
392 392
         $dependency = new TagDependency([
393 393
             'tags' => [
394 394
                 $this::GLOBAL_REDIRECTS_CACHE_TAG,
395
-                $this::GLOBAL_REDIRECTS_CACHE_TAG.$siteId,
395
+                $this::GLOBAL_REDIRECTS_CACHE_TAG . $siteId,
396 396
             ],
397 397
         ]);
398 398
         $cache->set($cacheKey, $redirect, Retour::$cacheDuration, $dependency);
@@ -400,7 +400,7 @@  discard block
 block discarded – undo
400 400
             Craft::t(
401 401
                 'retour',
402 402
                 'Cached redirect saved for {url}',
403
-                ['url' => $url]
403
+                [ 'url' => $url ]
404 404
             ),
405 405
             __METHOD__
406 406
         );
@@ -430,8 +430,8 @@  discard block
 block discarded – undo
430 430
         // Iterate through the redirects
431 431
         foreach ($redirects as $redirect) {
432 432
             // Figure out what type of source matching to do
433
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
434
-            $redirectEnabled = (bool)$redirect['enabled'];
433
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
434
+            $redirectEnabled = (bool) $redirect[ 'enabled' ];
435 435
             if ($redirectEnabled === true) {
436 436
                 switch ($redirectSrcMatch) {
437 437
                     case 'pathonly':
@@ -444,11 +444,11 @@  discard block
 block discarded – undo
444 444
                         $url = $pathOnly;
445 445
                         break;
446 446
                 }
447
-                $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
447
+                $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
448 448
                 switch ($redirectMatchType) {
449 449
                     // Do a straight up match
450 450
                     case 'exactmatch':
451
-                        if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
451
+                        if (strcasecmp($redirect[ 'redirectSrcUrlParsed' ], $url) === 0) {
452 452
                             $this->incrementRedirectHitCount($redirect);
453 453
                             $this->saveRedirectToCache($url, $redirect);
454 454
 
@@ -471,15 +471,15 @@  discard block
 block discarded – undo
471 471
 
472 472
                     // Do a regex match
473 473
                     case 'regexmatch':
474
-                        $matchRegEx = '`'.$redirect['redirectSrcUrlParsed'].'`i';
474
+                        $matchRegEx = '`' . $redirect[ 'redirectSrcUrlParsed' ] . '`i';
475 475
                         try {
476 476
                             if (preg_match($matchRegEx, $url) === 1) {
477 477
                                 $this->incrementRedirectHitCount($redirect);
478 478
                                 // If we're not associated with an EntryID, handle capture group replacement
479
-                                if ((int)$redirect['associatedElementId'] === 0) {
480
-                                    $redirect['redirectDestUrl'] = preg_replace(
479
+                                if ((int) $redirect[ 'associatedElementId' ] === 0) {
480
+                                    $redirect[ 'redirectDestUrl' ] = preg_replace(
481 481
                                         $matchRegEx,
482
-                                        $redirect['redirectDestUrl'],
482
+                                        $redirect[ 'redirectDestUrl' ],
483 483
                                         $url
484 484
                                     );
485 485
                                 }
@@ -503,7 +503,7 @@  discard block
 block discarded – undo
503 503
                             }
504 504
                         } catch (\Exception $e) {
505 505
                             // That's fine
506
-                            Craft::error('Invalid Redirect Regex: '.$matchRegEx, __METHOD__);
506
+                            Craft::error('Invalid Redirect Regex: ' . $matchRegEx, __METHOD__);
507 507
                         }
508 508
 
509 509
                         break;
@@ -517,7 +517,7 @@  discard block
 block discarded – undo
517 517
                                     'redirect' => &$redirect,
518 518
                                 ],
519 519
                             ];
520
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
520
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
521 521
                             if ($result) {
522 522
                                 $this->incrementRedirectHitCount($redirect);
523 523
                                 $this->saveRedirectToCache($url, $redirect);
@@ -557,7 +557,7 @@  discard block
 block discarded – undo
557 557
             Craft::t(
558 558
                 'retour',
559 559
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
560
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
560
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
561 561
             ),
562 562
             __METHOD__
563 563
         );
@@ -584,8 +584,8 @@  discard block
 block discarded – undo
584 584
 
585 585
             if ($url !== null && $redirect !== null) {
586 586
                 // Save the modified redirect to the cache
587
-                $redirect['redirectDestUrl'] = $event->redirectDestUrl;
588
-                $redirect['redirectHttpCode'] = $event->redirectHttpCode;
587
+                $redirect[ 'redirectDestUrl' ] = $event->redirectDestUrl;
588
+                $redirect[ 'redirectHttpCode' ] = $event->redirectHttpCode;
589 589
                 $this->saveRedirectToCache($url, $redirect);
590 590
             }
591 591
         }
@@ -609,7 +609,7 @@  discard block
 block discarded – undo
609 609
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
610 610
             /** @var Plugin $plugin */
611 611
             if (method_exists($plugin, 'retourMatch')) {
612
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
612
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
613 613
             }
614 614
         }
615 615
 
@@ -630,12 +630,12 @@  discard block
 block discarded – undo
630 630
         }
631 631
         // Query the db table
632 632
         $query = (new Query())
633
-            ->from(['{{%retour_static_redirects}}'])
633
+            ->from([ '{{%retour_static_redirects}}' ])
634 634
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
635 635
         if ($siteId) {
636 636
             $query
637
-                ->where(['siteId' => $siteId])
638
-                ->orWhere(['siteId' => null]);
637
+                ->where([ 'siteId' => $siteId ])
638
+                ->orWhere([ 'siteId' => null ]);
639 639
         }
640 640
         if ($limit) {
641 641
             $query->limit($limit);
@@ -658,8 +658,8 @@  discard block
 block discarded – undo
658 658
     {
659 659
         // Query the db table
660 660
         $redirect = (new Query())
661
-            ->from(['{{%retour_static_redirects}}'])
662
-            ->where(['id' => $id])
661
+            ->from([ '{{%retour_static_redirects}}' ])
662
+            ->where([ 'id' => $id ])
663 663
             ->one();
664 664
 
665 665
         return $redirect;
@@ -677,16 +677,16 @@  discard block
 block discarded – undo
677 677
     {
678 678
         // Query the db table
679 679
         $query = (new Query())
680
-            ->from(['{{%retour_static_redirects}}'])
681
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
680
+            ->from([ '{{%retour_static_redirects}}' ])
681
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
682 682
             ;
683 683
         if ($siteId) {
684 684
             $query
685
-                ->andWhere(['or', [
685
+                ->andWhere([ 'or', [
686 686
                     'siteId' => $siteId,
687 687
                 ], [
688 688
                     'siteId' => null,
689
-                ]]);
689
+                ] ]);
690 690
         }
691 691
         $redirect = $query->one();
692 692
 
@@ -728,13 +728,13 @@  discard block
 block discarded – undo
728 728
     {
729 729
         if ($redirectConfig !== null) {
730 730
             $db = Craft::$app->getDb();
731
-            $redirectConfig['hitCount']++;
732
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
731
+            $redirectConfig[ 'hitCount' ]++;
732
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
733 733
             Craft::debug(
734 734
                 Craft::t(
735 735
                     'retour',
736 736
                     'Incrementing statistics for: {redirect}',
737
-                    ['redirect' => print_r($redirectConfig, true)]
737
+                    [ 'redirect' => print_r($redirectConfig, true) ]
738 738
                 ),
739 739
                 __METHOD__
740 740
             );
@@ -743,14 +743,14 @@  discard block
 block discarded – undo
743 743
                 $rowsAffected = $db->createCommand()->update(
744 744
                     '{{%retour_static_redirects}}',
745 745
                     [
746
-                        'hitCount' => $redirectConfig['hitCount'],
747
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
746
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
747
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
748 748
                     ],
749 749
                     [
750
-                        'id' => $redirectConfig['id'],
750
+                        'id' => $redirectConfig[ 'id' ],
751 751
                     ]
752 752
                 )->execute();
753
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
753
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
754 754
             } catch (\Exception $e) {
755 755
                 Craft::error($e->getMessage(), __METHOD__);
756 756
             }
@@ -763,9 +763,9 @@  discard block
 block discarded – undo
763 763
     public function saveRedirect(array $redirectConfig)
764 764
     {
765 765
         // Handle URL encoded URLs by decoding them before saving them
766
-        if ( $redirectConfig['redirectMatchType'] === 'exactmatch') {
767
-            $redirectConfig['redirectSrcUrl'] = urldecode($redirectConfig['redirectSrcUrl'] ?? '');
768
-            $redirectConfig['redirectSrcUrlParsed'] = urldecode($redirectConfig['redirectSrcUrlParsed'] ?? '');
766
+        if ($redirectConfig[ 'redirectMatchType' ] === 'exactmatch') {
767
+            $redirectConfig[ 'redirectSrcUrl' ] = urldecode($redirectConfig[ 'redirectSrcUrl' ] ?? '');
768
+            $redirectConfig[ 'redirectSrcUrlParsed' ] = urldecode($redirectConfig[ 'redirectSrcUrlParsed' ] ?? '');
769 769
         }
770 770
         // Validate the model before saving it to the db
771 771
         $redirect = new StaticRedirectsModel($redirectConfig);
@@ -774,7 +774,7 @@  discard block
 block discarded – undo
774 774
                 Craft::t(
775 775
                     'retour',
776 776
                     'Error validating redirect {id}: {errors}',
777
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
777
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
778 778
                 ),
779 779
                 __METHOD__
780 780
             );
@@ -784,32 +784,32 @@  discard block
 block discarded – undo
784 784
         // Get the validated model attributes and save them to the db
785 785
         $redirectConfig = $redirect->getAttributes();
786 786
         // 0 for a siteId needs to be converted to null
787
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
788
-            $redirectConfig['siteId'] = null;
787
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
788
+            $redirectConfig[ 'siteId' ] = null;
789 789
         }
790 790
         // Throw an event to before saving the redirect
791 791
         $db = Craft::$app->getDb();
792 792
         // See if a redirect exists with this source URL already
793
-        if ((int)$redirectConfig['id'] === 0) {
793
+        if ((int) $redirectConfig[ 'id' ] === 0) {
794 794
             // Query the db table
795 795
             $redirect = (new Query())
796
-                ->from(['{{%retour_static_redirects}}'])
797
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
798
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
796
+                ->from([ '{{%retour_static_redirects}}' ])
797
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
798
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
799 799
                 ->one();
800 800
             // If it exists, update it rather than having duplicates
801 801
             if (!empty($redirect)) {
802
-                $redirectConfig['id'] = $redirect['id'];
802
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
803 803
             }
804 804
         }
805 805
         // Trigger a 'beforeSaveRedirect' event
806
-        $isNew = (int)$redirectConfig['id'] === 0;
806
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
807 807
         $event = new RedirectEvent([
808 808
             'isNew' => $isNew,
809
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
810
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
811
-            'matchType' => $redirectConfig['redirectSrcMatch'],
812
-            'redirectType' => $redirectConfig['redirectHttpCode'],
809
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
810
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
811
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
812
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
813 813
         ]);
814 814
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
815 815
         if (!$event->isValid) {
@@ -821,7 +821,7 @@  discard block
 block discarded – undo
821 821
                 Craft::t(
822 822
                     'retour',
823 823
                     'Updating existing redirect: {redirect}',
824
-                    ['redirect' => print_r($redirectConfig, true)]
824
+                    [ 'redirect' => print_r($redirectConfig, true) ]
825 825
                 ),
826 826
                 __METHOD__
827 827
             );
@@ -831,7 +831,7 @@  discard block
 block discarded – undo
831 831
                     '{{%retour_static_redirects}}',
832 832
                     $redirectConfig,
833 833
                     [
834
-                        'id' => $redirectConfig['id'],
834
+                        'id' => $redirectConfig[ 'id' ],
835 835
                     ]
836 836
                 )->execute();
837 837
             } catch (Exception $e) {
@@ -842,11 +842,11 @@  discard block
 block discarded – undo
842 842
                 Craft::t(
843 843
                     'retour',
844 844
                     'Creating new redirect: {redirect}',
845
-                    ['redirect' => print_r($redirectConfig, true)]
845
+                    [ 'redirect' => print_r($redirectConfig, true) ]
846 846
                 ),
847 847
                 __METHOD__
848 848
             );
849
-            unset($redirectConfig['id']);
849
+            unset($redirectConfig[ 'id' ]);
850 850
             // Create a new record
851 851
             try {
852 852
                 $db->createCommand()->insert(
@@ -859,15 +859,15 @@  discard block
 block discarded – undo
859 859
         }
860 860
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
861 861
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
862
-            $redirectConfig['redirectDestUrl'],
863
-            $redirectConfig['siteId']
862
+            $redirectConfig[ 'redirectDestUrl' ],
863
+            $redirectConfig[ 'siteId' ]
864 864
         );
865 865
         if ($testRedirectConfig !== null) {
866 866
             Craft::debug(
867 867
                 Craft::t(
868 868
                     'retour',
869 869
                     'Deleting redirect to prevent a loop: {redirect}',
870
-                    ['redirect' => print_r($testRedirectConfig, true)]
870
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
871 871
                 ),
872 872
                 __METHOD__
873 873
             );
@@ -875,7 +875,7 @@  discard block
 block discarded – undo
875 875
             try {
876 876
                 $db->createCommand()->delete(
877 877
                     '{{%retour_static_redirects}}',
878
-                    ['id' => $testRedirectConfig['id']]
878
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
879 879
                 )->execute();
880 880
             } catch (Exception $e) {
881 881
                 Craft::error($e->getMessage(), __METHOD__);
@@ -918,17 +918,17 @@  discard block
 block discarded – undo
918 918
      */
919 919
     public function excludeUri($uri): bool
920 920
     {
921
-        $uri = '/'.ltrim($uri, '/');
921
+        $uri = '/' . ltrim($uri, '/');
922 922
         if (!empty(Retour::$settings->excludePatterns)) {
923 923
             foreach (Retour::$settings->excludePatterns as $excludePattern) {
924
-                $pattern = '`'.$excludePattern['pattern'].'`i';
924
+                $pattern = '`' . $excludePattern[ 'pattern' ] . '`i';
925 925
                 try {
926 926
                     if (preg_match($pattern, $uri) === 1) {
927 927
                         return true;
928 928
                     }
929 929
                 } catch (\Exception $e) {
930 930
                     // That's fine
931
-                    Craft::error('Invalid exclude URI Regex: '.$pattern, __METHOD__);
931
+                    Craft::error('Invalid exclude URI Regex: ' . $pattern, __METHOD__);
932 932
                 }
933 933
             }
934 934
         }
Please login to merge, or discard this patch.