Passed
Push — develop ( d731e7...4ca4eb )
by Andrew
10: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,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
                                 }
@@ -445,7 +445,7 @@  discard block
 block discarded – undo
445 445
                                     'redirect' => &$redirect,
446 446
                                 ],
447 447
                             ];
448
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
448
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
449 449
                             if ($result) {
450 450
                                 $this->incrementRedirectHitCount($redirect);
451 451
                                 $this->saveRedirectToCache($url, $redirect);
@@ -472,7 +472,7 @@  discard block
 block discarded – undo
472 472
             Craft::t(
473 473
                 'retour',
474 474
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
475
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
475
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
476 476
             ),
477 477
             __METHOD__
478 478
         );
@@ -517,7 +517,7 @@  discard block
 block discarded – undo
517 517
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
518 518
             /** @var Plugin $plugin */
519 519
             if (method_exists($plugin, 'retourMatch')) {
520
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
520
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
521 521
             }
522 522
         }
523 523
 
@@ -538,12 +538,12 @@  discard block
 block discarded – undo
538 538
         }
539 539
         // Query the db table
540 540
         $query = (new Query())
541
-            ->from(['{{%retour_static_redirects}}'])
541
+            ->from([ '{{%retour_static_redirects}}' ])
542 542
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
543 543
         if ($siteId) {
544 544
             $query
545
-                ->where(['siteId' => $siteId])
546
-                ->orWhere(['siteId' => null]);
545
+                ->where([ 'siteId' => $siteId ])
546
+                ->orWhere([ 'siteId' => null ]);
547 547
         }
