RouteMaker::makeCollection()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\MissingUrlsRedirector\Helpers;
6
7
use Arcanedev\MissingUrlsRedirector\Entities\{Redirection, RedirectionCollection};
8
use Arcanedev\MissingUrlsRedirector\Events\RouteRedirected;
9
use Illuminate\Routing\{Route, RouteAction, RouteCollection};
10
11
/**
12
 * Class     RouteMaker
13
 *
14
 * @package  Arcanedev\MissingUrlsRedirector\Helpers
15
 * @author   ARCANEDEV <[email protected]>
16
 */
17
class RouteMaker
18
{
19
    /* -----------------------------------------------------------------
20
     |  Main Methods
21
     | -----------------------------------------------------------------
22
     */
23
24
    /**
25
     * Make routes collection.
26
     *
27
     * @param  \Arcanedev\MissingUrlsRedirector\Entities\RedirectionCollection  $redirections
28
     *
29
     * @return \Illuminate\Routing\RouteCollection
30
     */
31 66
    public static function makeCollection(RedirectionCollection $redirections): RouteCollection
32
    {
33 66
        $routes = new RouteCollection;
34
35 66
        foreach ($redirections as $group) {
36 60
            foreach ($group as $redirection) {
37
                /** @var  \Arcanedev\MissingUrlsRedirector\Entities\Redirection  $redirection */
38 60
                $routes->add($route = static::makeRoute($redirection));
39 60
                $redirection->setRoute($route);
40
            }
41
        }
42
43 66
        return $routes;
44
    }
45
46
    /**
47
     * Make a route.
48
     *
49
     * @param  \Arcanedev\MissingUrlsRedirector\Entities\Redirection  $redirection
50
     *
51
     * @return \Illuminate\Routing\Route
52
     */
53 60
    public static function makeRoute(Redirection $redirection): Route
54
    {
55 60
        $route = new Route(['GET', 'HEAD'], $redirection->getFrom(), []);
56
57 60
        return $route->setAction(
58 60
            static::parseAction($route, $redirection)
0 ignored issues
show
Bug introduced by
Since parseAction() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of parseAction() to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass {
      private static function getTemperature() {
        return "-182 °C";
    }
}

print YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
59
        );
60
    }
61
62
    /* -----------------------------------------------------------------
63
     |  Other Methods
64
     | -----------------------------------------------------------------
65
     */
66
67
    /**
68
     * Resolve the router parameters.
69
     *
70
     * @param  \Illuminate\Routing\Route                              $route
71
     * @param  \Arcanedev\MissingUrlsRedirector\Entities\Redirection  $redirection
72
     *
73
     * @return array
74
     */
75 60
    private static function parseAction(Route $route, Redirection $redirection): array
76
    {
77
        return RouteAction::parse($route->uri(), function () use ($redirection, $route) {
78 60
            event(new RouteRedirected($redirection));
79
80 60
            return redirect()->to(
81 60
                $redirection->getResolvedUrl(),
82 60
                $redirection->getStatus()
83
            );
84 60
        });
85
    }
86
}
87