Passed
Push — main ( ed0394...6f9c40 )
by Andrey
11:51
created

Http   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 155
ccs 38
cts 38
cp 1
rs 10
c 9
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A validateUrl() 0 4 2
A isUrl() 0 3 1
A validatedUrl() 0 5 1
A exists() 0 16 2
A subdomain() 0 7 2
A host() 0 8 1
A domain() 0 5 1
A scheme() 0 5 1
A image() 0 5 4
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use Exception;
6
use Helldar\Support\Exceptions\NotValidUrlException;
7
use Helldar\Support\Facades\Helpers\Filesystem\File as FileHelper;
8
use Helldar\Support\Facades\Helpers\HttpBuilder as HttpBuilderHelper;
9
10
final class Http
11
{
12
    /**
13
     * Check if the string is a valid URL.
14
     *
15
     * @param  string|null  $url
16
     *
17
     * @return bool
18
     */
19 48
    public function isUrl(?string $url): bool
20
    {
21 48
        return filter_var($url, FILTER_VALIDATE_URL) !== false;
22
    }
23
24
    /**
25
     * Validate if the value is a valid URL or throw an error.
26
     *
27
     * @param  string|null  $url
28
     *
29
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
30
     */
31 46
    public function validateUrl(?string $url): void
32
    {
33 46
        if (! $this->isUrl($url)) {
34 30
            throw new NotValidUrlException($url);
35
        }
36 16
    }
37
38
    /**
39
     * Returns the URL after validation, or throws an error.
40
     *
41
     * @param  string|null  $url
42
     *
43
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
44
     * @return string
45
     */
46 4
    public function validatedUrl(?string $url): string
47
    {
48 4
        $this->validateUrl($url);
49
50 2
        return $url;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $url could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
51
    }
52
53
    /**
54
     * Check if the specified URL exists.
55
     *
56
     * @param  string|null  $url
57
     *
58
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
59
     *
60
     * @return bool
61
     */
62 6
    public function exists(?string $url): bool
63
    {
64 6
        $this->validateUrl($url);
65
66
        try {
67 4
            $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

67
            $headers = get_headers(/** @scrutinizer ignore-type */ $url);
Loading history...
68
69 4
            $key   = array_search('HTTP/', $headers);
70 4
            $value = $headers[$key] ?? null;
71
72 4
            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

72
            preg_match('/HTTP\/\d{1}\.?\d?\s[2-3]\d{2}/i', /** @scrutinizer ignore-type */ $value, $matches);
Loading history...
73
74 4
            return count($matches) > 0;
75
        }
76 4
        catch (Exception $e) {
77 4
            return false;
78
        }
79
    }
80
81
    /**
82
     * Get the domain name from the URL.
83
     *
84
     * @param  string|null  $url
85
     *
86
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
87
     *
88
     * @return string
89
     */
90 8
    public function domain(?string $url): string
91
    {
92 8
        $this->validateUrl($url);
93
94 2
        return HttpBuilderHelper::parse($url)->getHost();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Helldar\Support\F...:parse($url)->getHost() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
95
    }
96
97
    /**
98
     * Get the subdomain name from the URL.
99
     *
100
     * @param  string|null  $url
101
     *
102
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
103
     *
104
     * @return string|null
105
     */
106 8
    public function subdomain(?string $url): ?string
107
    {
108 8
        $this->validateUrl($url);
109
110 2
        $host = explode('.', HttpBuilderHelper::parse($url)->getHost());
0 ignored issues
show
Bug introduced by
It seems like Helldar\Support\Facades\...:parse($url)->getHost() can also be of type null; however, parameter $string of explode() 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

110
        $host = explode('.', /** @scrutinizer ignore-type */ HttpBuilderHelper::parse($url)->getHost());
Loading history...
111
112 2
        return count($host) > 2 ? reset($host) : null;
113
    }
114
115
    /**
116
     * Get the scheme and host from the URL.
117
     *
118
     * @param  string|null  $url
119
     *
120
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
121
     *
122
     * @return string
123
     */
124 8
    public function host(?string $url): string
125
    {
126 8
        $this->validateUrl($url);
127
128 2
        return HttpBuilderHelper::same()
129 2
            ->parse($url, PHP_URL_SCHEME)
0 ignored issues
show
Bug introduced by
It seems like $url can also be of type null; however, parameter $url of Helldar\Support\Helpers\HttpBuilder::parse() 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

129
            ->parse(/** @scrutinizer ignore-type */ $url, PHP_URL_SCHEME)
Loading history...
130 2
            ->parse($url, PHP_URL_HOST)
131 2
            ->compile();
132
    }
133
134
    /**
135
     * Get the scheme name from the URL.
136
     *
137
     * @param  string|null  $url
138
     *
139
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
140
     *
141
     * @return string
142
     */
143 8
    public function scheme(?string $url): string
144
    {
145 8
        $this->validateUrl($url);
146
147 2
        return HttpBuilderHelper::parse($url)->getScheme();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Helldar\Support\F...arse($url)->getScheme() could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
148
    }
149
150
    /**
151
     * Check the existence of the file and return the default value if it is missing.
152
     *
153
     * @param  string  $url
154
     * @param  string|null  $default
155
     *
156
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
157
     *
158
     * @return string|null
159
     */
160 2
    public function image(string $url, string $default = null): ?string
161
    {
162 2
        return $this->isUrl($url)
163 2
            ? ($this->exists($url) ? $url : $default)
164 2
            : (FileHelper::exists($url) ? $url : $default);
165
    }
166
}
167