Passed
Push — develop ( 50b387...e629fa )
by Andrew
04:13
created
src/services/Redirects.php 1 patch
Spacing   +53 added lines, -53 removed lines patch added patch discarded remove patch
@@ -92,7 +92,7 @@  discard block
 block discarded – undo
92 92
                 Craft::t(
93 93
                     'retour',
94 94
                     '404 full URL: {fullUrl}, 404 path only: {pathOnly}',
95
-                    ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
95
+                    [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
96 96
                 ),
97 97
                 __METHOD__
98 98
             );
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
         $response = Craft::$app->getResponse();
125 125
         if ($redirect !== null) {
126 126
             // Figure out what type of source matching was done
127
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
127
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
128 128
             switch ($redirectSrcMatch) {
129 129
                 case 'pathonly':
130 130
                     $url = $pathOnly;
@@ -136,17 +136,17 @@  discard block
 block discarded – undo
136 136
                     $url = $pathOnly;
137 137
                     break;
138 138
             }
139
-            $dest = $redirect['redirectDestUrl'];
139
+            $dest = $redirect[ 'redirectDestUrl' ];
140 140
             if (Retour::$settings->preserveQueryString) {
141 141
                 $request = Craft::$app->getRequest();
142 142
                 $dest .= '?' . $request->getQueryString();
143 143
             }
144
-            $status = $redirect['redirectHttpCode'];
144
+            $status = $redirect[ 'redirectHttpCode' ];
145 145
             Craft::info(
146 146
                 Craft::t(
147 147
                     'retour',
148 148
                     'Redirecting {url} to {dest} with status {status}',
149
-                    ['url' => $url, 'dest' => $dest, 'status' => $status]
149
+                    [ 'url' => $url, 'dest' => $dest, 'status' => $status ]
150 150
                 ),
151 151
                 __METHOD__
152 152
             );
@@ -226,13 +226,13 @@  discard block
 block discarded – undo
226 226
     public function getRedirectFromCache($url, int $siteId = 0)
227 227
     {
228 228
         $cache = Craft::$app->getCache();
229
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
229
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
230 230
         $redirect = $cache->get($cacheKey);
231 231
         Craft::info(
232 232
             Craft::t(
233 233
                 'retour',
234 234
                 'Cached redirect hit for {url}',
235
-                ['url' => $url]
235
+                [ 'url' => $url ]
236 236
             ),
237 237
             __METHOD__
238 238
         );
@@ -254,12 +254,12 @@  discard block
 block discarded – undo
254 254
         } catch (SiteNotFoundException $e) {
255 255
             $siteId = 1;
256 256
         }
257
-        $cacheKey = $this::CACHE_KEY.md5($url).$siteId;
257
+        $cacheKey = $this::CACHE_KEY . md5($url) . $siteId;
258 258
         // Create the dependency tags
259 259
         $dependency = new TagDependency([
260 260
             'tags' => [
261 261
                 $this::GLOBAL_REDIRECTS_CACHE_TAG,
262
-                $this::GLOBAL_REDIRECTS_CACHE_TAG.$siteId,
262
+                $this::GLOBAL_REDIRECTS_CACHE_TAG . $siteId,
263 263
             ],
264 264
         ]);
265 265
         $cache->set($cacheKey, $redirect, Retour::$cacheDuration, $dependency);
@@ -267,7 +267,7 @@  discard block
 block discarded – undo
267 267
             Craft::t(
268 268
                 'retour',
269 269
                 'Cached redirect saved for {url}',
270
-                ['url' => $url]
270
+                [ 'url' => $url ]
271 271
             ),
272 272
             __METHOD__
273 273
         );
@@ -285,8 +285,8 @@  discard block
 block discarded – undo
285 285
         $result = null;
