Passed
Push — develop ( d18bf1...fe9d43 )
by Andrew
08:02
created
src/services/Redirects.php 1 patch
Spacing   +58 added lines, -58 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
             );
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
         $response = Craft::$app->getResponse();
161 161
         if ($redirect !== null) {
162 162
             // Figure out what type of source matching was done
163
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
163
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
164 164
             switch ($redirectSrcMatch) {
165 165
                 case 'pathonly':
166 166
                     $url = $pathOnly;
@@ -172,19 +172,19 @@  discard block
 block discarded – undo
172 172
                     $url = $pathOnly;
173 173
                     break;
174 174
             }
175
-            $dest = $redirect['redirectDestUrl'];
175
+            $dest = $redirect[ 'redirectDestUrl' ];
176 176
             if (Retour::$settings->preserveQueryString) {
177 177
                 $request = Craft::$app->getRequest();
178 178
                 if (!empty($request->getQueryStringWithoutPath())) {
179 179
                     $dest .= '?' . $request->getQueryStringWithoutPath();
180 180
                 }
181 181
             }
182
-            $status = $redirect['redirectHttpCode'];
182
+            $status = $redirect[ 'redirectHttpCode' ];
183 183
             Craft::info(
184 184
                 Craft::t(
185 185
                     'retour',
186 186
                     'Redirecting {url} to {dest} with status {status}',
187
-                    ['url' => $url, 'dest' => $dest, 'status' => $status]
187
+                    [ 'url' => $url, 'dest' => $dest, 'status' => $status ]
188 188
                 ),
189 189
                 __METHOD__
190 190
             );
@@ -264,13 +264,13 @@  discard block
 block discarded – undo
264 264
     public function getRedirectFromCache($url, int $siteId = 0)
265 265
     {
266 266
         $cache = Craft::$app->getCache();
267
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
267
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
268 268
         $redirect = $cache->get($cacheKey);
269 269
         Craft::info(
270 270
             Craft::t(
271 271
                 'retour',
272 272
                 'Cached redirect hit for {url}',
273
-                ['url' => $url]
273
+                [ 'url' => $url ]
274 274
             ),
275 275
             __METHOD__
276 276
         );
@@ -292,12 +292,12 @@  discard block
 block discarded – undo
292 292
         } catch (SiteNotFoundException $e) {
293 293
             $siteId = 1;
294 294
         }
295
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
295
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
296 296
         // Create the dependency tags
297 297
         $dependency = new TagDependency([
298 298
             'tags' => [
299 299
                 $this::GLOBAL_REDIRECTS_CACHE_TAG,
300
-                $this::GLOBAL_REDIRECTS_CACHE_TAG.$siteId,
300
+                $this::GLOBAL_REDIRECTS_CACHE_TAG . $siteId,
301 301
             ],
302 302
         ]);
303 303
         $cache->set($cacheKey, $redirect, Retour::$cacheDuration, $dependency);
@@ -305,7 +305,7 @@  discard block
 block discarded – undo
305 305
             Craft::t(
306 306
                 'retour',
307 307
                 'Cached redirect saved for {url}',
308
-                ['url' => $url]
308
+                [ 'url' => $url ]
309 309
             ),
310 310
             __METHOD__
311 311
         );
@@ -323,8 +323,8 @@  discard block
 block discarded – undo
323 323
         $result = null;
