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