Passed
Push — develop ( 566845...dde1ac )
by Andrew
07:25
created
src/services/Redirects.php 1 patch
Spacing   +57 added lines, -57 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->getQueryString())) {
179 179
                     $dest .= '?' . $request->getQueryString();
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;
@@ -484,9 +484,9 @@  discard block
 block discarded – undo
484 484
     {
485 485
         // Query the db table
486 486
         $redirect = (new Query())
487
-            ->from(['{{%retour_static_redirects}}'])
488
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
489
-            ->andWhere(['siteId' => $siteId])
487
+            ->from([ '{{%retour_static_redirects}}' ])
488
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
489
+            ->andWhere([ 'siteId' => $siteId ])
490 490
             ->one();
491 491
 
492 492
         return $redirect;
@@ -527,13 +527,13 @@  discard block
 block discarded – undo
527 527
     {
528 528
         if ($redirectConfig !== null) {
529 529
             $db = Craft::$app->getDb();
530
-            $redirectConfig['hitCount']++;
531
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
530
+            $redirectConfig[ 'hitCount' ]++;
531
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
532 532
             Craft::debug(
533 533
                 Craft::t(
534 534
                     'retour',
535 535
                     'Incrementing statistics for: {redirect}',
536
-                    ['redirect' => print_r($redirectConfig, true)]
536
+                    [ 'redirect' => print_r($redirectConfig, true) ]
537 537
                 ),
538 538
                 __METHOD__
539 539
             );
@@ -542,14 +542,14 @@  discard block
 block discarded – undo
542 542
                 $rowsAffected = $db->createCommand()->update(
543 543
                     '{{%retour_static_redirects}}',
544 544
                     [
545
-                        'hitCount' => $redirectConfig['hitCount'],
546
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
545
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
546
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
547 547
                     ],
548 548
                     [
549
-                        'id' => $redirectConfig['id'],
549
+                        'id' => $redirectConfig[ 'id' ],
550 550
                     ]
551 551
                 )->execute();
552
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
552
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
553 553
             } catch (Exception $e) {
554 554
                 Craft::error($e->getMessage(), __METHOD__);
555 555
             }
@@ -568,7 +568,7 @@  discard block
 block discarded – undo
568 568
                 Craft::t(
569 569
                     'retour',
570 570
                     'Error validating redirect {id}: {errors}',
571
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
571
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
572 572
                 ),
573 573
                 __METHOD__
574 574
             );
@@ -578,32 +578,32 @@  discard block
 block discarded – undo
578 578
         // Get the validated model attributes and save them to the db
579 579
         $redirectConfig = $redirect->getAttributes();
580 580
         // 0 for a siteId needs to be converted to null
581
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
582
-            $redirectConfig['siteId'] = null;
581
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
582
+            $redirectConfig[ 'siteId' ] = null;
583 583
         }
584 584
         // Throw an event to before saving the redirect
585 585
         $db = Craft::$app->getDb();
586 586
         // See if a redirect exists with this source URL already
587
-        if ((int)$redirectConfig['id'] === 0) {
587
+        if ((int) $redirectConfig[ 'id' ] === 0) {
588 588
             // Query the db table
589 589
             $redirect = (new Query())
590
-                ->from(['{{%retour_static_redirects}}'])
591
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
592
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
590
+                ->from([ '{{%retour_static_redirects}}' ])
591
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
592
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
593 593
                 ->one();
594 594
             // If it exists, update it rather than having duplicates
595 595
             if (!empty($redirect)) {
596
-                $redirectConfig['id'] = $redirect['id'];
596
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
597 597
             }
598 598
         }
599 599
         // Trigger a 'beforeSaveRedirect' event
600
-        $isNew = (int)$redirectConfig['id'] === 0;
600
+        $isNew = (int) $redirectConfig[ 'id' ] === 0;
601 601
         $event = new RedirectEvent([
602 602
             'isNew' => $isNew,
603
-            'legacyUrl' => $redirectConfig['redirectSrcUrlParsed'],
604
-            'destinationUrl' => $redirectConfig['redirectDestUrl'],
605
-            'matchType' => $redirectConfig['redirectSrcMatch'],
606
-            'redirectType' => $redirectConfig['redirectHttpCode'],
603
+            'legacyUrl' => $redirectConfig[ 'redirectSrcUrlParsed' ],
604
+            'destinationUrl' => $redirectConfig[ 'redirectDestUrl' ],
605
+            'matchType' => $redirectConfig[ 'redirectSrcMatch' ],
606
+            'redirectType' => $redirectConfig[ 'redirectHttpCode' ],
607 607
         ]);
608 608
         $this->trigger(self::EVENT_BEFORE_SAVE_REDIRECT, $event);
609 609
         if (!$event->isValid) {
@@ -615,7 +615,7 @@  discard block
 block discarded – undo
615 615
                 Craft::t(
616 616
                     'retour',
617 617
                     'Updating existing redirect: {redirect}',
618
-                    ['redirect' => print_r($redirectConfig, true)]
618
+                    [ 'redirect' => print_r($redirectConfig, true) ]
619 619
                 ),
620 620
                 __METHOD__
621 621
             );
@@ -625,7 +625,7 @@  discard block
 block discarded – undo
625 625
                     '{{%retour_static_redirects}}',
626 626
                     $redirectConfig,
627 627
                     [
628
-                        'id' => $redirectConfig['id'],
628
+                        'id' => $redirectConfig[ 'id' ],
629 629
                     ]
630 630
                 )->execute();
631 631
             } catch (Exception $e) {
@@ -636,11 +636,11 @@  discard block
 block discarded – undo
636 636
                 Craft::t(
637 637
                     'retour',
638 638
                     'Creating new redirect: {redirect}',
639
-                    ['redirect' => print_r($redirectConfig, true)]
639
+                    [ 'redirect' => print_r($redirectConfig, true) ]
640 640
                 ),
641 641
                 __METHOD__
642 642
             );
643
-            unset($redirectConfig['id']);
643
+            unset($redirectConfig[ 'id' ]);
644 644
             // Create a new record
645 645
             try {
646 646
                 $db->createCommand()->insert(
@@ -653,15 +653,15 @@  discard block
 block discarded – undo
653 653
         }
654 654
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
655 655
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
656
-            $redirectConfig['redirectDestUrl'],
657
-            $redirectConfig['siteId']
656
+            $redirectConfig[ 'redirectDestUrl' ],
657
+            $redirectConfig[ 'siteId' ]
658 658
         );
659 659
         if ($testRedirectConfig !== null) {
660 660
             Craft::debug(
661 661
                 Craft::t(
662 662
                     'retour',
663 663
                     'Deleting redirect to prevent a loop: {redirect}',
664
-                    ['redirect' => print_r($testRedirectConfig, true)]
664
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
665 665
                 ),
666 666
                 __METHOD__
667 667
             );
@@ -669,7 +669,7 @@  discard block
 block discarded – undo
669 669
             try {
670 670
                 $db->createCommand()->delete(
671 671
                     '{{%retour_static_redirects}}',
672
-                    ['id' => $testRedirectConfig['id']]
672
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
673 673
                 )->execute();
674 674
             } catch (Exception $e) {
675 675
                 Craft::error($e->getMessage(), __METHOD__);
Please login to merge, or discard this patch.