Passed
Push — develop ( 57bc07...b573f4 )
by Andrew
05:09
created
src/services/Redirects.php 1 patch
Spacing   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -128,7 +128,7 @@  discard block
 block discarded – undo
128 128
                 Craft::t(
129 129
                     'retour',
130 130
                     '404 full URL: {fullUrl}, 404 path only: {pathOnly}',
131
-                    ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
131
+                    [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
132 132
                 ),
133 133
                 __METHOD__
134 134
             );
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
         $response = Craft::$app->getResponse();
163 163
         if ($redirect !== null) {
164 164
             // Figure out what type of source matching was done
165
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
165
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
166 166
             switch ($redirectSrcMatch) {
167 167
                 case 'pathonly':
168 168
                     $url = $pathOnly;
@@ -174,11 +174,11 @@  discard block
 block discarded – undo
174 174
                     $url = $pathOnly;
175 175
                     break;
176 176
             }
177
-            $dest = $redirect['redirectDestUrl'];
177
+            $dest = $redirect[ 'redirectDestUrl' ];
178 178
             // If this isn't a full URL, make it one based on the appropriate site
179 179
             if (!UrlHelper::isFullUrl($dest)) {
180 180
                 try {
181
-                    $dest = UrlHelper::siteUrl($dest, null, null, $redirect['siteId'] ?? null);
181
+                    $dest = UrlHelper::siteUrl($dest, null, null, $redirect[ 'siteId' ] ?? null);
182 182
                 } catch (\yii\base\Exception $e) {
183 183
                 }
184 184
             }
@@ -188,12 +188,12 @@  discard block
 block discarded – undo
188 188
                     $dest .= '?' . $request->getQueryStringWithoutPath();
189 189
                 }
190 190
             }
191
-            $status = $redirect['redirectHttpCode'];
191
+            $status = $redirect[ 'redirectHttpCode' ];
192 192
             Craft::info(
193 193
                 Craft::t(
194 194
                     'retour',
195 195
                     'Redirecting {url} to {dest} with status {status}',
196
-                    ['url' => $url, 'dest' => $dest, 'status' => $status]
196
+                    [ 'url' => $url, 'dest' => $dest, 'status' => $status ]
197 197
                 ),
198 198
                 __METHOD__
199 199
             );
@@ -273,13 +273,13 @@  discard block
 block discarded – undo
273 273
     public function getRedirectFromCache($url, int $siteId = 0)
274 274
     {
275 275
         $cache = Craft::$app->getCache();
276
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
276
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
277 277
         $redirect = $cache->get($cacheKey);
278 278
         Craft::info(
279 279
             Craft::t(
280 280
                 'retour',
281 281
                 'Cached redirect hit for {url}',
282
-                ['url' => $url]
282
+                [ 'url' => $url ]
283 283
             ),
284 284
             __METHOD__
285 285
         );
@@ -301,12 +301,12 @@  discard block
 block discarded – undo
301 301
         } catch (SiteNotFoundException $e) {
302 302
             $siteId = 1;
303 303
         }
304
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
304
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
305 305
         // Create the dependency tags
306 306
         $dependency = new TagDependency([
307 307
             'tags' => [
308 308
                 $this::GLOBAL_REDIRECTS_CACHE_TAG,
309
-                $this::GLOBAL_REDIRECTS_CACHE_TAG.$siteId,
309
+                $this::GLOBAL_REDIRECTS_CACHE_TAG . $siteId,
310 310
             ],
311 311
         ]);
312 312
         $cache->set($cacheKey, $redirect, Retour::$cacheDuration, $dependency);
@@ -314,7 +314,7 @@  discard block
 block discarded – undo
314 314
             Craft::t(
315 315
                 'retour',
316 316
                 'Cached redirect saved for {url}',
317
-                ['url' => $url]
317
+                [ 'url' => $url ]
318 318
             ),
319 319
             __METHOD__