548 548
         if ($limit) {
549 549
             $query->limit($limit);
@@ -566,8 +566,8 @@  discard block
 block discarded – undo
566 566
     {
567 567
         // Query the db table
568 568
         $redirect = (new Query())
569
-            ->from(['{{%retour_static_redirects}}'])
570
-            ->where(['id' => $id])
569
+            ->from([ '{{%retour_static_redirects}}' ])
570
+            ->where([ 'id' => $id ])
571 571
             ->one();
572 572
 
573 573
         return $redirect;
@@ -585,16 +585,16 @@  discard block
 block discarded – undo
585 585
     {
586 586
         // Query the db table
587 587
         $query = (new Query())
588
-            ->from(['{{%retour_static_redirects}}'])
589
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
588
+            ->from([ '{{%retour_static_redirects}}' ])
589
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
590 590
             ;
591 591
         if ($siteId) {
592 592
             $query
593
-                ->andWhere(['or', [
593
+                ->andWhere([ 'or', [
594 594
                     'siteId' => $siteId,
595 595
                 ], [
596 596
                     'siteId' => null,
597
-                ]]);
597
+                ] ]);
598 598
         }
599 599
         $redirect = $query->one();
600 600
 
@@ -636,13 +636,13 @@  discard block
 block discarded – undo
636 636
     {
637 637
         if ($redirectConfig !== null) {
638 638
             $db = Craft::$app->getDb();
639
-            $redirectConfig['hitCount']++;
640
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
639
+            $redirectConfig[ 'hitCount' ]++;
640
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
641 641
             Craft::debug(
642 642
                 Craft::t(
643 643
                     'retour',
644 644
                     'Incrementing statistics for: {redirect}',
645
-                    ['redirect' => print_r($redirectConfig, true)]
645
+                    [ 'redirect' => print_r($redirectConfig, true) ]
646 646
                 ),
647 647
                 __METHOD__
648 648
             );
@@ -651,14 +651,14 @@  discard block
 block discarded – undo
651 651
                 $rowsAffected = $db->createCommand()->update(
652 652
                     '{{%retour_static_redirects}}',
653 653
                     [
654
-                        'hitCount' => $redirectConfig['hitCount'],
655
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
654
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
655
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
656 656
                     ],
657 657
                     [
658
-                        'id' => $redirectConfig['id'],
658
+                        'id' => $redirectConfig[ 'id' ],
659 659
                     ]
660 660
                 )->execute();
661
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
661
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
662 662
             } catch (\Exception $e) {
663 663
                 Craft::error($e->getMessage(), __METHOD__);
664 664
             }
@@ -677,7 +677,7 @@  discard block
 block discarded – undo
677 677
                 Craft::t(
678 678
                     'retour',
679 679
                     'Error validating redirect {id}: {errors}',
680
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
680
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
681 681
                 ),
682 682
                 __METHOD__
683 683
             );
@@ -687,32 +687,32 @@  discard block
 block discarded – undo
687 687
         // Get the validated model attributes and save them to the db
688 688
         $redirectConfig = $redirect->getAttributes();
689 689
         // 0 for a siteId needs to be converted to null
690
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
691
-            $redirectConfig['siteId'] = null;
690
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
691
+            $redirectConfig[ 'siteId' ] = null;
692 692
         }
693 693
         // Throw an event to before saving the redirect
694 694
         $db = Craft::$app->getDb();
695 695
         // See if a redirect exists with this source URL already
696
-        if ((int)$redirectConfig['id'] === 0) {
696
+        if ((int) $redirectConfig[ 'id' ] === 0) {
697 697
             // Query the db table
698 698
             $redirect = (new Query())
699
-                ->from(['{{%retour_static_redirects}}'])
700
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
701
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
699
+                ->from([ '{{%retour_static_redirects}}' ])
700
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
701
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
702 702
                 ->one();
703 703
             // If it exists, update it rather than having duplicates
704 704
             if (!empty($redirect)) {
705
-                $redirectConfig['id'] = $redirect['id'];
705
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
706 706
             }
707 707
         }
708 708
         // Trigger a 'beforeSaveRedirect' event
709
-        $isNew = (int)$redirectConfig['id'] === 0;
709
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
710 710
         $event = new RedirectEvent([
711 711
             'isNew' => $isNew,
712
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
713
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
714
-            'matchType' => $redirectConfig['redirectSrcMatch'],
715
-            'redirectType' => $redirectConfig['redirectHttpCode'],
712
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
713
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
714
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
715
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
716 716
         ]);
717 717
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
718 718
         if (!$event->isValid) {
@@ -724,7 +724,7 @@  discard block
 block discarded – undo
724 724
                 Craft::t(
725 725
                     'retour',
726 726
                     'Updating existing redirect: {redirect}',
727
-                    ['redirect' => print_r($redirectConfig, true)]
727
+                    [ 'redirect' => print_r($redirectConfig, true) ]
728 728
                 ),
729 729
                 __METHOD__
730 730
             );
@@ -734,7 +734,7 @@  discard block
 block discarded – undo
734 734
                     '{{%retour_static_redirects}}',
735 735
                     $redirectConfig,
736 736
                     [
737
-                        'id' => $redirectConfig['id'],
737
+                        'id' => $redirectConfig[ 'id' ],
738 738
                     ]
739 739
                 )->execute();
740 740
             } catch (Exception $e) {
@@ -745,11 +745,11 @@  discard block
 block discarded – undo
745 745
                 Craft::t(
746 746
                     'retour',
747 747
                     'Creating new redirect: {redirect}',
748
-                    ['redirect' => print_r($redirectConfig, true)]
748
+                    [ 'redirect' => print_r($redirectConfig, true) ]
749 749
                 ),
750 750
                 __METHOD__
751 751
             );
752
-            unset($redirectConfig['id']);
752
+            unset($redirectConfig[ 'id' ]);
753 753
             // Create a new record
754 754
             try {
755 755
                 $db->createCommand()->insert(
@@ -762,15 +762,15 @@  discard block
 block discarded – undo
762 762
         }
763 763
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
764 764
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
765
-            $redirectConfig['redirectDestUrl'],
766
-            $redirectConfig['siteId']
765
+            $redirectConfig[ 'redirectDestUrl' ],
766
+            $redirectConfig[ 'siteId' ]
767 767
         );
768 768
         if ($testRedirectConfig !== null) {
769 769
             Craft::debug(
770 770
                 Craft::t(
771 771
                     'retour',
772 772
                     'Deleting redirect to prevent a loop: {redirect}',
773
-                    ['redirect' => print_r($testRedirectConfig, true)]
773
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
774 774
                 ),
775 775
                 __METHOD__
776 776
             );
@@ -778,7 +778,7 @@  discard block
 block discarded – undo
778 778
             try {
779 779
                 $db->createCommand()->delete(
780 780
                     '{{%retour_static_redirects}}',
781
-                    ['id' => $testRedirectConfig['id']]
781
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
782 782
                 )->execute();
783 783
             } catch (Exception $e) {
784 784
                 Craft::error($e->getMessage(), __METHOD__);
@@ -811,10 +811,10 @@  discard block
 block discarded – undo
811 811
      */
812 812
     public function excludeUri($uri): bool
813 813
     {
814
-        $uri = '/'.ltrim($uri, '/');
814
+        $uri = '/' . ltrim($uri, '/');
815 815
         if (!empty(Retour::$settings->excludePatterns)) {
816 816
             foreach (Retour::$settings->excludePatterns as $excludePattern) {
817
-                $pattern = '`'.$excludePattern['pattern'].'`i';
817
+                $pattern = '`' . $excludePattern[ 'pattern' ] . '`i';
818 818
                 try {
819 819
                     if (preg_match($pattern, $uri) === 1) {
820 820
                         return true;
Please login to merge, or discard this patch.