MissingPageRouter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 71
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getRedirectFor() 0 26 2
A determineRedirectUrl() 0 8 2
A determineRedirectStatusCode() 0 4 2
A resolveRouterParameters() 0 10 2
1
<?php
2
3
namespace Spatie\MissingPageRedirector;
4
5
use Exception;
6
use Illuminate\Routing\Router;
7
use Spatie\MissingPageRedirector\Events\RedirectNotFound;
8
use Spatie\MissingPageRedirector\Events\RouteWasHit;
9
use Spatie\MissingPageRedirector\Redirector\Redirector;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
13
class MissingPageRouter
14
{
15
    /** @var \Illuminate\Routing\Router */
16
    protected $router;
17
18
    /** @var \Spatie\MissingPageRedirector\Redirector\Redirector */
19
    protected $redirector;
20
21
    public function __construct(Router $router, Redirector $redirector)
22
    {
23
        $this->router = $router;
24
        $this->redirector = $redirector;
25
    }
26
27
    /**
28
     * @param \Symfony\Component\HttpFoundation\Request $request
29
     *
30
     * @return \Illuminate\Http\Response|null
31
     */
32
    public function getRedirectFor(Request $request)
33
    {
34
        $redirects = $this->redirector->getRedirectsFor($request);
35
36
        collect($redirects)->each(function ($redirects, $missingUrl) {
37
            $this->router->get($missingUrl, function () use ($redirects, $missingUrl) {
38
                $redirectUrl = $this->determineRedirectUrl($redirects);
39
                $statusCode = $this->determineRedirectStatusCode($redirects);
40
41
                event(new RouteWasHit($redirectUrl, $missingUrl, $statusCode));
42
43
                return redirect()->to(
44
                    $redirectUrl,
45
                    $statusCode
46
                );
47
            });
48
        });
49
50
        try {
51
            return $this->router->dispatch($request);
0 ignored issues
show
Compatibility introduced by
$request of type object<Symfony\Component\HttpFoundation\Request> is not a sub-type of object<Illuminate\Http\Request>. It seems like you assume a child class of the class Symfony\Component\HttpFoundation\Request to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
52
        } catch (Exception $e) {
53
            event(new RedirectNotFound($request));
54
55
            return;
56
        }
57
    }
58
59
    protected function determineRedirectUrl($redirects): string
60
    {
61
        if (is_array($redirects)) {
62
            return $this->resolveRouterParameters($redirects[0]);
63
        }
64
65
        return $this->resolveRouterParameters($redirects);
66
    }
67
68
    protected function determineRedirectStatusCode($redirects): int
69
    {
70
        return is_array($redirects) ? $redirects[1] : Response::HTTP_MOVED_PERMANENTLY;
71
    }
72
73
    protected function resolveRouterParameters(string $redirectUrl): string
74
    {
75
        foreach ($this->router->getCurrentRoute()->parameters() as $key => $value) {
76
            $redirectUrl = str_replace("{{$key}}", $value, $redirectUrl);
77
        }
78
79
        $redirectUrl = preg_replace('/\/{[\w-]+}/', '', $redirectUrl);
80
81
        return $redirectUrl;
82
    }
83
}
84