Passed
Push — main ( 35cea8...263c2f )
by Andrey
21:51 queued 20:06
created

Url::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Helldar\Support\Helpers\Http;
4
5
use Helldar\Support\Exceptions\NotValidUrlException;
6
use Helldar\Support\Facades\Http\Builder as UrlBuilder;
7
use Throwable;
8
9
class Url
10
{
11
    /**
12
     * Parsing URL into components.
13
     *
14
     * @param  \Psr\Http\Message\UriInterface|string|null  $url
15
     *
16
     * @return \Helldar\Support\Helpers\Http\Builder
17
     */
18 6
    public function parse($url): Builder
19
    {
20 6
        return UrlBuilder::parse($url);
21
    }
22
23
    /**
24
     * Check if the string is a valid URL.
25
     *
26
     * @param  \Psr\Http\Message\UriInterface|string|null  $url
27
     *
28
     * @return bool
29
     */
30 160
    public function is($url): bool
31
    {
32 160
        return filter_var((string) $url, FILTER_VALIDATE_URL) !== false;
33
    }
34
35
    /**
36
     * Validate if the value is a valid URL or throw an error.
37
     *
38
     * @param  \Psr\Http\Message\UriInterface|string|null  $url
39
     *
40
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
41
     */
42 160
    public function validate($url): void
43
    {
44 160
        if (! $this->is($url)) {
45 12
            throw new NotValidUrlException((string) $url);
46
        }
47 148
    }
48
49
    /**
50
     * Returns the URL after validation, or throws an error.
51
     *
52
     * @param  \Psr\Http\Message\UriInterface|string|null  $url
53
     *
54
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
55
     *
56
     * @return string
57
     */
58 9
    public function validated($url): string
59
    {
60 9
        $this->validate($url);
61
62 9
        return (string) $url;
63
    }
64
65
    /**
66
     * Check if the specified URL exists.
67
     *
68
     * @param  \Psr\Http\Message\UriInterface|string|null  $url
69
     *
70
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
71
     *
72
     * @return bool
73
     */
74 12
    public function exists($url): bool
75
    {
76 12
        $this->validate($url);
77
78
        try {
79 9
            $headers = get_headers($url);
0 ignored issues
show
Bug introduced by
It seems like $url can also be of type null; however, parameter $url of get_headers() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            $headers = get_headers(/** @scrutinizer ignore-type */ $url);
Loading history...
80
81 9
            $key = array_search('HTTP/', $headers);
82
83 9
            $value = $headers[$key] ?? null;
84
85 9
            preg_match('/HTTP\/\d{1}\.?\d?\s[2-3]\d{2}/i', $value, $matches);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type null; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

85
            preg_match('/HTTP\/\d{1}\.?\d?\s[2-3]\d{2}/i', /** @scrutinizer ignore-type */ $value, $matches);
Loading history...
86
87 9
            return count($matches) > 0;
88 3
        } catch (Throwable $e) {
89 3
            return false;
90
        }
91
    }
92
93
    /**
94
     * Check the existence of the URL and return the default value if it is missing.
95
     *
96
     * @param  \Psr\Http\Message\UriInterface|string  $url
97
     * @param  \Psr\Http\Message\UriInterface|string  $default
98
     *
99
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
100
     *
101
     * @return string|null
102
     */
103 3
    public function default($url, $default): string
104
    {
105 3
        $value = $this->exists($url) ? $url : $default;
106
107 3
        return $this->validated($value);
108
    }
109
}
110