Passed
Push — develop ( 4ca4eb...456651 )
by Andrew
04:48
created
src/services/Redirects.php 1 patch
Spacing   +63 added lines, -63 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,12 +228,12 @@  discard block
 block discarded – undo
228 228
                     $dest .= '?' . $request->getQueryStringWithoutPath();
229 229
                 }
230 230
             }
231
-            $status = $redirect['redirectHttpCode'];
231
+            $status = $redirect[ 'redirectHttpCode' ];
232 232
             Craft::info(
233 233
                 Craft::t(
234 234
                     'retour',
235 235
                     'Redirecting {url} to {dest} with status {status}',
236
-                    ['url' => $url, 'dest' => $dest, 'status' => $status]
236
+                    [ 'url' => $url, 'dest' => $dest, 'status' => $status ]
237 237
                 ),
238 238
                 __METHOD__
239 239
             );
@@ -315,13 +315,13 @@  discard block
 block discarded – undo
315 315
     public function getRedirectFromCache($url, int $siteId = 0)
316 316
     {
317 317
         $cache = Craft::$app->getCache();
318
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
318
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
319 319
         $redirect = $cache->get($cacheKey);
320 320
         Craft::info(
321 321
             Craft::t(
322 322
                 'retour',
323 323
                 'Cached redirect hit for {url}',
324
-                ['url' => $url]
324
+                [ 'url' => $url ]
325 325
             ),
326 326
             __METHOD__
327 327
         );
@@ -343,12 +343,12 @@  discard block
 block discarded – undo
343 343
         } catch (SiteNotFoundException $e) {
344 344
             $siteId = 1;
345 345
         }
346
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
346
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
347 347
         // Create the dependency tags
348 348
         $dependency = new TagDependency([
349 349
             'tags' => [
350 350
                 $this::GLOBAL_REDIRECTS_CACHE_TAG,
351
-                $this::GLOBAL_REDIRECTS_CACHE_TAG.$siteId,
351
+                $this::GLOBAL_REDIRECTS_CACHE_TAG . $siteId,
352 352
             ],
353 353
         ]);
354 354
         $cache->set($cacheKey, $redirect, Retour::$cacheDuration, $dependency);
@@ -356,7 +356,7 @@  discard block
 block discarded – undo
356 356
             Craft::t(
357 357
                 'retour',
358 358
                 'Cached redirect saved for {url}',
359
-                ['url' => $url]
359
+                [ 'url' => $url ]
360 360
             ),
361 361
             __METHOD__
362 362
         );
@@ -386,8 +386,8 @@  discard block
 block discarded – undo
386 386
         // Iterate through the redirects
