Passed
Push — develop ( cee39f...4b73a5 )
by Andrew
04:44
created
src/services/Redirects.php 1 patch
Spacing   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -166,7 +166,7 @@  discard block
 block discarded – undo
166 166
                 Craft::t(
167 167
                     'retour',
168 168
                     '404 full URL: {fullUrl}, 404 path only: {pathOnly}',
169
-                    ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
169
+                    [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
170 170
                 ),
171 171
                 __METHOD__
172 172
             );
@@ -200,7 +200,7 @@  discard block
 block discarded – undo
200 200
         $response = Craft::$app->getResponse();
201 201
         if ($redirect !== null) {
202 202
             // Figure out what type of source matching was done
203
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
203
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
204 204
             switch ($redirectSrcMatch) {
205 205
                 case 'pathonly':
206 206
                     $url = $pathOnly;
@@ -212,11 +212,11 @@  discard block
 block discarded – undo
212 212
                     $url = $pathOnly;
213 213
                     break;
214 214
             }
215
-            $dest = $redirect['redirectDestUrl'];
215
+            $dest = $redirect[ 'redirectDestUrl' ];
216 216
             // If this isn't a full URL, make it one based on the appropriate site
217 217
             if (!UrlHelper::isFullUrl($dest)) {
218 218
                 try {
219
-                    $dest = UrlHelper::siteUrl($dest, null, null, $redirect['siteId'] ?? null);
219
+                    $dest = UrlHelper::siteUrl($dest, null, null, $redirect[ 'siteId' ] ?? null);
220 220
                 } catch (\yii\base\Exception $e) {
221 221
                 }
222 222
             }
@@ -226,12 +226,12 @@  discard block
 block discarded – undo
226 226
                     $dest .= '?' . $request->getQueryStringWithoutPath();
227 227
                 }
228 228
             }
229
-            $status = $redirect['redirectHttpCode'];
229
+            $status = $redirect[ 'redirectHttpCode' ];
230 230
             Craft::info(
231 231
                 Craft::t(
232 232
                     'retour',
233 233
                     'Redirecting {url} to {dest} with status {status}',
234
-                    ['url' => $url, 'dest' => $dest, 'status' => $status]
234
+                    [ 'url' => $url, 'dest' => $dest, 'status' => $status ]
235 235
                 ),
236 236
                 __METHOD__
237 237
             );
@@ -313,13 +313,13 @@  discard block
 block discarded – undo
313 313
     public function getRedirectFromCache($url, int $siteId = 0)
314 314
     {
315 315
         $cache = Craft::$app->getCache();
316
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
316
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
317 317
         $redirect = $cache->get($cacheKey);
318 318
         Craft::info(
319 319
             Craft::t(
320 320
                 'retour',
321 321
                 'Cached redirect hit for {url}',
322
-                ['url' => $url]
322
+                [ 'url' => $url ]
323 323
             ),
324 324
             __METHOD__
325 325
         );
@@ -341,12 +341,12 @@  discard block
 block discarded – undo
341 341
         } catch (SiteNotFoundException $e) {
342 342
             $siteId = 1;
343 343
         }
344
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
344
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
345 345
         // Create the dependency tags
346 346
         $dependency = new TagDependency([
347 347
             'tags' => [
348 348
                 $this::GLOBAL_REDIRECTS_CACHE_TAG,
349
-                $this::GLOBAL_REDIRECTS_CACHE_TAG.$siteId,
349
+                $this::GLOBAL_REDIRECTS_CACHE_TAG . $siteId,
350 350
             ],
351 351
         ]);
352 352
         $cache->set($cacheKey, $redirect, Retour::$cacheDuration, $dependency);
@@ -354,7 +354,7 @@  discard block
 block discarded – undo
354 354
             Craft::t(
355 355
                 'retour',
356 356
                 'Cached redirect saved for {url}',
357
-                ['url' => $url]
357
+                [ 'url' => $url ]
358 358
             ),
