Passed
Push — develop ( 77e1ed...d09e2c )
by Andrew
04:33
created
src/services/Redirects.php 1 patch
Spacing   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -90,7 +90,7 @@  discard block
 block discarded – undo
90 90
                 Craft::t(
91 91
                     'retour',
92 92
                     '404 full URL: {fullUrl}, 404 path only: {pathOnly}',
93
-                    ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
93
+                    [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
94 94
                 ),
95 95
                 __METHOD__
96 96
             );
@@ -122,7 +122,7 @@  discard block
 block discarded – undo
122 122
         $response = Craft::$app->getResponse();
123 123
         if ($redirect !== null) {
124 124
             // Figure out what type of source matching was done
125
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
125
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
126 126
             switch ($redirectSrcMatch) {
127 127
                 case 'pathonly':
128 128
                     $url = $pathOnly;
@@ -134,13 +134,13 @@  discard block
 block discarded – undo
134 134
                     $url = $pathOnly;
135 135
                     break;
136 136
             }
137
-            $dest = $redirect['redirectDestUrl'];
138
-            $status = $redirect['redirectHttpCode'];
137
+            $dest = $redirect[ 'redirectDestUrl' ];
138
+            $status = $redirect[ 'redirectHttpCode' ];
139 139
             Craft::info(
140 140
                 Craft::t(
141 141
                     'retour',
142 142
                     'Redirecting {url} to {dest} with status {status}',
143
-                    ['url' => $url, 'dest' => $dest, 'status' => $status]
143
+                    [ 'url' => $url, 'dest' => $dest, 'status' => $status ]
144 144
                 ),
145 145
                 __METHOD__
146 146
             );
@@ -202,13 +202,13 @@  discard block
 block discarded – undo
202 202
     public function getRedirectFromCache($url)
203 203
     {
204 204
         $cache = Craft::$app->getCache();
205
-        $cacheKey = $this::CACHE_KEY.md5($url);
205
+        $cacheKey = $this::CACHE_KEY . md5($url);
206 206
         $redirect = $cache->get($cacheKey);
207 207
         Craft::info(
208 208
             Craft::t(
209 209
                 'retour',
210 210
                 'Cached redirect hit for {url}',
211
-                ['url' => $url]
211
+                [ 'url' => $url ]
212 212
             ),
213 213
             __METHOD__
214 214
         );
@@ -222,7 +222,7 @@  discard block
 block discarded – undo
222 222
      */
223 223
     public function saveRedirectToCache($url, $redirect)
224 224
     {
225
-        $cacheKey = $this::CACHE_KEY.md5($url);
225
+        $cacheKey = $this::CACHE_KEY . md5($url);
226 226
         $cache = Craft::$app->getCache();
227 227
         // Get the current site id
228 228
         $sites = Craft::$app->getSites();
@@ -235,7 +235,7 @@  discard block
 block discarded – undo
235 235
         $dependency = new TagDependency([
236 236
             'tags' => [
237 237
                 $this::GLOBAL_REDIRECTS_CACHE_TAG,
238
-                $this::GLOBAL_REDIRECTS_CACHE_TAG.$siteId,
238
+                $this::GLOBAL_REDIRECTS_CACHE_TAG . $siteId,
239 239
             ],
240 240
         ]);
241 241
         $cache->set($cacheKey, $redirect, Retour::$cacheDuration, $dependency);
@@ -243,7 +243,7 @@  discard block
 block discarded – undo
243 243
             Craft::t(
244 244
                 'retour',
245 245
                 'Cached redirect saved for {url}',
246
-                ['url' => $url]
246
+                [ 'url' => $url ]
247 247
             ),
248 248
             __METHOD__
249 249
         );
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
         $result = null;
262 262
         foreach ($redirects as $redirect) {
263 263
             // Figure out what type of source matching to do
264
-            $redirectSrcMatch = $redirect['redirectSrcMatch'] ?? 'pathonly';
264
+            $redirectSrcMatch = $redirect[ 'redirectSrcMatch' ] ?? 'pathonly';
265 265
             switch ($redirectSrcMatch) {
266 266
                 case 'pathonly':
267 267
                     $url = $pathOnly;
@@ -273,11 +273,11 @@  discard block
 block discarded – undo
273 273
                     $url = $pathOnly;
274 274
                     break;
275 275
             }
276
-            $redirectMatchType = $redirect['redirectMatchType'] ?? 'notfound';
276
+            $redirectMatchType = $redirect[ 'redirectMatchType' ] ?? 'notfound';
277 277
             switch ($redirectMatchType) {
278 278
                 // Do a straight up match
279 279
                 case 'exactmatch':
280
-                    if (strcasecmp($redirect['redirectSrcUrlParsed'], $url) === 0) {
280
+                    if (strcasecmp($redirect[ 'redirectSrcUrlParsed' ], $url) === 0) {
281 281
                         $this->incrementRedirectHitCount($redirect);
282 282
                         $this->saveRedirectToCache($url, $redirect);
283 283
 
@@ -287,14 +287,14 @@  discard block
 block discarded – undo
287 287
 
288 288
                 // Do a regex match
289 289
                 case 'regexmatch':
290
-                    $matchRegEx = '`'.$redirect['redirectSrcUrlParsed'].'`i';
290
+                    $matchRegEx = '`' . $redirect[ 'redirectSrcUrlParsed' ] . '`i';
291 291
                     if (preg_match($matchRegEx, $url) === 1) {
292 292
                         $this->incrementRedirectHitCount($redirect);
293 293
                         // If we're not associated with an EntryID, handle capture group replacement
294
-                        if ((int)$redirect['associatedElementId'] === 0) {
295
-                            $redirect['redirectDestUrl'] = preg_replace(
294
+                        if ((int) $redirect[ 'associatedElementId' ] === 0) {
295
+                            $redirect[ 'redirectDestUrl' ] = preg_replace(
296 296
                                 $matchRegEx,
297
-                                $redirect['redirectDestUrl'],
297
+                                $redirect[ 'redirectDestUrl' ],
298 298
                                 $url
299 299
                             );
300 300
                         }
@@ -313,7 +313,7 @@  discard block
 block discarded – undo
313 313
                                 'redirect' => &$redirect,
314 314
                             ],
315 315
                         ];
316
-                        $result = \call_user_func_array([$plugin, 'retourMatch'], $args);
316
+                        $result = \call_user_func_array([ $plugin, 'retourMatch' ], $args);
317 317
                         if ($result) {
318 318
                             $this->incrementRedirectHitCount($redirect);
319 319
                             $this->saveRedirectToCache($url, $redirect);
@@ -328,7 +328,7 @@  discard block
 block discarded – undo
328 328
             Craft::t(
329 329
                 'retour',
330 330
                 'Not handled-> full URL: {fullUrl}, path only: {pathOnly}',
331
-                ['fullUrl' => $fullUrl, 'pathOnly' => $pathOnly]
331
+                [ 'fullUrl' => $fullUrl, 'pathOnly' => $pathOnly ]
332 332
             ),
333 333
             __METHOD__
334 334
         );
@@ -352,7 +352,7 @@  discard block
 block discarded – undo
352 352
         foreach (Craft::$app->getPlugins()->getAllPlugins() as $plugin) {
353 353
             /** @var Plugin $plugin */
354 354
             if (method_exists($plugin, 'retourMatch')) {
355
-                $result[$plugin->getHandle()] = $plugin->name.Craft::t('retour', ' Match');
355
+                $result[ $plugin->getHandle() ] = $plugin->name . Craft::t('retour', ' Match');
356 356
             }
357 357
         }
358 358
 
@@ -372,7 +372,7 @@  discard block
 block discarded – undo
372 372
         }
373 373
         // Query the db table
374 374
         $query = (new Query())
375
-            ->from(['{{%retour_static_redirects}}'])
375
+            ->from([ '{{%retour_static_redirects}}' ])
376 376
             ->orderBy('redirectMatchType ASC, redirectSrcMatch ASC, hitCount DESC');
377 377
         if ($limit) {
378 378
             $query->limit($limit);
@@ -395,8 +395,8 @@  discard block
 block discarded – undo
395 395
     {
396 396
         // Query the db table
397 397
         $redirect = (new Query())
398
-            ->from(['{{%retour_static_redirects}}'])
399
-            ->where(['id' => $id])
398
+            ->from([ '{{%retour_static_redirects}}' ])
399
+            ->where([ 'id' => $id ])
400 400
             ->one();
401 401
 
402 402
         return $redirect;
@@ -413,8 +413,8 @@  discard block
 block discarded – undo
413 413
     {
414 414
         // Query the db table
415 415
         $redirect = (new Query())
416
-            ->from(['{{%retour_static_redirects}}'])
417
-            ->where(['redirectSrcUrl' => $redirectSrcUrl])
416
+            ->from([ '{{%retour_static_redirects}}' ])
417
+            ->where([ 'redirectSrcUrl' => $redirectSrcUrl ])
418 418
             ->one();
419 419
 
420 420
         return $redirect;
@@ -455,13 +455,13 @@  discard block
 block discarded – undo
455 455
     {
456 456
         if ($redirectConfig !== null) {
457 457
             $db = Craft::$app->getDb();
458
-            $redirectConfig['hitCount']++;
459
-            $redirectConfig['hitLastTime'] = Db::prepareDateForDb(new \DateTime());
458
+            $redirectConfig[ 'hitCount' ]++;
459
+            $redirectConfig[ 'hitLastTime' ] = Db::prepareDateForDb(new \DateTime());
460 460
             Craft::debug(
461 461
                 Craft::t(
462 462
                     'retour',
463 463
                     'Incrementing statistics for: {redirect}',
464
-                    ['redirect' => print_r($redirectConfig, true)]
464
+                    [ 'redirect' => print_r($redirectConfig, true) ]
465 465
                 ),
466 466
                 __METHOD__
467 467
             );
@@ -470,14 +470,14 @@  discard block
 block discarded – undo
470 470
                 $rowsAffected = $db->createCommand()->update(
471 471
                     '{{%retour_static_redirects}}',
472 472
                     [
473
-                        'hitCount' => $redirectConfig['hitCount'],
474
-                        'hitLastTime' => $redirectConfig['hitLastTime'],
473
+                        'hitCount' => $redirectConfig[ 'hitCount' ],
474
+                        'hitLastTime' => $redirectConfig[ 'hitLastTime' ],
475 475
                     ],
476 476
                     [
477
-                        'id' => $redirectConfig['id'],
477
+                        'id' => $redirectConfig[ 'id' ],
478 478
                     ]
479 479
                 )->execute();
480
-                Craft::debug('Rows affected: '.$rowsAffected, __METHOD__);
480
+                Craft::debug('Rows affected: ' . $rowsAffected, __METHOD__);
481 481
             } catch (Exception $e) {
482 482
                 Craft::error($e->getMessage(), __METHOD__);
483 483
             }
@@ -496,7 +496,7 @@  discard block
 block discarded – undo
496 496
                 Craft::t(
497 497
                     'retour',
498 498
                     'Error validating redirect {id}: {errors}',
499
-                    ['id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true)]
499
+                    [ 'id' => $redirect->id, 'errors' => print_r($redirect->getErrors(), true) ]
500 500
                 ),
501 501
                 __METHOD__
502 502
             );
@@ -507,23 +507,23 @@  discard block
 block discarded – undo
507 507
         $redirectConfig = $redirect->getAttributes();
508 508
         $db = Craft::$app->getDb();
509 509
         // See if a redirect exists with this source URL already
510
-        if ((int)$redirectConfig['id'] === 0) {
510
+        if ((int) $redirectConfig[ 'id' ] === 0) {
511 511
             // Query the db table
512 512
             $redirect = (new Query())
513
-                ->from(['{{%retour_static_redirects}}'])
514
-                ->where(['redirectSrcUrlParsed' => $redirectConfig['redirectSrcUrlParsed']])
513
+                ->from([ '{{%retour_static_redirects}}' ])
514
+                ->where([ 'redirectSrcUrlParsed' => $redirectConfig[ 'redirectSrcUrlParsed' ] ])
515 515
                 ->one();
516 516
             // If it exists, update it rather than having duplicates
517 517
             if (!empty($redirect)) {
518
-                $redirectConfig['id'] = $redirect['id'];
518
+                $redirectConfig[ 'id' ] = $redirect[ 'id' ];
519 519
             }
520 520
         }
521
-        if ((int)$redirectConfig['id'] !== 0) {
521
+        if ((int) $redirectConfig[ 'id' ] !== 0) {
522 522
             Craft::debug(
523 523
                 Craft::t(
524 524
                     'retour',
525 525
                     'Updating existing redirect: {redirect}',
526
-                    ['redirect' => print_r($redirectConfig, true)]
526
+                    [ 'redirect' => print_r($redirectConfig, true) ]
527 527
                 ),
528 528
                 __METHOD__
529 529
             );
@@ -533,7 +533,7 @@  discard block
 block discarded – undo
533 533
                     '{{%retour_static_redirects}}',
534 534
                     $redirectConfig,
535 535
                     [
536
-                        'id' => $redirectConfig['id'],
536
+                        'id' => $redirectConfig[ 'id' ],
537 537
                     ]
538 538
                 )->execute();
539 539
             } catch (Exception $e) {
@@ -544,11 +544,11 @@  discard block
 block discarded – undo
544 544
                 Craft::t(
545 545
                     'retour',
546 546
                     'Creating new redirect: {redirect}',
547
-                    ['redirect' => print_r($redirectConfig, true)]
547
+                    [ 'redirect' => print_r($redirectConfig, true) ]
548 548
                 ),
549 549
                 __METHOD__
550 550
             );
551
-            unset($redirectConfig['id']);
551
+            unset($redirectConfig[ 'id' ]);
552 552
             // Create a new record
553 553
             try {
554 554
                 $db->createCommand()->insert(
@@ -560,13 +560,13 @@  discard block
 block discarded – undo
560 560
             }
561 561
         }
562 562
         // To prevent redirect loops, see if any static redirects have our redirectDestUrl as their redirectSrcUrl
563
-        $testRedirectConfig = $this->getRedirectByRedirectSrcUrl($redirectConfig['redirectDestUrl']);
563
+        $testRedirectConfig = $this->getRedirectByRedirectSrcUrl($redirectConfig[ 'redirectDestUrl' ]);
564 564
         if ($testRedirectConfig !== null) {
565 565
             Craft::debug(
566 566
                 Craft::t(
567 567
                     'retour',
568 568
                     'Deleting redirect to prevent a loop: {redirect}',
569
-                    ['redirect' => print_r($testRedirectConfig, true)]
569
+                    [ 'redirect' => print_r($testRedirectConfig, true) ]
570 570
                 ),
571 571
                 __METHOD__
572 572
             );
@@ -574,7 +574,7 @@  discard block
 block discarded – undo
574 574
             try {
575 575
                 $db->createCommand()->delete(
576 576
                     '{{%retour_static_redirects}}',
577
-                    ['id' => $testRedirectConfig['id']]
577
+                    [ 'id' => $testRedirectConfig[ 'id' ] ]
578 578
                 )->execute();
579 579
             } catch (Exception $e) {
580 580
                 Craft::error($e->getMessage(), __METHOD__);
Please login to merge, or discard this patch.