Passed
Push — develop ( 4b73a5...8b3ddf )
by Andrew
04:46
created
src/services/Redirects.php 1 patch
Spacing   +61 added lines, -61 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,14 +414,14 @@  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
                         if (preg_match($matchRegEx, $url) === 1) {
419 419
                             $this->incrementRedirectHitCount($redirect);
420 420
                             // If we're not associated with an EntryID, handle capture group replacement
421
-                            if ((int)$redirect['associatedElementId'] === 0) {
422
-                                $redirect['redirectDestUrl'] = preg_replace(
421
+                            if ((int) $redirect[ 'associatedElementId' ] === 0) {
422
+                                $redirect[ 'redirectDestUrl' ] = preg_replace(
423 423
                                     $matchRegEx,
424
-                                    $redirect['redirectDestUrl'],
424
+                                    $redirect[ 'redirectDestUrl' ],
425 425
                                     $url
426 426
                                 );
427 427
                             }
@@ -440,7 +440,7 @@  discard block
 block discarded – undo
440 440
                                     'redirect' => &$redirect,
441 441
                                 ],
442 442
                             ];
443
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
443
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
444 444
                             if ($result) {
445 445
                                 $this->incrementRedirectHitCount($redirect);
446 446
                                 $this->saveRedirectToCache($url, $redirect);
@@ -467,7 +467,7 @@  discard block
 block discarded – undo
467 467
             Craft::t(
468 468
                 'retour',
469 469
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
470
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
470
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
471 471
             ),
472 472
             __METHOD__
473 473
         );
@@ -512,7 +512,7 @@  discard block
 block discarded – undo
512 512
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
513 513
             /** @var Plugin $plugin */
514 514
             if (method_exists($plugin, 'retourMatch')) {
515
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
515
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
516 516
             }
517 517
         }
518 518
 
@@ -533,12 +533,12 @@  discard block
 block discarded – undo
533 533
         }
534 534
         // Query the db table
535 535
         $query = (new Query())
536
-            ->from(['{{%retour_static_redirects}}'])
536
+            ->from([ '{{%retour_static_redirects}}' ])
537 537
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
538 538
         if ($siteId) {
539 539
             $query
540
-                ->where(['siteId' => $siteId])
541
-                ->orWhere(['siteId' => null]);
540
+                ->where([ 'siteId' => $siteId ])
541
+                ->orWhere([ 'siteId' => null ]);
542 542
         }
