Test Setup Failed
Push — master ( fc6567...89d4a6 )
by Gabriel
07:58
created

AbstractRoute::initBase()   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 3
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 13
    public function __construct($map = false, $params = [])
43
    {
44 13
        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 13
        parent::__construct($map);
53
54
55 13
        if (count($params)) {
56 5
            $this->getParser()->setParams($params);
57 5
            $this->setDefaults($params);
58
        }
59 13
        $this->init();
60
61 13
    }
62
63
64 13
    public function init()
65
    {
66 13
    }
67
68
    /**
69
     * @param $name
70
     * @param $arguments
71
     * @return mixed
72
     */
73
    public function __call($name, $arguments)
74
    {
75
        return call_user_func_array([$this->getParser(), $name], $arguments);
76
    }
77
78
    /**
79
     * @param array $params
80
     * @return string
81
     */
82
    public function assembleFull($params = [])
83
    {
84
        $base = $this->getBase($params);
85
        $base = rtrim($base, "/");
86
87
        return $base . $this->assemble($params);
88
    }
89
90
    /**
91
     * @param array $params
92
     * @return string
93
     */
94
    public function getBase($params = [])
95
    {
96
        $this->checkBase($params);
97
        if (isset($params['_subdomain']) && !empty($params['_subdomain'])) {
98
            $base = $this->replaceSubdomain($params['_subdomain'], $this->base);
99
100
            return $base;
101
        }
102
103
        return $this->base;
104
    }
105
106
    /**
107
     * @param string $base
108
     */
109
    public function setBase($base)
110
    {
111
        $this->base = $base;
112
    }
113
114
    /** @noinspection PhpUnusedParameterInspection
115
     * @param array $params
116
     */
117
    public function checkBase($params = [])
118
    {
119
        if ($this->base === null) {
120
            $this->initBase($params);
121
        }
122
    }
123
124
    /** @noinspection PhpUnusedParameterInspection
125
     * @param array $params
126
     */
127
    public function initBase($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...
128
    {
129
        $this->setBase(BASE_URL);
130
    }
131
132
    /**
133
     * @param $subdomain
134
     * @param $url
135
     * @return mixed
136
     */
137
    public function replaceSubdomain($subdomain, $url)
138
    {
139
        $host = parse_url($url, PHP_URL_HOST);
140
        $parts = explode('.', $host);
141
        if (count($parts) > 2) {
142
            array_shift($parts);
143
        }
144
145
        array_unshift($parts, $subdomain);
146
        $newHost = implode('.', $parts);
147
148
        return str_replace($host, $newHost, $url);
149
    }
150
151
    /**
152
     * @param array $params
153
     * @return string
154
     */
155 1
    public function assemble($params = [])
156
    {
157 1
        return $this->getParser()->assemble($params);
158
    }
159
160
    /**
161
     * @return array
162
     */
163 2
    public function getParams()
164
    {
165 2
        return $this->getParser()->getParams();
166
    }
167
168
    /**
169
     * @return array
170
     */
171 2
    public function getMatches()
172
    {
173 2
        return $this->getParser()->getMatches();
174
    }
175
176
    /**
177
     * @return string
178
     */
179
    public function getUri()
180
    {
181
        return $this->uri;
182
    }
183
184
    /**
185
     * @return string
186
     */
187 4
    public function getName()
188
    {
189 4
        if ($this->name == null) {
190
            $this->initName();
191
        }
192
193 4
        return $this->name;
194
    }
195
196
    /**
197
     * @param string $name
198
     */
199 11
    public function setName($name)
200
    {
201 11
        $this->name = $name;
202 11
    }
203
204
    public function initName()
205
    {
206
        $this->setName($this->getClassName());
207
    }
208
209
    /**
210
     * @return string
211
     */
212 10
    public function getClassName()
213
    {
214 10
        return get_class($this);
215
    }
216
}
217