Test Setup Failed
Push — master ( 415317...7450ca )
by Gabriel
02:29 queued 10s
created

AbstractRoute::assemble()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Nip\Router\Route;
4
5
use Nip\Router\Parsers\Dynamic;
6
use Nip\Router\Route\Traits\HasMatchTrait;
7
use Nip\Router\Route\Traits\HasParserTrait;
8
use Nip\Router\Route\Traits\HasRequestTrait;
9
use Nip\Router\Route\Traits\HasTypeTrait;
10
use Nip\Router\Utility\MapTransform;
11
use Nip\Utility\Traits\NameWorksTrait;
12
13
/**
14
 * Class AbstractRoute
15
 * @package Nip\Router\Route
16
 */
17
abstract class AbstractRoute extends \Symfony\Component\Routing\Route
18
{
19
    use NameWorksTrait;
20
    use HasParserTrait;
21
    use HasTypeTrait;
22
    use HasRequestTrait;
23
    use HasMatchTrait;
24
25
    /**
26
     * @var string
27
     */
28
    protected $name = null;
29
30
    protected $base = null;
31
32
    /**
33
     * @var string
34
     */
35
    protected $uri;
36
37
    /**
38
     * AbstractRoute constructor.
39
     * @param bool $map
40
     * @param array $params
41
     */
42 14
    public function __construct($map = false, $params = [])
43
    {
44 14
        if ($map) {
45 8
            $parser = $this->getParser();
46 8
            $parser->setMap($map);
47 8
            if ($parser instanceof Dynamic) {
48 5
                $map = MapTransform::run($map);
49
            }
50
        }
51
52 14
        parent::__construct($map);
53
54
55 14
        if (count($params)) {
56 5
            $this->getParser()->setParams($params);
57 5
            $this->setDefaults($params);
58
        }
59 14
        $this->init();
60 14
    }
61
62
63 14
    public function init()
64
    {
65 14
    }
66
67
    /**
68
     * @param $name
69
     * @param $arguments
70
     * @return mixed
71
     */
72
    public function __call($name, $arguments)
73
    {
74
        return call_user_func_array([$this->getParser(), $name], $arguments);
75
    }
76
77
    /**
78
     * @param array $params
79
     * @return string
80
     */
81
    public function assembleFull($params = [])
82
    {
83
        $base = $this->getBase($params);
84
        $base = rtrim($base, "/");
85
86
        return $base . $this->assemble($params);
87
    }
88
89
    /**
90
     * @param array $params
91
     * @return string
92
     */
93
    public function getBase($params = [])
94
    {
95
        $this->checkBase($params);
96
        if (isset($params['_subdomain']) && !empty($params['_subdomain'])) {
97
            $base = $this->replaceSubdomain($params['_subdomain'], $this->base);
98
99
            return $base;
100
        }
101
102
        return $this->base;
103
    }
104
105
    /**
106
     * @param string $base
107
     */
108
    public function setBase($base)
109
    {
110
        $this->base = $base;
111
    }
112
113
    /** @noinspection PhpUnusedParameterInspection
114
     * @param array $params
115
     */
116
    public function checkBase($params = [])
117
    {
118
        if ($this->base === null) {
119
            $this->initBase($params);
120
        }
121
    }
122
123
    /** @noinspection PhpUnusedParameterInspection
124
     * @param array $params
125
     */
126
    public function initBase($params = [])
127
    {
128
        $this->setBase(
129
            $this->generateBase($params)
130
        );
131
    }
132
133
    /**
134
     * @param $params
135
     * @return string
136
     */
137
    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...
138
    {
139
        if (function_exists('request')) {
140
            return request()->getBaseUrl();
141
        }
142
        return $this->getRequest()->getBaseUrl();
143
    }
144
145
    /**
146
     * @param $subdomain
147
     * @param $url
148
     * @return mixed
149
     */
150
    public function replaceSubdomain($subdomain, $url)
151
    {
152
        $host = parse_url($url, PHP_URL_HOST);
153
        $parts = explode('.', $host);
154
        if (count($parts) > 2) {
155
            array_shift($parts);
156
        }
157
158
        array_unshift($parts, $subdomain);
159
        $newHost = implode('.', $parts);
160
161
        return str_replace($host, $newHost, $url);
162
    }
163
164
    /**
165
     * @param array $params
166
     * @return string
167
     */
168
    public function assemble($params = [])
169
    {
170
        return $this->getParser()->assemble($params);
171
    }
172
173
    /**
174
     * @return array
175
     */
176 2
    public function getParams()
177
    {
178 2
        return $this->getParser()->getParams();
179
    }
180
181
    /**
182
     * @return array
183
     */
184 2
    public function getMatches()
185
    {
186 2
        return $this->getParser()->getMatches();
187
    }
188
189
    /**
190
     * @return string
191
     */
192
    public function getUri()
193
    {
194
        return $this->uri;
195
    }
196
197
    /**
198
     * @return string
199
     */
200 5
    public function getName()
201
    {
202 5
        if ($this->name == null) {
203
            $this->initName();
204
        }
205
206 5
        return $this->name;
207
    }
208
209
    /**
210
     * @param string $name
211
     */
212 12
    public function setName($name)
213
    {
214 12
        $this->name = $name;
215 12
    }
216
217
    public function initName()
218
    {
219
        $this->setName($this->getClassName());
220
    }
221
222
    /**
223
     * @return string
224
     */
225 10
    public function getClassName()
226
    {
227 10
        return get_class($this);
228
    }
229
}
230