Passed
Push — develop ( 0487b2...4ee8d3 )
by Andrew
05:12
created
src/services/Redirects.php 1 patch
Spacing   +60 added lines, -60 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,19 +174,19 @@  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 (Retour::$settings->preserveQueryString) {
179 179
                 $request = Craft::$app->getRequest();
180 180
                 if (!empty($request->getQueryStringWithoutPath())) {
181 181
                     $dest .= '?' . $request->getQueryStringWithoutPath();
182 182
                 }
183 183
             }
184
-            $status = $redirect['redirectHttpCode'];
184
+            $status = $redirect[ 'redirectHttpCode' ];
185 185
             Craft::info(
186 186
                 Craft::t(
187 187
                     'retour',
188 188
                     'Redirecting {url} to {dest} with status {status}',
189
-                    ['url' => $url, 'dest' => $dest, 'status' => $status]
189
+                    [ 'url' => $url, 'dest' => $dest, 'status' => $status ]
190 190
                 ),
191 191
                 __METHOD__
192 192
             );
@@ -266,13 +266,13 @@  discard block
 block discarded – undo
266 266
     public function getRedirectFromCache($url, int $siteId = 0)
267 267
     {
268 268
         $cache = Craft::$app->getCache();
269
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
269
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
270 270
         $redirect = $cache->get($cacheKey);
271 271
         Craft::info(
272 272
             Craft::t(
273 273
                 'retour',
274 274
                 'Cached redirect hit for {url}',
275
-                ['url' => $url]
275
+                [ 'url' => $url ]
276 276
             ),
277 277
             __METHOD__
278 278
         );
@@ -294,12 +294,12 @@  discard block
 block discarded – undo
294 294
         } catch (SiteNotFoundException $e) {
295 295
             $siteId = 1;
296 296
         }
297
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
297
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
298 298
         // Create the dependency tags
299 299
         $dependency = new TagDependency([
300 300
             'tags' => [
301 301
                 $this::GLOBAL_REDIRECTS_CACHE_TAG,
302
-                $this::GLOBAL_REDIRECTS_CACHE_TAG.$siteId,
302
+                $this::GLOBAL_REDIRECTS_CACHE_TAG . $siteId,
303 303
             ],
304 304
         ]);
305 305
         $cache->set($cacheKey, $redirect, Retour::$cacheDuration, $dependency);
@@ -307,7 +307,7 @@  discard block
 block discarded – undo
307 307
             Craft::t(
308 308
                 'retour',
309 309
                 'Cached redirect saved for {url}',
310
-                ['url' => $url]
310
+                [ 'url' => $url ]
311 311
             ),
312 312
             __METHOD__
313 313
         );
@@ -325,8 +325,8 @@  discard block
 block discarded – undo
325 325
         $result = null;