543 543
         if ($limit) {
544 544
             $query->limit($limit);
@@ -561,8 +561,8 @@  discard block
 block discarded – undo
561 561
     {
562 562
         // Query the db table
563 563
         $redirect = (new Query())
564
-            ->from(['{{%retour_static_redirects}}'])
565
-            ->where(['id' => $id])
564
+            ->from([ '{{%retour_static_redirects}}' ])
565
+            ->where([ 'id' => $id ])
566 566
             ->one();
567 567
 
568 568
         return $redirect;
@@ -580,16 +580,16 @@  discard block
 block discarded – undo
580 580
     {
581 581
         // Query the db table
582 582
         $query = (new Query())
583
-            ->from(['{{%retour_static_redirects}}'])
584
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
583
+            ->from([ '{{%retour_static_redirects}}' ])
584
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
585 585
             ;
586 586
         if ($siteId) {
587 587
             $query
588
-                ->andWhere(['or', [
588
+                ->andWhere([ 'or', [
589 589
                     'siteId' => $siteId,
590 590
                 ], [
591 591
                     'siteId' => null,
592
-                ]]);
592
+                ] ]);
593 593
         }
594 594
         $redirect = $query->one();
595 595
 
@@ -631,13 +631,13 @@  discard block
 block discarded – undo
631 631
     {
632 632
         if ($redirectConfig !== null) {
633 633
             $db = Craft::$app->getDb();
634
-            $redirectConfig['hitCount']++;
635
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
634
+            $redirectConfig[ 'hitCount' ]++;
635
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
636 636
             Craft::debug(
637 637
                 Craft::t(
638 638
                     'retour',
639 639
                     'Incrementing statistics for: {redirect}',
640
-                    ['redirect' => print_r($redirectConfig, true)]
640
+                    [ 'redirect' => print_r($redirectConfig, true) ]
641 641
                 ),
642 642
                 __METHOD__
643 643
             );
@@ -646,14 +646,14 @@  discard block
 block discarded – undo
646 646
                 $rowsAffected = $db->createCommand()->update(
647 647
                     '{{%retour_static_redirects}}',
648 648
                     [
649
-                        'hitCount' => $redirectConfig['hitCount'],
650
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
649
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
650
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
651 651
                     ],
652 652
                     [
653
-                        'id' => $redirectConfig['id'],
653
+                        'id' => $redirectConfig[ 'id' ],
654 654
                     ]
655 655
                 )->execute();
656
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
656
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
657 657
             } catch (\Exception $e) {
658 658
                 Craft::error($e->getMessage(), __METHOD__);
659 659
             }
@@ -672,7 +672,7 @@  discard block
 block discarded – undo
672 672
                 Craft::t(
673 673
                     'retour',
674 674
                     'Error validating redirect {id}: {errors}',
675
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
675
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
676 676
                 ),
677 677
                 __METHOD__
678 678
             );
@@ -682,32 +682,32 @@  discard block
 block discarded – undo
682 682
         // Get the validated model attributes and save them to the db
683 683
         $redirectConfig = $redirect->getAttributes();
684 684
         // 0 for a siteId needs to be converted to null
685
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
686
-            $redirectConfig['siteId'] = null;
685
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
686
+            $redirectConfig[ 'siteId' ] = null;
687 687
         }
688 688
         // Throw an event to before saving the redirect
689 689
         $db = Craft::$app->getDb();
690 690
         // See if a redirect exists with this source URL already
691
-        if ((int)$redirectConfig['id'] === 0) {
691
+        if ((int) $redirectConfig[ 'id' ] === 0) {
692 692
             // Query the db table
693 693
             $redirect = (new Query())
694
-                ->from(['{{%retour_static_redirects}}'])
695
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
696
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
694
+                ->from([ '{{%retour_static_redirects}}' ])
695
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
696
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
697 697
                 ->one();
698 698
             // If it exists, update it rather than having duplicates
699 699
             if (!empty($redirect)) {
700
-                $redirectConfig['id'] = $redirect['id'];
700
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
701 701
             }
702 702
         }
703 703
         // Trigger a 'beforeSaveRedirect' event
704
-        $isNew = (int)$redirectConfig['id'] === 0;
704
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
705 705
         $event = new RedirectEvent([
706 706
             'isNew' => $isNew,
707
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
708
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
709
-            'matchType' => $redirectConfig['redirectSrcMatch'],
710
-            'redirectType' => $redirectConfig['redirectHttpCode'],
707
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
708
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
709
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
710
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
711 711
         ]);
712 712
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
713 713
         if (!$event->isValid) {
@@ -719,7 +719,7 @@  discard block
 block discarded – undo
719 719
                 Craft::t(
720 720
                     'retour',
721 721
                     'Updating existing redirect: {redirect}',
722
-                    ['redirect' => print_r($redirectConfig, true)]
722
+                    [ 'redirect' => print_r($redirectConfig, true) ]
723 723
                 ),
724 724
                 __METHOD__
725 725
             );
@@ -729,7 +729,7 @@  discard block
 block discarded – undo
729 729
                     '{{%retour_static_redirects}}',
730 730
                     $redirectConfig,
731 731
                     [
732
-                        'id' => $redirectConfig['id'],
732
+                        'id' => $redirectConfig[ 'id' ],
733 733
                     ]
734 734
                 )->execute();
735 735
             } catch (Exception $e) {
@@ -740,11 +740,11 @@  discard block
 block discarded – undo
740 740
                 Craft::t(
741 741
                     'retour',
742 742
                     'Creating new redirect: {redirect}',
743
-                    ['redirect' => print_r($redirectConfig, true)]
743
+                    [ 'redirect' => print_r($redirectConfig, true) ]
744 744
                 ),
745 745
                 __METHOD__
746 746
             );
747
-            unset($redirectConfig['id']);
747
+            unset($redirectConfig[ 'id' ]);
748 748
             // Create a new record
749 749
             try {
750 750
                 $db->createCommand()->insert(
@@ -757,15 +757,15 @@  discard block
 block discarded – undo
757 757
         }
758 758
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
759 759
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
760
-            $redirectConfig['redirectDestUrl'],
761
-            $redirectConfig['siteId']
760
+            $redirectConfig[ 'redirectDestUrl' ],
761
+            $redirectConfig[ 'siteId' ]
762 762
         );
763 763
         if ($testRedirectConfig !== null) {
764 764
             Craft::debug(
765 765
                 Craft::t(
766 766
                     'retour',
767 767
                     'Deleting redirect to prevent a loop: {redirect}',
768
-                    ['redirect' => print_r($testRedirectConfig, true)]
768
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
769 769
                 ),
770 770
                 __METHOD__
771 771
             );
@@ -773,7 +773,7 @@  discard block
 block discarded – undo
773 773
             try {
774 774
                 $db->createCommand()->delete(
775 775
                     '{{%retour_static_redirects}}',
776
-                    ['id' => $testRedirectConfig['id']]
776
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
777 777
                 )->execute();
778 778
             } catch (Exception $e) {
779 779
                 Craft::error($e->getMessage(), __METHOD__);
@@ -806,10 +806,10 @@  discard block
 block discarded – undo
806 806
      */
807 807
     public function excludeUri($uri): bool
808 808
     {
809
-        $uri = '/'.ltrim($uri, '/');
809
+        $uri = '/' . ltrim($uri, '/');
810 810
         if (!empty(Retour::$settings->excludePatterns)) {
811 811
             foreach (Retour::$settings->excludePatterns as $excludePattern) {
812
-                $pattern = '`'.$excludePattern['pattern'].'`i';
812
+                $pattern = '`' . $excludePattern[ 'pattern' ] . '`i';
813 813
                 if (preg_match($pattern, $uri) === 1) {
814 814
                     return true;
815 815
                 }
Please login to merge, or discard this patch.