359 359
             __METHOD__
360 360
         );
@@ -384,8 +384,8 @@  discard block
 block discarded – undo
384 384
         // Iterate through the redirects
385 385
         foreach ($redirects as $redirect) {
386 386
             // Figure out what type of source matching to do
387
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
388
-            $redirectEnabled = (bool)$redirect['enabled'];
387
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
388
+            $redirectEnabled = (bool) $redirect[ 'enabled' ];
389 389
             if ($redirectEnabled === true) {
390 390
                 switch ($redirectSrcMatch) {
391 391
                     case 'pathonly':
@@ -398,11 +398,11 @@  discard block
 block discarded – undo
398 398
                         $url = $pathOnly;
399 399
                         break;
400 400
                 }
401
-                $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
401
+                $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
402 402
                 switch ($redirectMatchType) {
403 403
                     // Do a straight up match
404 404
                     case 'exactmatch':
405
-                        if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
405
+                        if (strcasecmp($redirect[ 'redirectSrcUrlParsed' ], $url) === 0) {
406 406
                             $this->incrementRedirectHitCount($redirect);
407 407
                             $this->saveRedirectToCache($url, $redirect);
408 408
 
@@ -412,14 +412,14 @@  discard block
 block discarded – undo
412 412
 
413 413
                     // Do a regex match
414 414
                     case 'regexmatch':
415
-                        $matchRegEx = '`'.$redirect['redirectSrcUrlParsed'].'`i';
415
+                        $matchRegEx = '`' . $redirect[ 'redirectSrcUrlParsed' ] . '`i';
416 416
                         if (preg_match($matchRegEx, $url) === 1) {
417 417
                             $this->incrementRedirectHitCount($redirect);
418 418
                             // If we're not associated with an EntryID, handle capture group replacement
419
-                            if ((int)$redirect['associatedElementId'] === 0) {
420
-                                $redirect['redirectDestUrl'] = preg_replace(
419
+                            if ((int) $redirect[ 'associatedElementId' ] === 0) {
420
+                                $redirect[ 'redirectDestUrl' ] = preg_replace(
421 421
                                     $matchRegEx,
422
-                                    $redirect['redirectDestUrl'],
422
+                                    $redirect[ 'redirectDestUrl' ],
423 423
                                     $url
424 424
                                 );
425 425
                             }
@@ -438,7 +438,7 @@  discard block
 block discarded – undo
438 438
                                     'redirect' => &$redirect,
439 439
                                 ],
440 440
                             ];
441
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
441
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
442 442
                             if ($result) {
443 443
                                 $this->incrementRedirectHitCount($redirect);
444 444
                                 $this->saveRedirectToCache($url, $redirect);
@@ -465,7 +465,7 @@  discard block
 block discarded – undo
465 465
             Craft::t(
466 466
                 'retour',
467 467
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
468
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
468
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
469 469
             ),
470 470
             __METHOD__
471 471
         );
@@ -501,7 +501,7 @@  discard block
 block discarded – undo
501 501
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
502 502
             /** @var Plugin $plugin */
503 503
             if (method_exists($plugin, 'retourMatch')) {
504
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
504
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
505 505
             }
506 506
         }
507 507
 
@@ -522,12 +522,12 @@  discard block
 block discarded – undo
522 522
         }
523 523
         // Query the db table
524 524
         $query = (new Query())
525
-            ->from(['{{%retour_static_redirects}}'])
525
+            ->from([ '{{%retour_static_redirects}}' ])
526 526
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
527 527
         if ($siteId) {
528 528
             $query
529
-                ->where(['siteId' => $siteId])
530
-                ->orWhere(['siteId' => null]);
529
+                ->where([ 'siteId' => $siteId ])
530
+                ->orWhere([ 'siteId' => null ]);
531 531
         }
532 532
         if ($limit) {
533 533
             $query->limit($limit);
@@ -550,8 +550,8 @@  discard block
 block discarded – undo
550 550
     {
551 551
         // Query the db table
552 552
         $redirect = (new Query())
553
-            ->from(['{{%retour_static_redirects}}'])
554
-            ->where(['id' => $id])
553
+            ->from([ '{{%retour_static_redirects}}' ])
554
+            ->where([ 'id' => $id ])
555 555
             ->one();
556 556
 
557 557
         return $redirect;
@@ -569,16 +569,16 @@  discard block
 block discarded – undo
569 569
     {
570 570
         // Query the db table
571 571
         $query = (new Query())
572
-            ->from(['{{%retour_static_redirects}}'])
573
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
572
+            ->from([ '{{%retour_static_redirects}}' ])
573
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
574 574
             ;
575 575
         if ($siteId) {
576 576
             $query
577
-                ->andWhere(['or', [
577
+                ->andWhere([ 'or', [
578 578
                     'siteId' => $siteId,
579 579
                 ], [
580 580
                     'siteId' => null,
581
-                ]]);
581
+                ] ]);
582 582
         }
583 583
         $redirect = $query->one();
584 584
 
@@ -620,13 +620,13 @@  discard block
 block discarded – undo
620 620
     {
621 621
         if ($redirectConfig !== null) {
622 622
             $db = Craft::$app->getDb();
623
-            $redirectConfig['hitCount']++;
624
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
623
+            $redirectConfig[ 'hitCount' ]++;
624
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
625 625
             Craft::debug(
626 626
                 Craft::t(
627 627
                     'retour',
628 628
                     'Incrementing statistics for: {redirect}',
629
-                    ['redirect' => print_r($redirectConfig, true)]
629
+                    [ 'redirect' => print_r($redirectConfig, true) ]
630 630
                 ),
631 631
                 __METHOD__
632 632
             );
@@ -635,14 +635,14 @@  discard block
 block discarded – undo
635 635
                 $rowsAffected = $db->createCommand()->update(
636 636
                     '{{%retour_static_redirects}}',
637 637
                     [
638
-                        'hitCount' => $redirectConfig['hitCount'],
639
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
638
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
639
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
640 640
                     ],
641 641
                     [
642
-                        'id' => $redirectConfig['id'],
642
+                        'id' => $redirectConfig[ 'id' ],
643 643
                     ]
644 644
                 )->execute();
645
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
645
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
646 646
             } catch (Exception $e) {
647 647
                 Craft::error($e->getMessage(), __METHOD__);
648 648
             }
@@ -661,7 +661,7 @@  discard block
 block discarded – undo
661 661
                 Craft::t(
662 662
                     'retour',
663 663
                     'Error validating redirect {id}: {errors}',
664
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
664
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
665 665
                 ),
666 666
                 __METHOD__
667 667
             );
@@ -671,32 +671,32 @@  discard block
 block discarded – undo
671 671
         // Get the validated model attributes and save them to the db
672 672
         $redirectConfig = $redirect->getAttributes();
673 673
         // 0 for a siteId needs to be converted to null
674
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
675
-            $redirectConfig['siteId'] = null;
674
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
675
+            $redirectConfig[ 'siteId' ] = null;
676 676
         }
677 677
         // Throw an event to before saving the redirect
678 678
         $db = Craft::$app->getDb();
679 679
         // See if a redirect exists with this source URL already
680
-        if ((int)$redirectConfig['id'] === 0) {
680
+        if ((int) $redirectConfig[ 'id' ] === 0) {
681 681
             // Query the db table
682 682
             $redirect = (new Query())
683
-                ->from(['{{%retour_static_redirects}}'])
684
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
685
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
683
+                ->from([ '{{%retour_static_redirects}}' ])
684
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
685
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
686 686
                 ->one();
687 687
             // If it exists, update it rather than having duplicates
688 688
             if (!empty($redirect)) {
689
-                $redirectConfig['id'] = $redirect['id'];
689
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
690 690
             }
691 691
         }
692 692
         // Trigger a 'beforeSaveRedirect' event
693
-        $isNew = (int)$redirectConfig['id'] === 0;
693
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
694 694
         $event = new RedirectEvent([
695 695
             'isNew' => $isNew,
696
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
697
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
698
-            'matchType' => $redirectConfig['redirectSrcMatch'],
699
-            'redirectType' => $redirectConfig['redirectHttpCode'],
696
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
697
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
698
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
699
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
700 700
         ]);
701 701
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
702 702
         if (!$event->isValid) {
@@ -708,7 +708,7 @@  discard block
 block discarded – undo
708 708
                 Craft::t(
709 709
                     'retour',
710 710
                     'Updating existing redirect: {redirect}',
711
-                    ['redirect' => print_r($redirectConfig, true)]
711
+                    [ 'redirect' => print_r($redirectConfig, true) ]
712 712
                 ),
713 713
                 __METHOD__
714 714
             );
@@ -718,7 +718,7 @@  discard block
 block discarded – undo
718 718
                     '{{%retour_static_redirects}}',
719 719
                     $redirectConfig,
720 720
                     [
721
-                        'id' => $redirectConfig['id'],
721
+                        'id' => $redirectConfig[ 'id' ],
722 722
                     ]
723 723
                 )->execute();
724 724
             } catch (Exception $e) {
@@ -729,11 +729,11 @@  discard block
 block discarded – undo
729 729
                 Craft::t(
730 730
                     'retour',
731 731
                     'Creating new redirect: {redirect}',
732
-                    ['redirect' => print_r($redirectConfig, true)]
732
+                    [ 'redirect' => print_r($redirectConfig, true) ]
733 733
                 ),
734 734
                 __METHOD__
735 735
             );
736
-            unset($redirectConfig['id']);
736
+            unset($redirectConfig[ 'id' ]);
737 737
             // Create a new record
738 738
             try {
739 739
                 $db->createCommand()->insert(
@@ -746,15 +746,15 @@  discard block
 block discarded – undo
746 746
         }
747 747
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
748 748
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
749
-            $redirectConfig['redirectDestUrl'],
750
-            $redirectConfig['siteId']
749
+            $redirectConfig[ 'redirectDestUrl' ],
750
+            $redirectConfig[ 'siteId' ]
751 751
         );
752 752
         if ($testRedirectConfig !== null) {
753 753
             Craft::debug(
754 754
                 Craft::t(
755 755
                     'retour',
756 756
                     'Deleting redirect to prevent a loop: {redirect}',
757
-                    ['redirect' => print_r($testRedirectConfig, true)]
757
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
758 758
                 ),
759 759
                 __METHOD__
760 760
             );
@@ -762,7 +762,7 @@  discard block
 block discarded – undo
762 762
             try {
763 763
                 $db->createCommand()->delete(
764 764
                     '{{%retour_static_redirects}}',
765
-                    ['id' => $testRedirectConfig['id']]
765
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
766 766
                 )->execute();
767 767
             } catch (Exception $e) {
768 768
                 Craft::error($e->getMessage(), __METHOD__);
@@ -795,10 +795,10 @@  discard block
 block discarded – undo
795 795
      */
796 796
     public function excludeUri($uri): bool
797 797
     {
798
-        $uri = '/'.ltrim($uri, '/');
798
+        $uri = '/' . ltrim($uri, '/');
799 799
         if (!empty(Retour::$settings->excludePatterns)) {
800 800
             foreach (Retour::$settings->excludePatterns as $excludePattern) {
801
-                $pattern = '`'.$excludePattern['pattern'].'`i';
801
+                $pattern = '`' . $excludePattern[ 'pattern' ] . '`i';
802 802
                 if (preg_match($pattern, $uri) === 1) {
803 803
                     return true;
804 804
                 }
Please login to merge, or discard this patch.