326 326
         foreach ($redirects as $redirect) {
327 327
             // Figure out what type of source matching to do
328
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
329
-            $redirectEnabled = (bool)$redirect['enabled'];
328
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
329
+            $redirectEnabled = (bool) $redirect[ 'enabled' ];
330 330
             if ($redirectEnabled === true) {
331 331
                 switch ($redirectSrcMatch) {
332 332
                     case 'pathonly':
@@ -339,11 +339,11 @@  discard block
 block discarded – undo
339 339
                         $url = $pathOnly;
340 340
                         break;
341 341
                 }
342
-                $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
342
+                $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
343 343
                 switch ($redirectMatchType) {
344 344
                     // Do a straight up match
345 345
                     case 'exactmatch':
346
-                        if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
346
+                        if (strcasecmp($redirect[ 'redirectSrcUrlParsed' ], $url) === 0) {
347 347
                             $this->incrementRedirectHitCount($redirect);
348 348
                             $this->saveRedirectToCache($url, $redirect);
349 349
 
@@ -353,14 +353,14 @@  discard block
 block discarded – undo
353 353
 
354 354
                     // Do a regex match
355 355
                     case 'regexmatch':
356
-                        $matchRegEx = '`'.$redirect['redirectSrcUrlParsed'].'`i';
356
+                        $matchRegEx = '`' . $redirect[ 'redirectSrcUrlParsed' ] . '`i';
357 357
                         if (preg_match($matchRegEx, $url) === 1) {
358 358
                             $this->incrementRedirectHitCount($redirect);
359 359
                             // If we're not associated with an EntryID, handle capture group replacement
360
-                            if ((int)$redirect['associatedElementId'] === 0) {
361
-                                $redirect['redirectDestUrl'] = preg_replace(
360
+                            if ((int) $redirect[ 'associatedElementId' ] === 0) {
361
+                                $redirect[ 'redirectDestUrl' ] = preg_replace(
362 362
                                     $matchRegEx,
363
-                                    $redirect['redirectDestUrl'],
363
+                                    $redirect[ 'redirectDestUrl' ],
364 364
                                     $url
365 365
                                 );
366 366
                             }
@@ -379,7 +379,7 @@  discard block
 block discarded – undo
379 379
                                     'redirect' => &$redirect,
380 380
                                 ],
381 381
                             ];
382
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
382
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
383 383
                             if ($result) {
384 384
                                 $this->incrementRedirectHitCount($redirect);
385 385
                                 $this->saveRedirectToCache($url, $redirect);
@@ -395,7 +395,7 @@  discard block
 block discarded – undo
395 395
             Craft::t(
396 396
                 'retour',
397 397
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
398
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
398
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
399 399
             ),
400 400
             __METHOD__
401 401
         );
@@ -419,7 +419,7 @@  discard block
 block discarded – undo
419 419
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
420 420
             /** @var Plugin $plugin */
421 421
             if (method_exists($plugin, 'retourMatch')) {
422
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
422
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
423 423
             }
424 424
         }
425 425
 
@@ -440,12 +440,12 @@  discard block
 block discarded – undo
440 440
         }
441 441
         // Query the db table
442 442
         $query = (new Query())
443
-            ->from(['{{%retour_static_redirects}}'])
443
+            ->from([ '{{%retour_static_redirects}}' ])
444 444
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
445 445
         if ($siteId) {
446 446
             $query
447
-                ->where(['siteId' => $siteId])
448
-                ->orWhere(['siteId' => null]);
447
+                ->where([ 'siteId' => $siteId ])
448
+                ->orWhere([ 'siteId' => null ]);
449 449
         }
450 450
         if ($limit) {
451 451
             $query->limit($limit);
@@ -468,8 +468,8 @@  discard block
 block discarded – undo
468 468
     {
469 469
         // Query the db table
470 470
         $redirect = (new Query())
471
-            ->from(['{{%retour_static_redirects}}'])
472
-            ->where(['id' => $id])
471
+            ->from([ '{{%retour_static_redirects}}' ])
472
+            ->where([ 'id' => $id ])
473 473
             ->one();
474 474
 
475 475
         return $redirect;
@@ -487,13 +487,13 @@  discard block
 block discarded – undo
487 487
     {
488 488
         // Query the db table
489 489
         $query = (new Query())
490
-            ->from(['{{%retour_static_redirects}}'])
491
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
490
+            ->from([ '{{%retour_static_redirects}}' ])
491
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
492 492
             ;
493 493
         if ($siteId) {
494 494
             $query
495
-                ->andWhere(['siteId' => $siteId])
496
-                ->orWhere(['siteId' => null]);
495
+                ->andWhere([ 'siteId' => $siteId ])
496
+                ->orWhere([ 'siteId' => null ]);
497 497
         }
498 498
         $redirect = $query->one();
499 499
 
@@ -535,13 +535,13 @@  discard block
 block discarded – undo
535 535
     {
536 536
         if ($redirectConfig !== null) {
537 537
             $db = Craft::$app->getDb();
538
-            $redirectConfig['hitCount']++;
539
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
538
+            $redirectConfig[ 'hitCount' ]++;
539
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
540 540
             Craft::debug(
541 541
                 Craft::t(
542 542
                     'retour',
543 543
                     'Incrementing statistics for: {redirect}',
544
-                    ['redirect' => print_r($redirectConfig, true)]
544
+                    [ 'redirect' => print_r($redirectConfig, true) ]
545 545
                 ),
546 546
                 __METHOD__
547 547
             );
@@ -550,14 +550,14 @@  discard block
 block discarded – undo
550 550
                 $rowsAffected = $db->createCommand()->update(
551 551
                     '{{%retour_static_redirects}}',
552 552
                     [
553
-                        'hitCount' => $redirectConfig['hitCount'],
554
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
553
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
554
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
555 555
                     ],
556 556
                     [
557
-                        'id' => $redirectConfig['id'],
557
+                        'id' => $redirectConfig[ 'id' ],
558 558
                     ]
559 559
                 )->execute();
560
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
560
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
561 561
             } catch (Exception $e) {
562 562
                 Craft::error($e->getMessage(), __METHOD__);
563 563
             }
@@ -576,7 +576,7 @@  discard block
 block discarded – undo
576 576
                 Craft::t(
577 577
                     'retour',
578 578
                     'Error validating redirect {id}: {errors}',
579
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
579
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
580 580
                 ),
581 581
                 __METHOD__
582 582
             );
@@ -586,32 +586,32 @@  discard block
 block discarded – undo
586 586
         // Get the validated model attributes and save them to the db
587 587
         $redirectConfig = $redirect->getAttributes();
588 588
         // 0 for a siteId needs to be converted to null
589
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
590
-            $redirectConfig['siteId'] = null;
589
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
590
+            $redirectConfig[ 'siteId' ] = null;
591 591
         }
592 592
         // Throw an event to before saving the redirect
593 593
         $db = Craft::$app->getDb();
594 594
         // See if a redirect exists with this source URL already
595
-        if ((int)$redirectConfig['id'] === 0) {
595
+        if ((int) $redirectConfig[ 'id' ] === 0) {
596 596
             // Query the db table
597 597
             $redirect = (new Query())
598
-                ->from(['{{%retour_static_redirects}}'])
599
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
600
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
598
+                ->from([ '{{%retour_static_redirects}}' ])
599
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
600
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
601 601
                 ->one();
602 602
             // If it exists, update it rather than having duplicates
603 603
             if (!empty($redirect)) {
604
-                $redirectConfig['id'] = $redirect['id'];
604
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
605 605
             }
606 606
         }
607 607
         // Trigger a 'beforeSaveRedirect' event
608
-        $isNew = (int)$redirectConfig['id'] === 0;
608
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
609 609
         $event = new RedirectEvent([
610 610
             'isNew' => $isNew,
611
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
612
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
613
-            'matchType' => $redirectConfig['redirectSrcMatch'],
614
-            'redirectType' => $redirectConfig['redirectHttpCode'],
611
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
612
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
613
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
614
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
615 615
         ]);
616 616
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
617 617
         if (!$event->isValid) {
@@ -623,7 +623,7 @@  discard block
 block discarded – undo
623 623
                 Craft::t(
624 624
                     'retour',
625 625
                     'Updating existing redirect: {redirect}',
626
-                    ['redirect' => print_r($redirectConfig, true)]
626
+                    [ 'redirect' => print_r($redirectConfig, true) ]
627 627
                 ),
628 628
                 __METHOD__
629 629
             );
@@ -633,7 +633,7 @@  discard block
 block discarded – undo
633 633
                     '{{%retour_static_redirects}}',
634 634
                     $redirectConfig,
635 635
                     [
636
-                        'id' => $redirectConfig['id'],
636
+                        'id' => $redirectConfig[ 'id' ],
637 637
                     ]
638 638
                 )->execute();
639 639
             } catch (Exception $e) {
@@ -644,11 +644,11 @@  discard block
 block discarded – undo
644 644
                 Craft::t(
645 645
                     'retour',
646 646
                     'Creating new redirect: {redirect}',
647
-                    ['redirect' => print_r($redirectConfig, true)]
647
+                    [ 'redirect' => print_r($redirectConfig, true) ]
648 648
                 ),
649 649
                 __METHOD__
650 650
             );
651
-            unset($redirectConfig['id']);
651
+            unset($redirectConfig[ 'id' ]);
652 652
             // Create a new record
653 653
             try {
654 654
                 $db->createCommand()->insert(
@@ -661,15 +661,15 @@  discard block
 block discarded – undo
661 661
         }
662 662
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
663 663
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
664
-            $redirectConfig['redirectDestUrl'],
665
-            $redirectConfig['siteId']
664
+            $redirectConfig[ 'redirectDestUrl' ],
665
+            $redirectConfig[ 'siteId' ]
666 666
         );
667 667
         if ($testRedirectConfig !== null) {
668 668
             Craft::debug(
669 669
                 Craft::t(
670 670
                     'retour',
671 671
                     'Deleting redirect to prevent a loop: {redirect}',
672
-                    ['redirect' => print_r($testRedirectConfig, true)]
672
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
673 673
                 ),
674 674
                 __METHOD__
675 675
             );
@@ -677,7 +677,7 @@  discard block
 block discarded – undo
677 677
             try {
678 678
                 $db->createCommand()->delete(
679 679
                     '{{%retour_static_redirects}}',
680
-                    ['id' => $testRedirectConfig['id']]
680
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
681 681
                 )->execute();
682 682
             } catch (Exception $e) {
683 683
                 Craft::error($e->getMessage(), __METHOD__);
@@ -710,10 +710,10 @@  discard block
 block discarded – undo
710 710
      */
711 711
     public function excludeUri($uri): bool
712 712
     {
713
-        $uri = '/'.ltrim($uri, '/');
713
+        $uri = '/' . ltrim($uri, '/');
714 714
         if (!empty(Retour::$settings->excludePatterns)) {
715 715
             foreach (Retour::$settings->excludePatterns as $excludePattern) {
716
-                $pattern = '`'.$excludePattern['pattern'].'`i';
716
+                $pattern = '`' . $excludePattern[ 'pattern' ] . '`i';
717 717
                 if (preg_match($pattern, $uri) === 1) {
718 718
                     return true;
719 719
                 }
Please login to merge, or discard this patch.