Asset::removeFragment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
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 $codesError = [404, 403, 500, 503];
15
    public static $codesWarning = [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 getText() {
229
        return strip_tags($this->fullHtml);
230
    }
231
232
    /**
233
     * @return string
234
     */
235
    public function getURL()
236
    {
237
        $path = strpos($this->path, '/') === 0 ?
238
            substr($this->path, 1) : $this->path;
239
240
        $port = ($this->port === 80 ? '' : ":{$this->port}");
241
        $url = "{$this->scheme}://{$this->host}{$port}/{$path}";
242
243
        if ($this->query) {
244
            $url .= "?{$this->query}";
245
        }
246
247
        return $url;
248
    }
249
250
    /**
251
     * Convert the url to string.
252
     *
253
     * @return string
254
     */
255
    public function __toString()
256
    {
257
        return $this->getURL();
258
    }
259
260
    /**
261
     * @return bool
262
     */
263
    public function isSuccessful()
264
    {
265
        return !$this->isError();
266
    }
267
268
    /**
269
     * @return bool
270
     */
271
    public function isError()
272
    {
273
        return in_array($this->responseCode, self::$codesError);
274
    }
275
276
    /**
277
     * @return bool
278
     */
279
    public function isWarning()
280
    {
281
        return in_array($this->responseCode, self::$codesWarning);
282
    }
283
284
    /**
285
     * @return string
286
     */
287
    public function getType()
288
    {
289
        return $this->type;
290
    }
291
292
    /**
293
     * @return null|string
294
     */
295
    public function getPath()
296
    {
297
        return $this->path;
298
    }
299
300
}
301