387 387
         foreach ($redirects as $redirect) {
388 388
             // Figure out what type of source matching to do
389
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
390
-            $redirectEnabled = (bool)$redirect['enabled'];
389
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
390
+            $redirectEnabled = (bool) $redirect[ 'enabled' ];
391 391
             if ($redirectEnabled === true) {
392 392
                 switch ($redirectSrcMatch) {
393 393
                     case 'pathonly':
@@ -400,11 +400,11 @@  discard block
 block discarded – undo
400 400
                         $url = $pathOnly;
401 401
                         break;
402 402
                 }
403
-                $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
403
+                $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
404 404
                 switch ($redirectMatchType) {
405 405
                     // Do a straight up match
406 406
                     case 'exactmatch':
407
-                        if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
407
+                        if (strcasecmp($redirect[ 'redirectSrcUrlParsed' ], $url) === 0) {
408 408
                             $this->incrementRedirectHitCount($redirect);
409 409
                             $this->saveRedirectToCache($url, $redirect);
410 410
 
@@ -414,15 +414,15 @@  discard block
 block discarded – undo
414 414
 
415 415
                     // Do a regex match
416 416
                     case 'regexmatch':
417
-                        $matchRegEx = '`'.$redirect['redirectSrcUrlParsed'].'`i';
417
+                        $matchRegEx = '`' . $redirect[ 'redirectSrcUrlParsed' ] . '`i';
418 418
                         try {
419 419
                             if (preg_match($matchRegEx, $url) === 1) {
420 420
                                 $this->incrementRedirectHitCount($redirect);
421 421
                                 // If we're not associated with an EntryID, handle capture group replacement
422
-                                if ((int)$redirect['associatedElementId'] === 0) {
423
-                                    $redirect['redirectDestUrl'] = preg_replace(
422
+                                if ((int) $redirect[ 'associatedElementId' ] === 0) {
423
+                                    $redirect[ 'redirectDestUrl' ] = preg_replace(
424 424
                                         $matchRegEx,
425
-                                        $redirect['redirectDestUrl'],
425
+                                        $redirect[ 'redirectDestUrl' ],
426 426
                                         $url
427 427
                                     );
428 428
                                 }
@@ -432,7 +432,7 @@  discard block
 block discarded – undo
432 432
                             }
433 433
                         } catch (\Exception $e) {
434 434
                             // That's fine
435
-                            Craft::error('Invalid Redirect Regex: '.$matchRegEx, __METHOD__);
435
+                            Craft::error('Invalid Redirect Regex: ' . $matchRegEx, __METHOD__);
436 436
                         }
437 437
 
438 438
                         break;
@@ -446,7 +446,7 @@  discard block
 block discarded – undo
446 446
                                     'redirect' => &$redirect,
447 447
                                 ],
448 448
                             ];
449
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
449
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
450 450
                             if ($result) {
451 451
                                 $this->incrementRedirectHitCount($redirect);
452 452
                                 $this->saveRedirectToCache($url, $redirect);
@@ -473,7 +473,7 @@  discard block
 block discarded – undo
473 473
             Craft::t(
474 474
                 'retour',
475 475
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
476
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
476
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
477 477
             ),
478 478
             __METHOD__
479 479
         );
@@ -518,7 +518,7 @@  discard block
 block discarded – undo
518 518
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
519 519
             /** @var Plugin $plugin */
520 520
             if (method_exists($plugin, 'retourMatch')) {
521
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
521
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
522 522
             }
523 523
         }
524 524
 
@@ -539,12 +539,12 @@  discard block
 block discarded – undo
539 539
         }
540 540
         // Query the db table
541 541
         $query = (new Query())
542
-            ->from(['{{%retour_static_redirects}}'])
542
+            ->from([ '{{%retour_static_redirects}}' ])
543 543
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
544 544
         if ($siteId) {
545 545
             $query
546
-                ->where(['siteId' => $siteId])
547
-                ->orWhere(['siteId' => null]);
546
+                ->where([ 'siteId' => $siteId ])
547
+                ->orWhere([ 'siteId' => null ]);
548 548
         }
549 549
         if ($limit) {
550 550
             $query->limit($limit);
@@ -567,8 +567,8 @@  discard block
 block discarded – undo
567 567
     {
568 568
         // Query the db table
569 569
         $redirect = (new Query())
570
-            ->from(['{{%retour_static_redirects}}'])
571
-            ->where(['id' => $id])
570
+            ->from([ '{{%retour_static_redirects}}' ])
571
+            ->where([ 'id' => $id ])
572 572
             ->one();
573 573
 
574 574
         return $redirect;
@@ -586,16 +586,16 @@  discard block
 block discarded – undo
586 586
     {
587 587
         // Query the db table
588 588
         $query = (new Query())
589
-            ->from(['{{%retour_static_redirects}}'])
590
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
589
+            ->from([ '{{%retour_static_redirects}}' ])
590
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
591 591
             ;
592 592
         if ($siteId) {
593 593
             $query
594
-                ->andWhere(['or', [
594
+                ->andWhere([ 'or', [
595 595
                     'siteId' => $siteId,
596 596
                 ], [
597 597
                     'siteId' => null,
598
-                ]]);
598
+                ] ]);
599 599
         }
600 600
         $redirect = $query->one();
601 601
 
@@ -637,13 +637,13 @@  discard block
 block discarded – undo
637 637
     {
638 638
         if ($redirectConfig !== null) {
639 639
             $db = Craft::$app->getDb();
640
-            $redirectConfig['hitCount']++;
641
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
640
+            $redirectConfig[ 'hitCount' ]++;
641
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
642 642
             Craft::debug(
643 643
                 Craft::t(
644 644
                     'retour',
645 645
                     'Incrementing statistics for: {redirect}',
646
-                    ['redirect' => print_r($redirectConfig, true)]
646
+                    [ 'redirect' => print_r($redirectConfig, true) ]
647 647
                 ),
648 648
                 __METHOD__
649 649
             );
@@ -652,14 +652,14 @@  discard block
 block discarded – undo
652 652
                 $rowsAffected = $db->createCommand()->update(
653 653
                     '{{%retour_static_redirects}}',
654 654
                     [
655
-                        'hitCount' => $redirectConfig['hitCount'],
656
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
655
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
656
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
657 657
                     ],
658 658
                     [
659
-                        'id' => $redirectConfig['id'],
659
+                        'id' => $redirectConfig[ 'id' ],
660 660
                     ]
661 661
                 )->execute();
662
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
662
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
663 663
             } catch (\Exception $e) {
664 664
                 Craft::error($e->getMessage(), __METHOD__);
665 665
             }
@@ -678,7 +678,7 @@  discard block
 block discarded – undo
678 678
                 Craft::t(
679 679
                     'retour',
680 680
                     'Error validating redirect {id}: {errors}',
681
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
681
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
682 682
                 ),
683 683
                 __METHOD__
684 684
             );
@@ -688,32 +688,32 @@  discard block
 block discarded – undo
688 688
         // Get the validated model attributes and save them to the db
689 689
         $redirectConfig = $redirect->getAttributes();
690 690
         // 0 for a siteId needs to be converted to null
691
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
692
-            $redirectConfig['siteId'] = null;
691
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
692
+            $redirectConfig[ 'siteId' ] = null;
693 693
         }
694 694
         // Throw an event to before saving the redirect
695 695
         $db = Craft::$app->getDb();
696 696
         // See if a redirect exists with this source URL already
697
-        if ((int)$redirectConfig['id'] === 0) {
697
+        if ((int) $redirectConfig[ 'id' ] === 0) {
698 698
             // Query the db table
699 699
             $redirect = (new Query())
700
-                ->from(['{{%retour_static_redirects}}'])
701
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
702
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
700
+                ->from([ '{{%retour_static_redirects}}' ])
701
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
702
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
703 703
                 ->one();
704 704
             // If it exists, update it rather than having duplicates
705 705
             if (!empty($redirect)) {
706
-                $redirectConfig['id'] = $redirect['id'];
706
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
707 707
             }
708 708
         }
709 709
         // Trigger a 'beforeSaveRedirect' event
710
-        $isNew = (int)$redirectConfig['id'] === 0;
710
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
711 711
         $event = new RedirectEvent([
712 712
             'isNew' => $isNew,
713
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
714
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
715
-            'matchType' => $redirectConfig['redirectSrcMatch'],
716
-            'redirectType' => $redirectConfig['redirectHttpCode'],
713
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
714
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
715
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
716
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
717 717
         ]);
718 718
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
719 719
         if (!$event->isValid) {
@@ -725,7 +725,7 @@  discard block
 block discarded – undo
725 725
                 Craft::t(
726 726
                     'retour',
727 727
                     'Updating existing redirect: {redirect}',
728
-                    ['redirect' => print_r($redirectConfig, true)]
728
+                    [ 'redirect' => print_r($redirectConfig, true) ]
729 729
                 ),
730 730
                 __METHOD__
731 731
             );
@@ -735,7 +735,7 @@  discard block
 block discarded – undo
735 735
                     '{{%retour_static_redirects}}',
736 736
                     $redirectConfig,
737 737
                     [
738
-                        'id' => $redirectConfig['id'],
738
+                        'id' => $redirectConfig[ 'id' ],
739 739
                     ]
740 740
                 )->execute();
741 741
             } catch (Exception $e) {
@@ -746,11 +746,11 @@  discard block
 block discarded – undo
746 746
                 Craft::t(
747 747
                     'retour',
748 748
                     'Creating new redirect: {redirect}',
749
-                    ['redirect' => print_r($redirectConfig, true)]
749
+                    [ 'redirect' => print_r($redirectConfig, true) ]
750 750
                 ),
751 751
                 __METHOD__
752 752
             );
753
-            unset($redirectConfig['id']);
753
+            unset($redirectConfig[ 'id' ]);
754 754
             // Create a new record
755 755
             try {
756 756
                 $db->createCommand()->insert(
@@ -763,15 +763,15 @@  discard block
 block discarded – undo
763 763
         }
764 764
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
765 765
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
766
-            $redirectConfig['redirectDestUrl'],
767
-            $redirectConfig['siteId']
766
+            $redirectConfig[ 'redirectDestUrl' ],
767
+            $redirectConfig[ 'siteId' ]
768 768
         );
769 769
         if ($testRedirectConfig !== null) {
770 770
             Craft::debug(
771 771
                 Craft::t(
772 772
                     'retour',
773 773
                     'Deleting redirect to prevent a loop: {redirect}',
774
-                    ['redirect' => print_r($testRedirectConfig, true)]
774
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
775 775
                 ),
776 776
                 __METHOD__
777 777
             );
@@ -779,7 +779,7 @@  discard block
 block discarded – undo
779 779
             try {
780 780
                 $db->createCommand()->delete(
781 781
                     '{{%retour_static_redirects}}',
782
-                    ['id' => $testRedirectConfig['id']]
782
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
783 783
                 )->execute();
784 784
             } catch (Exception $e) {
785 785
                 Craft::error($e->getMessage(), __METHOD__);
@@ -812,17 +812,17 @@  discard block
 block discarded – undo
812 812
      */
813 813
     public function excludeUri($uri): bool
814 814
     {
815
-        $uri = '/'.ltrim($uri, '/');
815
+        $uri = '/' . ltrim($uri, '/');
816 816
         if (!empty(Retour::$settings->excludePatterns)) {
817 817
             foreach (Retour::$settings->excludePatterns as $excludePattern) {
818
-                $pattern = '`'.$excludePattern['pattern'].'`i';
818
+                $pattern = '`' . $excludePattern[ 'pattern' ] . '`i';
819 819
                 try {
820 820
                     if (preg_match($pattern, $uri) === 1) {
821 821
                         return true;
822 822
                     }
823 823
                 } catch (\Exception $e) {
824 824
                     // That's fine
825
-                    Craft::error('Invalid exclude URI Regex: '.$pattern, __METHOD__);
825
+                    Craft::error('Invalid exclude URI Regex: ' . $pattern, __METHOD__);
826 826
                 }
827 827
             }
828 828
         }
Please login to merge, or discard this patch.