Completed
Push — master ( 2b0ce8...94c7b3 )
by ARCANEDEV
8s
created

AbstractRedirector   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 2
dl 0
loc 115
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getOption() 0 4 1
A getRedirectFor() 0 20 2
A determineRedirectUrl() 0 6 2
A determineRedirectStatusCode() 0 4 2
A resolveRouterParameters() 0 8 2
1
<?php namespace Arcanedev\LaravelSeo\Redirectors;
2
3
use Arcanedev\LaravelSeo\Contracts\Redirector;
4
use Illuminate\Http\Request;
5
use Illuminate\Support\Arr;
6
use Symfony\Component\HttpFoundation\Response;
7
8
/**
9
 * Class     AbstractRedirector
10
 *
11
 * @package  Arcanedev\LaravelSeo\Redirectors
12
 * @author   ARCANEDEV <[email protected]>
13
 */
14
abstract class AbstractRedirector implements Redirector
15
{
16
    /* -----------------------------------------------------------------
17
     |  Properties
18
     | -----------------------------------------------------------------
19
     */
20
    /** @var \Illuminate\Routing\Router */
21
    protected $router;
22
23
    /** @var  \Illuminate\Http\Request */
24
    protected $request;
25
26
    /** @var  array */
27
    protected $options = [];
28
29
    /* -----------------------------------------------------------------
30
     |  Constructor
31
     | -----------------------------------------------------------------
32
     */
33
    /**
34
     * AbstractRedirector constructor.
35
     *
36
     * @param  array  $options
37
     */
38 30
    public function __construct($router, array $options)
39
    {
40 30
        $this->router  = $router;
41 30
        $this->options = $options;
42 30
    }
43
44
    /* -----------------------------------------------------------------
45
     |  Getters & Setters
46
     | -----------------------------------------------------------------
47
     */
48
    /**
49
     * Get an option.
50
     *
51
     * @param  string      $key
52
     * @param  mixed|null  $default
53
     *
54
     * @return mixed
55
     */
56 24
    public function getOption($key, $default = null)
57
    {
58 24
        return Arr::get($this->options, $key, $default);
59
    }
60
61
    /* -----------------------------------------------------------------
62
     |  Main Methods
63
     | -----------------------------------------------------------------
64
     */
65
    /**
66
     * Get the redirect url.
67
     *
68
     * @param  \Illuminate\Http\Request  $request
69
     *
70
     * @return \Illuminate\Http\Response|null
71
     */
72 18
    public function getRedirectFor(Request $request)
73
    {
74 18
        $this->request = $request;
75
76
        collect($this->getRedirectedUrls())->each(function ($redirects, $missingUrl) {
77 18
            $this->router->get($missingUrl, function () use ($redirects) {
78 15
                return redirect()->to(
79 15
                    $this->determineRedirectUrl($redirects),
80 15
                    $this->determineRedirectStatusCode($redirects)
81 5
                );
82 18
            });
83 18
        });
84
85
        try {
86 18
            return $this->router->dispatch($request);
87
        }
88 3
        catch (\Exception $e) {
89 3
            return null;
90
        }
91
    }
92
93
    /**
94
     * @param  array|string  $redirects
95
     *
96
     * @return string
97
     */
98 15
    protected function determineRedirectUrl($redirects)
99
    {
100 15
        return $this->resolveRouterParameters(
101 15
            is_array($redirects) ? reset($redirects) : $redirects
102 5
        );
103
    }
104
105
    /**
106
     * @param  mixed  $redirects
107
     *
108
     * @return int
109
     */
110 15
    protected function determineRedirectStatusCode($redirects)
111
    {
112 15
        return is_array($redirects) ? end($redirects) : Response::HTTP_MOVED_PERMANENTLY;
113
    }
114
115
    /**
116
     * @param  string  $redirectUrl
117
     *
118
     * @return string
119
     */
120 15
    protected function resolveRouterParameters($redirectUrl)
121
    {
122 15
        foreach ($this->router->getCurrentRoute()->parameters() as $key => $value) {
123 9
            $redirectUrl = str_replace("{{$key}}", $value, $redirectUrl);
124 5
        }
125
126 15
        return $redirectUrl;
127
    }
128
}
129