Test Setup Failed
Push — master ( ecdc34...0769f8 )
by Gabriel
02:46 queued 11s
created

AbstractRoute::initBase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Nip\Router\Route;
4
5
use Nip\Router\Legacy\Route\AbstractRouteTrait;
6
use Nip\Router\Parsers\Dynamic;
7
use Nip\Router\Route\Traits\HasMatchTrait;
8
use Nip\Router\Route\Traits\HasParserTrait;
9
use Nip\Router\Route\Traits\HasRequestTrait;
10
use Nip\Router\Route\Traits\HasTypeTrait;
11
use Nip\Router\Utility\MapTransform;
12
use Nip\Utility\Traits\NameWorksTrait;
13
14
/**
15
 * Class AbstractRoute
16
 * @package Nip\Router\Route
17
 */
18
abstract class AbstractRoute extends \Symfony\Component\Routing\Route
19
{
20
    use AbstractRouteTrait;
21
22
    use NameWorksTrait;
23
    use HasParserTrait;
24
    use HasTypeTrait;
25
    use HasRequestTrait;
26
    use HasMatchTrait;
27
28
    /**
29
     * @var string
30
     */
31
    protected $name = null;
32
33
    protected $base = null;
34
35
    /**
36
     * @var string
37
     */
38
    protected $uri;
39
40
    /**
41
     * AbstractRoute constructor.
42
     * @param bool $map
43
     * @param array $defaults
44
     */
45 33
    public function __construct($map = false, $defaults = [])
46
    {
47 33
        if ($map) {
48 26
            $parser = $this->getParser();
49 26
            $parser->setMap($map);
50 26
            if ($parser instanceof Dynamic) {
51 18
                $map = MapTransform::run($map);
52
            }
53
        }
54
55 33
        parent::__construct($map, $defaults);
56 33
        $this->init();
57 33
    }
58
59 33
    public function init()
60
    {
61 33
    }
62
63
    /**
64
     * @param $name
65
     * @param $arguments
66
     * @return mixed
67
     */
68
    public function __call($name, $arguments)
69
    {
70
        return call_user_func_array([$this->getParser(), $name], $arguments);
71
    }
72
73
    /**
74
     * @param array $params
75
     * @return string
76
     */
77
    public function assembleFull($params = [])
78
    {
79
        $base = $this->getBase($params);
80
        $base = rtrim($base, "/");
81
82
        return $base . $this->assemble($params);
83
    }
84
85
    /**
86
     * @param array $params
87
     * @return string
88
     */
89
    public function getBase($params = [])
90
    {
91
        $this->checkBase($params);
92
        if (isset($params['_subdomain']) && !empty($params['_subdomain'])) {
93
            $base = $this->replaceSubdomain($params['_subdomain'], $this->base);
94
95
            return $base;
96
        }
97
98
        return $this->base;
99
    }
100
101
    /**
102
     * @param string $base
103
     */
104
    public function setBase($base)
105
    {
106
        $this->base = $base;
107
    }
108
109
    /** @noinspection PhpUnusedParameterInspection
110
     * @param array $params
111
     */
112
    public function checkBase($params = [])
113
    {
114
        if ($this->base === null) {
115
            $this->initBase($params);
116
        }
117
    }
118
119
    /** @noinspection PhpUnusedParameterInspection
120
     * @param array $params
121
     */
122
    public function initBase($params = [])
123
    {
124
        $this->setBase(
125
            $this->generateBase($params)
126
        );
127
    }
128
129
    /**
130
     * @param $params
131
     * @return string
132
     */
133
    protected function generateBase($params)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
134
    {
135
        if (function_exists('request')) {
136
            return request()->getBaseUrl();
137
        }
138
        return $this->getRequest()->getBaseUrl();
139
    }
140
141
    /**
142
     * @param $subdomain
143
     * @param $url
144
     * @return mixed
145
     */
146
    public function replaceSubdomain($subdomain, $url)
147
    {
148
        $host = parse_url($url, PHP_URL_HOST);
149
        $parts = explode('.', $host);
150
        if (count($parts) > 2) {
151
            array_shift($parts);
152
        }
153
154
        array_unshift($parts, $subdomain);
155
        $newHost = implode('.', $parts);
156
157
        return str_replace($host, $newHost, $url);
158
    }
159
160
    /**
161
     * @param array $params
162
     * @return string
163
     */
164
    public function assemble($params = [])
165
    {
166
        return $this->getParser()->assemble($params);
167
    }
168
169
    /**
170
     * @return array
171
     */
172
    public function getParams()
173
    {
174
        return $this->getParser()->getParams();
175
    }
176
177
    /**
178
     * @return array
179
     */
180
    public function getMatches()
181
    {
182
        return $this->getParser()->getMatches();
183
    }
184
185
    /**
186
     * @return string
187
     */
188
    public function getUri()
189
    {
190
        return $this->uri;
191
    }
192
193
    /**
194
     * @return string
195
     */
196 6
    public function getName()
197
    {
198 6
        if ($this->name == null) {
199
            $this->initName();
200
        }
201
202 6
        return $this->name;
203
    }
204
205
    /**
206
     * @param string $name
207
     */
208 29
    public function setName($name)
209
    {
210 29
        $this->name = $name;
211 29
    }
212
213
    public function initName()
214
    {
215
        $this->setName($this->getClassName());
216
    }
217
218
    /**
219
     * @return string
220
     */
221 23
    public function getClassName()
222
    {
223 23
        return get_class($this);
224
    }
225
}
226