320 320
         );
@@ -332,8 +332,8 @@  discard block
 block discarded – undo
332 332
         $result = null;
333 333
         foreach ($redirects as $redirect) {
334 334
             // Figure out what type of source matching to do
335
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
336
-            $redirectEnabled = (bool)$redirect['enabled'];
335
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
336
+            $redirectEnabled = (bool) $redirect[ 'enabled' ];
337 337
             if ($redirectEnabled === true) {
338 338
                 switch ($redirectSrcMatch) {
339 339
                     case 'pathonly':
@@ -346,11 +346,11 @@  discard block
 block discarded – undo
346 346
                         $url = $pathOnly;
347 347
                         break;
348 348
                 }
349
-                $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
349
+                $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
350 350
                 switch ($redirectMatchType) {
351 351
                     // Do a straight up match
352 352
                     case 'exactmatch':
353
-                        if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
353
+                        if (strcasecmp($redirect[ 'redirectSrcUrlParsed' ], $url) === 0) {
354 354
                             $this->incrementRedirectHitCount($redirect);
355 355
                             $this->saveRedirectToCache($url, $redirect);
356 356
 
@@ -360,14 +360,14 @@  discard block
 block discarded – undo
360 360
 
361 361
                     // Do a regex match
362 362
                     case 'regexmatch':
363
-                        $matchRegEx = '`'.$redirect['redirectSrcUrlParsed'].'`i';
363
+                        $matchRegEx = '`' . $redirect[ 'redirectSrcUrlParsed' ] . '`i';
364 364
                         if (preg_match($matchRegEx, $url) === 1) {
365 365
                             $this->incrementRedirectHitCount($redirect);
366 366
                             // If we're not associated with an EntryID, handle capture group replacement
367
-                            if ((int)$redirect['associatedElementId'] === 0) {
368
-                                $redirect['redirectDestUrl'] = preg_replace(
367
+                            if ((int) $redirect[ 'associatedElementId' ] === 0) {
368
+                                $redirect[ 'redirectDestUrl' ] = preg_replace(
369 369
                                     $matchRegEx,
370
-                                    $redirect['redirectDestUrl'],
370
+                                    $redirect[ 'redirectDestUrl' ],
371 371
                                     $url
372 372
                                 );
373 373
                             }
@@ -386,7 +386,7 @@  discard block
 block discarded – undo
386 386
                                     'redirect' => &$redirect,
387 387
                                 ],
388 388
                             ];
389
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
389
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
390 390
                             if ($result) {
391 391
                                 $this->incrementRedirectHitCount($redirect);
392 392
                                 $this->saveRedirectToCache($url, $redirect);
@@ -402,7 +402,7 @@  discard block
 block discarded – undo
402 402
             Craft::t(
403 403
                 'retour',
404 404
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
405
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
405
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
406 406
             ),
407 407
             __METHOD__
408 408
         );
@@ -426,7 +426,7 @@  discard block
 block discarded – undo
426 426
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
427 427
             /** @var Plugin $plugin */
428 428
             if (method_exists($plugin, 'retourMatch')) {
429
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
429
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
430 430
             }
431 431
         }
432 432
 
@@ -447,12 +447,12 @@  discard block
 block discarded – undo
447 447
         }
448 448
         // Query the db table
449 449
         $query = (new Query())
450
-            ->from(['{{%retour_static_redirects}}'])
450
+            ->from([ '{{%retour_static_redirects}}' ])
451 451
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
452 452
         if ($siteId) {
453 453
             $query
454
-                ->where(['siteId' => $siteId])
455
-                ->orWhere(['siteId' => null]);
454
+                ->where([ 'siteId' => $siteId ])
455
+                ->orWhere([ 'siteId' => null ]);
456 456
         }
457 457
         if ($limit) {
458 458
             $query->limit($limit);
@@ -475,8 +475,8 @@  discard block
 block discarded – undo
475 475
     {
476 476
         // Query the db table
477 477
         $redirect = (new Query())
478
-            ->from(['{{%retour_static_redirects}}'])
479
-            ->where(['id' => $id])
478
+            ->from([ '{{%retour_static_redirects}}' ])
479
+            ->where([ 'id' => $id ])
480 480
             ->one();
481 481
 
482 482
         return $redirect;
@@ -494,13 +494,13 @@  discard block
 block discarded – undo
494 494
     {
495 495
         // Query the db table
496 496
         $query = (new Query())
497
-            ->from(['{{%retour_static_redirects}}'])
498
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
497
+            ->from([ '{{%retour_static_redirects}}' ])
498
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
499 499
             ;
500 500
         if ($siteId) {
501 501
             $query
502
-                ->andWhere(['siteId' => $siteId])
503
-                ->orWhere(['siteId' => null]);
502
+                ->andWhere([ 'siteId' => $siteId ])
503
+                ->orWhere([ 'siteId' => null ]);
504 504
         }
505 505
         $redirect = $query->one();
506 506
 
@@ -542,13 +542,13 @@  discard block
 block discarded – undo
542 542
     {
543 543
         if ($redirectConfig !== null) {
544 544
             $db = Craft::$app->getDb();
545
-            $redirectConfig['hitCount']++;
546
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
545
+            $redirectConfig[ 'hitCount' ]++;
546
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
547 547
             Craft::debug(
548 548
                 Craft::t(
549 549
                     'retour',
550 550
                     'Incrementing statistics for: {redirect}',
551
-                    ['redirect' => print_r($redirectConfig, true)]
551
+                    [ 'redirect' => print_r($redirectConfig, true) ]
552 552
                 ),
553 553
                 __METHOD__
554 554
             );
@@ -557,14 +557,14 @@  discard block
 block discarded – undo
557 557
                 $rowsAffected = $db->createCommand()->update(
558 558
                     '{{%retour_static_redirects}}',
559 559
                     [
560
-                        'hitCount' => $redirectConfig['hitCount'],
561
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
560
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
561
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
562 562
                     ],
563 563
                     [
564
-                        'id' => $redirectConfig['id'],
564
+                        'id' => $redirectConfig[ 'id' ],
565 565
                     ]
566 566
                 )->execute();
567
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
567
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
568 568
             } catch (Exception $e) {
569 569
                 Craft::error($e->getMessage(), __METHOD__);
570 570
             }
@@ -583,7 +583,7 @@  discard block
 block discarded – undo
583 583
                 Craft::t(
584 584
                     'retour',
585 585
                     'Error validating redirect {id}: {errors}',
586
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
586
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
587 587
                 ),
588 588
                 __METHOD__
589 589
             );
@@ -593,32 +593,32 @@  discard block
 block discarded – undo
593 593
         // Get the validated model attributes and save them to the db
594 594
         $redirectConfig = $redirect->getAttributes();
595 595
         // 0 for a siteId needs to be converted to null
596
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
597
-            $redirectConfig['siteId'] = null;
596
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
597
+            $redirectConfig[ 'siteId' ] = null;
598 598
         }
599 599
         // Throw an event to before saving the redirect
600 600
         $db = Craft::$app->getDb();
601 601
         // See if a redirect exists with this source URL already
602
-        if ((int)$redirectConfig['id'] === 0) {
602
+        if ((int) $redirectConfig[ 'id' ] === 0) {
603 603
             // Query the db table
604 604
             $redirect = (new Query())
605
-                ->from(['{{%retour_static_redirects}}'])
606
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
607
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
605
+                ->from([ '{{%retour_static_redirects}}' ])
606
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
607
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
608 608
                 ->one();
609 609
             // If it exists, update it rather than having duplicates
610 610
             if (!empty($redirect)) {
611
-                $redirectConfig['id'] = $redirect['id'];
611
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
612 612
             }
613 613
         }
614 614
         // Trigger a 'beforeSaveRedirect' event
615
-        $isNew = (int)$redirectConfig['id'] === 0;
615
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
616 616
         $event = new RedirectEvent([
617 617
             'isNew' => $isNew,
618
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
619
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
620
-            'matchType' => $redirectConfig['redirectSrcMatch'],
621
-            'redirectType' => $redirectConfig['redirectHttpCode'],
618
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
619
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
620
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
621
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
622 622
         ]);
623 623
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
624 624
         if (!$event->isValid) {
@@ -630,7 +630,7 @@  discard block
 block discarded – undo
630 630
                 Craft::t(
631 631
                     'retour',
632 632
                     'Updating existing redirect: {redirect}',
633
-                    ['redirect' => print_r($redirectConfig, true)]
633
+                    [ 'redirect' => print_r($redirectConfig, true) ]
634 634
                 ),
635 635
                 __METHOD__
636 636
             );
@@ -640,7 +640,7 @@  discard block
 block discarded – undo
640 640
                     '{{%retour_static_redirects}}',
641 641
                     $redirectConfig,
642 642
                     [
643
-                        'id' => $redirectConfig['id'],
643
+                        'id' => $redirectConfig[ 'id' ],
644 644
                     ]
645 645
                 )->execute();
646 646
             } catch (Exception $e) {
@@ -651,11 +651,11 @@  discard block
 block discarded – undo
651 651
                 Craft::t(
652 652
                     'retour',
653 653
                     'Creating new redirect: {redirect}',
654
-                    ['redirect' => print_r($redirectConfig, true)]
654
+                    [ 'redirect' => print_r($redirectConfig, true) ]
655 655
                 ),
656 656
                 __METHOD__
657 657
             );
658
-            unset($redirectConfig['id']);
658
+            unset($redirectConfig[ 'id' ]);
659 659
             // Create a new record
660 660
             try {
661 661
                 $db->createCommand()->insert(
@@ -668,15 +668,15 @@  discard block
 block discarded – undo
668 668
         }
669 669
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
670 670
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
671
-            $redirectConfig['redirectDestUrl'],
672
-            $redirectConfig['siteId']
671
+            $redirectConfig[ 'redirectDestUrl' ],
672
+            $redirectConfig[ 'siteId' ]
673 673
         );
674 674
         if ($testRedirectConfig !== null) {
675 675
             Craft::debug(
676 676
                 Craft::t(
677 677
                     'retour',
678 678
                     'Deleting redirect to prevent a loop: {redirect}',
679
-                    ['redirect' => print_r($testRedirectConfig, true)]
679
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
680 680
                 ),
681 681
                 __METHOD__
682 682
             );
@@ -684,7 +684,7 @@  discard block
 block discarded – undo
684 684
             try {
685 685
                 $db->createCommand()->delete(
686 686
                     '{{%retour_static_redirects}}',
687
-                    ['id' => $testRedirectConfig['id']]
687
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
688 688
                 )->execute();
689 689
             } catch (Exception $e) {
690 690
                 Craft::error($e->getMessage(), __METHOD__);
@@ -717,10 +717,10 @@  discard block
 block discarded – undo
717 717
      */
718 718
     public function excludeUri($uri): bool
719 719
     {
720
-        $uri = '/'.ltrim($uri, '/');
720
+        $uri = '/' . ltrim($uri, '/');
721 721
         if (!empty(Retour::$settings->excludePatterns)) {
722 722
             foreach (Retour::$settings->excludePatterns as $excludePattern) {
723
-                $pattern = '`'.$excludePattern['pattern'].'`i';
723
+                $pattern = '`' . $excludePattern[ 'pattern' ] . '`i';
724 724
                 if (preg_match($pattern, $uri) === 1) {
725 725
                     return true;
726 726
                 }
Please login to merge, or discard this patch.