324 324
         foreach ($redirects as $redirect) {
325 325
             // Figure out what type of source matching to do
326
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
327
-            $redirectEnabled = (bool)$redirect['enabled'];
326
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
327
+            $redirectEnabled = (bool) $redirect[ 'enabled' ];
328 328
             if ($redirectEnabled === true) {
329 329
                 switch ($redirectSrcMatch) {
330 330
                     case 'pathonly':
@@ -337,11 +337,11 @@  discard block
 block discarded – undo
337 337
                         $url = $pathOnly;
338 338
                         break;
339 339
                 }
340
-                $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
340
+                $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
341 341
                 switch ($redirectMatchType) {
342 342
                     // Do a straight up match
343 343
                     case 'exactmatch':
344
-                        if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
344
+                        if (strcasecmp($redirect[ 'redirectSrcUrlParsed' ], $url) === 0) {
345 345
                             $this->incrementRedirectHitCount($redirect);
346 346
                             $this->saveRedirectToCache($url, $redirect);
347 347
 
@@ -351,14 +351,14 @@  discard block
 block discarded – undo
351 351
 
352 352
                     // Do a regex match
353 353
                     case 'regexmatch':
354
-                        $matchRegEx = '`'.$redirect['redirectSrcUrlParsed'].'`i';
354
+                        $matchRegEx = '`' . $redirect[ 'redirectSrcUrlParsed' ] . '`i';
355 355
                         if (preg_match($matchRegEx, $url) === 1) {
356 356
                             $this->incrementRedirectHitCount($redirect);
357 357
                             // If we're not associated with an EntryID, handle capture group replacement
358
-                            if ((int)$redirect['associatedElementId'] === 0) {
359
-                                $redirect['redirectDestUrl'] = preg_replace(
358
+                            if ((int) $redirect[ 'associatedElementId' ] === 0) {
359
+                                $redirect[ 'redirectDestUrl' ] = preg_replace(
360 360
                                     $matchRegEx,
361
-                                    $redirect['redirectDestUrl'],
361
+                                    $redirect[ 'redirectDestUrl' ],
362 362
                                     $url
363 363
                                 );
364 364
                             }
@@ -377,7 +377,7 @@  discard block
 block discarded – undo
377 377
                                     'redirect' => &$redirect,
378 378
                                 ],
379 379
                             ];
380
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
380
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
381 381
                             if ($result) {
382 382
                                 $this->incrementRedirectHitCount($redirect);
383 383
                                 $this->saveRedirectToCache($url, $redirect);
@@ -393,7 +393,7 @@  discard block
 block discarded – undo
393 393
             Craft::t(
394 394
                 'retour',
395 395
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
396
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
396
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
397 397
             ),
398 398
             __METHOD__
399 399
         );
@@ -417,7 +417,7 @@  discard block
 block discarded – undo
417 417
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
418 418
             /** @var Plugin $plugin */
419 419
             if (method_exists($plugin, 'retourMatch')) {
420
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
420
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
421 421
             }
422 422
         }
423 423
 
@@ -438,12 +438,12 @@  discard block
 block discarded – undo
438 438
         }
439 439
         // Query the db table
440 440
         $query = (new Query())
441
-            ->from(['{{%retour_static_redirects}}'])
441
+            ->from([ '{{%retour_static_redirects}}' ])
442 442
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
443 443
         if ($siteId) {
444 444
             $query
445
-                ->where(['siteId' => $siteId])
446
-                ->orWhere(['siteId' => null]);
445
+                ->where([ 'siteId' => $siteId ])
446
+                ->orWhere([ 'siteId' => null ]);
447 447
         }
448 448
         if ($limit) {
449 449
             $query->limit($limit);
@@ -466,8 +466,8 @@  discard block
 block discarded – undo
466 466
     {
467 467
         // Query the db table
468 468
         $redirect = (new Query())
469
-            ->from(['{{%retour_static_redirects}}'])
470
-            ->where(['id' => $id])
469
+            ->from([ '{{%retour_static_redirects}}' ])
470
+            ->where([ 'id' => $id ])
471 471
             ->one();
472 472
 
473 473
         return $redirect;
@@ -485,13 +485,13 @@  discard block
 block discarded – undo
485 485
     {
486 486
         // Query the db table
487 487
         $query = (new Query())
488
-            ->from(['{{%retour_static_redirects}}'])
489
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
488
+            ->from([ '{{%retour_static_redirects}}' ])
489
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
490 490
             ;
491 491
         if ($siteId) {
492 492
             $query
493
-                ->andWhere(['siteId' => $siteId])
494
-                ->orWhere(['siteId' => null]);
493
+                ->andWhere([ 'siteId' => $siteId ])
494
+                ->orWhere([ 'siteId' => null ]);
495 495
         }
496 496
         $redirect = $query->one();
497 497
 
@@ -533,13 +533,13 @@  discard block
 block discarded – undo
533 533
     {
534 534
         if ($redirectConfig !== null) {
535 535
             $db = Craft::$app->getDb();
536
-            $redirectConfig['hitCount']++;
537
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
536
+            $redirectConfig[ 'hitCount' ]++;
537
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
538 538
             Craft::debug(
539 539
                 Craft::t(
540 540
                     'retour',
541 541
                     'Incrementing statistics for: {redirect}',
542
-                    ['redirect' => print_r($redirectConfig, true)]
542
+                    [ 'redirect' => print_r($redirectConfig, true) ]
543 543
                 ),
544 544
                 __METHOD__
545 545
             );
@@ -548,14 +548,14 @@  discard block
 block discarded – undo
548 548
                 $rowsAffected = $db->createCommand()->update(
549 549
                     '{{%retour_static_redirects}}',
550 550
                     [
551
-                        'hitCount' => $redirectConfig['hitCount'],
552
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
551
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
552
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
553 553
                     ],
554 554
                     [
555
-                        'id' => $redirectConfig['id'],
555
+                        'id' => $redirectConfig[ 'id' ],
556 556
                     ]
557 557
                 )->execute();
558
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
558
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
559 559
             } catch (Exception $e) {
560 560
                 Craft::error($e->getMessage(), __METHOD__);
561 561
             }
@@ -574,7 +574,7 @@  discard block
 block discarded – undo
574 574
                 Craft::t(
575 575
                     'retour',
576 576
                     'Error validating redirect {id}: {errors}',
577
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
577
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
578 578
                 ),
579 579
                 __METHOD__
580 580
             );
@@ -584,32 +584,32 @@  discard block
 block discarded – undo
584 584
         // Get the validated model attributes and save them to the db
585 585
         $redirectConfig = $redirect->getAttributes();
586 586
         // 0 for a siteId needs to be converted to null
587
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
588
-            $redirectConfig['siteId'] = null;
587
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
588
+            $redirectConfig[ 'siteId' ] = null;
589 589
         }
590 590
         // Throw an event to before saving the redirect
591 591
         $db = Craft::$app->getDb();
592 592
         // See if a redirect exists with this source URL already
593
-        if ((int)$redirectConfig['id'] === 0) {
593
+        if ((int) $redirectConfig[ 'id' ] === 0) {
594 594
             // Query the db table
595 595
             $redirect = (new Query())
596
-                ->from(['{{%retour_static_redirects}}'])
597
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
598
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
596
+                ->from([ '{{%retour_static_redirects}}' ])
597
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
598
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
599 599
                 ->one();
600 600
             // If it exists, update it rather than having duplicates
601 601
             if (!empty($redirect)) {
602
-                $redirectConfig['id'] = $redirect['id'];
602
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
603 603
             }
604 604
         }
605 605
         // Trigger a 'beforeSaveRedirect' event
606
-        $isNew = (int)$redirectConfig['id'] === 0;
606
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
607 607
         $event = new RedirectEvent([
608 608
             'isNew' => $isNew,
609
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
610
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
611
-            'matchType' => $redirectConfig['redirectSrcMatch'],
612
-            'redirectType' => $redirectConfig['redirectHttpCode'],
609
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
610
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
611
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
612
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
613 613
         ]);
614 614
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
615 615
         if (!$event->isValid) {
@@ -621,7 +621,7 @@  discard block
 block discarded – undo
621 621
                 Craft::t(
622 622
                     'retour',
623 623
                     'Updating existing redirect: {redirect}',
624
-                    ['redirect' => print_r($redirectConfig, true)]
624
+                    [ 'redirect' => print_r($redirectConfig, true) ]
625 625
                 ),
626 626
                 __METHOD__
627 627
             );
@@ -631,7 +631,7 @@  discard block
 block discarded – undo
631 631
                     '{{%retour_static_redirects}}',
632 632
                     $redirectConfig,
633 633
                     [
634
-                        'id' => $redirectConfig['id'],
634
+                        'id' => $redirectConfig[ 'id' ],
635 635
                     ]
636 636
                 )->execute();
637 637
             } catch (Exception $e) {
@@ -642,11 +642,11 @@  discard block
 block discarded – undo
642 642
                 Craft::t(
643 643
                     'retour',
644 644
                     'Creating new redirect: {redirect}',
645
-                    ['redirect' => print_r($redirectConfig, true)]
645
+                    [ 'redirect' => print_r($redirectConfig, true) ]
646 646
                 ),
647 647
                 __METHOD__
648 648
             );
649
-            unset($redirectConfig['id']);
649
+            unset($redirectConfig[ 'id' ]);
650 650
             // Create a new record
651 651
             try {
652 652
                 $db->createCommand()->insert(
@@ -659,15 +659,15 @@  discard block
 block discarded – undo
659 659
         }
660 660
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
661 661
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
662
-            $redirectConfig['redirectDestUrl'],
663
-            $redirectConfig['siteId']
662
+            $redirectConfig[ 'redirectDestUrl' ],
663
+            $redirectConfig[ 'siteId' ]
664 664
         );
665 665
         if ($testRedirectConfig !== null) {
666 666
             Craft::debug(
667 667
                 Craft::t(
668 668
                     'retour',
669 669
                     'Deleting redirect to prevent a loop: {redirect}',
670
-                    ['redirect' => print_r($testRedirectConfig, true)]
670
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
671 671
                 ),
672 672
                 __METHOD__
673 673
             );
@@ -675,7 +675,7 @@  discard block
 block discarded – undo
675 675
             try {
676 676
                 $db->createCommand()->delete(
677 677
                     '{{%retour_static_redirects}}',
678
-                    ['id' => $testRedirectConfig['id']]
678
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
679 679
                 )->execute();
680 680
             } catch (Exception $e) {
681 681
                 Craft::error($e->getMessage(), __METHOD__);
Please login to merge, or discard this patch.