286 286
         foreach ($redirects as $redirect) {
287 287
             // Figure out what type of source matching to do
288
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
289
-            $redirectEnabled = (bool)$redirect['enabled'];
288
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
289
+            $redirectEnabled = (bool) $redirect[ 'enabled' ];
290 290
             if ($redirectEnabled === true) {
291 291
                 switch ($redirectSrcMatch) {
292 292
                     case 'pathonly':
@@ -299,11 +299,11 @@  discard block
 block discarded – undo
299 299
                         $url = $pathOnly;
300 300
                         break;
301 301
                 }
302
-                $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
302
+                $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
303 303
                 switch ($redirectMatchType) {
304 304
                     // Do a straight up match
305 305
                     case 'exactmatch':
306
-                        if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
306
+                        if (strcasecmp($redirect[ 'redirectSrcUrlParsed' ], $url) === 0) {
307 307
                             $this->incrementRedirectHitCount($redirect);
308 308
                             $this->saveRedirectToCache($url, $redirect);
309 309
 
@@ -313,14 +313,14 @@  discard block
 block discarded – undo
313 313
 
314 314
                     // Do a regex match
315 315
                     case 'regexmatch':
316
-                        $matchRegEx = '`'.$redirect['redirectSrcUrlParsed'].'`i';
316
+                        $matchRegEx = '`' . $redirect[ 'redirectSrcUrlParsed' ] . '`i';
317 317
                         if (preg_match($matchRegEx, $url) === 1) {
318 318
                             $this->incrementRedirectHitCount($redirect);
319 319
                             // If we're not associated with an EntryID, handle capture group replacement
320
-                            if ((int)$redirect['associatedElementId'] === 0) {
321
-                                $redirect['redirectDestUrl'] = preg_replace(
320
+                            if ((int) $redirect[ 'associatedElementId' ] === 0) {
321
+                                $redirect[ 'redirectDestUrl' ] = preg_replace(
322 322
                                     $matchRegEx,
323
-                                    $redirect['redirectDestUrl'],
323
+                                    $redirect[ 'redirectDestUrl' ],
324 324
                                     $url
325 325
                                 );
326 326
                             }
@@ -339,7 +339,7 @@  discard block
 block discarded – undo
339 339
                                     'redirect' => &$redirect,
340 340
                                 ],
341 341
                             ];
342
-                            $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
342
+                            $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
343 343
                             if ($result) {
344 344
                                 $this->incrementRedirectHitCount($redirect);
345 345
                                 $this->saveRedirectToCache($url, $redirect);
@@ -355,7 +355,7 @@  discard block
 block discarded – undo
355 355
             Craft::t(
356 356
                 'retour',
357 357
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
358
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
358
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
359 359
             ),
360 360
             __METHOD__
361 361
         );
@@ -379,7 +379,7 @@  discard block
 block discarded – undo
379 379
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
380 380
             /** @var Plugin $plugin */
381 381
             if (method_exists($plugin, 'retourMatch')) {
382
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
382
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
383 383
             }
384 384
         }
385 385
 
@@ -400,12 +400,12 @@  discard block
 block discarded – undo
400 400
         }
401 401
         // Query the db table
402 402
         $query = (new Query())
403
-            ->from(['{{%retour_static_redirects}}'])
403
+            ->from([ '{{%retour_static_redirects}}' ])
404 404
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
405 405
         if ($siteId) {
406 406
             $query
407
-                ->where(['siteId' => $siteId])
408
-                ->orWhere(['siteId' => null]);
407
+                ->where([ 'siteId' => $siteId ])
408
+                ->orWhere([ 'siteId' => null ]);
409 409
         }
410 410
         if ($limit) {
411 411
             $query->limit($limit);
@@ -428,8 +428,8 @@  discard block
 block discarded – undo
428 428
     {
429 429
         // Query the db table
430 430
         $redirect = (new Query())
431
-            ->from(['{{%retour_static_redirects}}'])
432
-            ->where(['id' => $id])
431
+            ->from([ '{{%retour_static_redirects}}' ])
432
+            ->where([ 'id' => $id ])
433 433
             ->one();
434 434
 
435 435
         return $redirect;
@@ -446,9 +446,9 @@  discard block
 block discarded – undo
446 446
     {
447 447
         // Query the db table
448 448
         $redirect = (new Query())
449
-            ->from(['{{%retour_static_redirects}}'])
450
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
451
-            ->andWhere(['siteId' => $siteId])
449
+            ->from([ '{{%retour_static_redirects}}' ])
450
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
451
+            ->andWhere([ 'siteId' => $siteId ])
452 452
             ->one();
453 453
 
454 454
         return $redirect;
@@ -489,13 +489,13 @@  discard block
 block discarded – undo
489 489
     {
490 490
         if ($redirectConfig !== null) {
491 491
             $db = Craft::$app->getDb();
492
-            $redirectConfig['hitCount']++;
493
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
492
+            $redirectConfig[ 'hitCount' ]++;
493
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
494 494
             Craft::debug(
495 495
                 Craft::t(
496 496
                     'retour',
497 497
                     'Incrementing statistics for: {redirect}',
498
-                    ['redirect' => print_r($redirectConfig, true)]
498
+                    [ 'redirect' => print_r($redirectConfig, true) ]
499 499
                 ),
500 500
                 __METHOD__
501 501
             );
@@ -504,14 +504,14 @@  discard block
 block discarded – undo
504 504
                 $rowsAffected = $db->createCommand()->update(
505 505
                     '{{%retour_static_redirects}}',
506 506
                     [
507
-                        'hitCount' => $redirectConfig['hitCount'],
508
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
507
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
508
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
509 509
                     ],
510 510
                     [
511
-                        'id' => $redirectConfig['id'],
511
+                        'id' => $redirectConfig[ 'id' ],
512 512
                     ]
513 513
                 )->execute();
514
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
514
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
515 515
             } catch (Exception $e) {
516 516
                 Craft::error($e->getMessage(), __METHOD__);
517 517
             }
@@ -530,7 +530,7 @@  discard block
 block discarded – undo
530 530
                 Craft::t(
531 531
                     'retour',
532 532
                     'Error validating redirect {id}: {errors}',
533
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
533
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
534 534
                 ),
535 535
                 __METHOD__
536 536
             );
@@ -540,29 +540,29 @@  discard block
 block discarded – undo
540 540
         // Get the validated model attributes and save them to the db
541 541
         $redirectConfig = $redirect->getAttributes();
542 542
         // 0 for a siteId needs to be converted to null
543
-        if (empty($redirectConfig['siteId']) || (int)$redirectConfig['siteId'] === 0) {
544
-            $redirectConfig['siteId'] = null;
543
+        if (empty($redirectConfig[ 'siteId' ]) || (int) $redirectConfig[ 'siteId' ] === 0) {
544
+            $redirectConfig[ 'siteId' ] = null;
545 545
         }
546 546
         $db = Craft::$app->getDb();
547 547
         // See if a redirect exists with this source URL already
548
-        if ((int)$redirectConfig['id'] === 0) {
548
+        if ((int) $redirectConfig[ 'id' ] === 0) {
549 549
             // Query the db table
550 550
             $redirect = (new Query())
551
-                ->from(['{{%retour_static_redirects}}'])
552
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
553
-                ->andWhere(['siteId' => $redirectConfig['siteId']])
551
+                ->from([ '{{%retour_static_redirects}}' ])
552
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
553
+                ->andWhere([ 'siteId' => $redirectConfig[ 'siteId' ] ])
554 554
                 ->one();
555 555
             // If it exists, update it rather than having duplicates
556 556
             if (!empty($redirect)) {
557
-                $redirectConfig['id'] = $redirect['id'];
557
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
558 558
             }
559 559
         }
560
-        if ((int)$redirectConfig['id'] !== 0) {
560
+        if ((int) $redirectConfig[ 'id' ] !== 0) {
561 561
             Craft::debug(
562 562
                 Craft::t(
563 563
                     'retour',
564 564
                     'Updating existing redirect: {redirect}',
565
-                    ['redirect' => print_r($redirectConfig, true)]
565
+                    [ 'redirect' => print_r($redirectConfig, true) ]
566 566
                 ),
567 567
                 __METHOD__
568 568
             );
@@ -572,7 +572,7 @@  discard block
 block discarded – undo
572 572
                     '{{%retour_static_redirects}}',
573 573
                     $redirectConfig,
574 574
                     [
575
-                        'id' => $redirectConfig['id'],
575
+                        'id' => $redirectConfig[ 'id' ],
576 576
                     ]
577 577
                 )->execute();
578 578
             } catch (Exception $e) {
@@ -583,11 +583,11 @@  discard block
 block discarded – undo
583 583
                 Craft::t(
584 584
                     'retour',
585 585
                     'Creating new redirect: {redirect}',
586
-                    ['redirect' => print_r($redirectConfig, true)]
586
+                    [ 'redirect' => print_r($redirectConfig, true) ]
587 587
                 ),
588 588
                 __METHOD__
589 589
             );
590
-            unset($redirectConfig['id']);
590
+            unset($redirectConfig[ 'id' ]);
591 591
             // Create a new record
592 592
             try {
593 593
                 $db->createCommand()->insert(
@@ -600,15 +600,15 @@  discard block
 block discarded – undo
600 600
         }
601 601
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
602 602
         $testRedirectConfig = $this->getRedirectByRedirectSrcUrl(
603
-            $redirectConfig['redirectDestUrl'],
604
-            $redirectConfig['siteId']
603
+            $redirectConfig[ 'redirectDestUrl' ],
604
+            $redirectConfig[ 'siteId' ]
605 605
         );
606 606
         if ($testRedirectConfig !== null) {
607 607
             Craft::debug(
608 608
                 Craft::t(
609 609
                     'retour',
610 610
                     'Deleting redirect to prevent a loop: {redirect}',
611
-                    ['redirect' => print_r($testRedirectConfig, true)]
611
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
612 612
                 ),
613 613
                 __METHOD__
614 614
             );
@@ -616,7 +616,7 @@  discard block
 block discarded – undo
616 616
             try {
617 617
                 $db->createCommand()->delete(
618 618
                     '{{%retour_static_redirects}}',
619
-                    ['id' => $testRedirectConfig['id']]
619
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
620 620
                 )->execute();
621 621
             } catch (Exception $e) {
622 622
                 Craft::error($e->getMessage(), __METHOD__);
Please login to merge, or discard this patch.