Completed
Push — master ( dda887...1f2752 )
by George
32:55 queued 32:55
created

Asset::getPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace SiteChecker;
4
5
/**
6
 * General class for pages, CSS, JS, images and whatever that can be downloaded.
7
 * Class Asset
8
 * @package SiteChecker
9
 */
10
class Asset
11
{
12
13
    const CODE_ERROR = 500;
14
    public static $CODES_ERROR = [404, 403, 500, 503];
15
    public static $CODES_WARNING = [301];
16
17
    /**
18
     * @var null|string
19
     */
20
    public $scheme;
21
22
    /**
23
     * @var null|string
24
     */
25
    public $host;
26
27
    /**
28
     * @var int
29
     */
30
    public $port = 80;
31
32
    /**
33
     * @var null|string
34
     */
35
    public $path;
36
37
    /**
38
     * @var
39
     */
40
    public $query;
41
42
    /**
43
     * @var Asset
44
     */
45
    public $parentPage;
46
47
    /**
48
     * @var string
49
     */
50
    private $fullHtml;
51
52
    /**
53
     * @var string
54
     */
55
    private $responseCode;
56
57
    /**
58
     * @var string
59
     */
60
    private $type;
61
62
63
    /**
64
     * Asset constructor.
65
     *
66
     * @param string $url
67
     * @param Asset $parentPage
68
     * @param $fullHtml
69
     * @param $type
70
     */
71
    public function __construct(
72
        $url,
73
        $parentPage = null,
74
        $fullHtml = '',
75
        $type = 'page'
76
    ) {
77
        $urlProperties = parse_url($url);
78
79
        foreach (['scheme', 'host', 'path', 'port', 'query'] as $property) {
80
            if (isset($urlProperties[$property])) {
81
                $this->$property = $urlProperties[$property];
82
            }
83
        }
84
        $this->parentPage = $parentPage;
85
        $this->fullHtml = $fullHtml;
86
        $this->type = $type;
87
    }
88
89
    /**
90
     * Determine if the url is relative.
91
     *
92
     * @return bool
93
     */
94
    public function isRelative()
95
    {
96
        return is_null($this->host);
97
    }
98
99
    /**
100
     * Determine if the url is protocol independent.
101
     *
102
     * @return bool
103
     */
104
    public function isProtocolIndependent()
105
    {
106
        return is_null($this->scheme);
107
    }
108
109
    /**
110
     * Determine if this is a mailto-link.
111
     *
112
     * @return bool
113
     */
114
    public function isHttp()
115
    {
116
        // Empty scheme usually means http
117
        return in_array(
118
            $this->scheme,
119
            ['http', 'https', '']
120
        );
121
    }
122
123
    /**
124
     * Set the scheme.
125
     *
126
     * @param string $scheme
127
     *
128
     * @return $this
129
     */
130
    public function setScheme($scheme)
131
    {
132
        $this->scheme = $scheme;
133
134
        return $this;
135
    }
136
137
    /**
138
     * Set the host.
139
     *
140
     * @param string $host
141
     *
142
     * @return $this
143
     */
144
    public function setHost($host)
145
    {
146
        $this->host = $host;
147
148
        return $this;
149
    }
150
151
    /**
152
     * @param int $port
153
     *
154
     * @return $this
155
     *
156
     */
157
    public function setPort($port)
158
    {
159
        $this->port = $port;
160
161
        return $this;
162
    }
163
164
    /**
165
     * @return Asset
166
     */
167
    public function getParentPage()
168
    {
169
        return $this->parentPage;
170
    }
171
172
    /**
173
     * @param Asset $parentPage
174
     */
175
    public function setParentPage($parentPage)
176
    {
177
        $this->parentPage = $parentPage;
178
    }
179
180
181
    /**
182
     * @return string
183
     */
184
    public function getResponseCode()
185
    {
186
        return $this->responseCode;
187
    }
188
189
    /**
190
     * @param string $responseCode
191
     */
192
    public function setResponseCode($responseCode)
193
    {
194
        $this->responseCode = $responseCode;
195
    }
196
197
    /**
198
     * Remove the fragment.
199
     *
200
     * @return $this
201
     */
202
    public function removeFragment()
203
    {
204
        $this->path = explode('#', $this->path)[0];
205
206
        return $this;
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getFullHtml()
213
    {
214
        return $this->fullHtml;
215
    }
216
217
    /**
218
     * @param string $fullHtml
219
     */
220
    public function setFullHtml($fullHtml)
221
    {
222
        $this->fullHtml = $fullHtml;
223
    }
224
225
    /**
226
     * @return string
227
     */
228
    public function getURL()
229
    {
230
        $path = strpos($this->path, '/') === 0 ?
231
            substr($this->path, 1) : $this->path;
232
233
        $port = ($this->port === 80 ? '' : ":{$this->port}");
234
        $url = "{$this->scheme}://{$this->host}{$port}/{$path}";
235
236
        if ($this->query) {
237
            $url .= "?{$this->query}";
238
        }
239
240
        return $url;
241
    }
242
243
    /**
244
     * Convert the url to string.
245
     *
246
     * @return string
247
     */
248
    public function __toString()
249
    {
250
        return $this->getURL();
251
    }
252
253
    /**
254
     * @return bool
255
     */
256
    public function isSuccessful()
257
    {
258
        return !$this->isError();
259
    }
260
261
    /**
262
     * @return bool
263
     */
264
    public function isError()
265
    {
266
        return in_array($this->responseCode, self::$CODES_ERROR);
267
    }
268
269
    /**
270
     * @return bool
271
     */
272
    public function isWarning()
273
    {
274
        return in_array($this->responseCode, self::$CODES_WARNING);
275
    }
276
277
    /**
278
     * @return string
279
     */
280
    public function getType()
281
    {
282
        return $this->type;
283
    }
284
285
    /**
286
     * @return null|string
287
     */
288
    public function getPath()
289
    {
290
        return $this->path;
291
    }
292
293
}
294