Passed
Pull Request — main (#22)
by Andrey
12:11
created

Http::scheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Helldar\Support\Helpers;
4
5
use Exception;
6
use Helldar\Support\Exceptions\NotValidUrlException;
7
use Helldar\Support\Helpers\Filesystem\File;
8
9
final class Http
10
{
11
    /**
12
     * Checks whether the string is URL address.
13
     *
14
     * @param  string|null  $url
15
     *
16
     * @return bool
17
     */
18
    public function isUrl(string $url = null): bool
19
    {
20
        return filter_var($url, FILTER_VALIDATE_URL) !== false;
21
    }
22
23
    public function validateUrl(string $url = null): void
24
    {
25
        if (! $this->isUrl($url)) {
26
            throw new NotValidUrlException($url);
27
        }
28
    }
29
30
    /**
31
     * Checks whether a URL exists.
32
     *
33
     * @param  string  $url
34
     *
35
     * @return bool
36
     */
37
    public function exists(string $url): bool
38
    {
39
        try {
40
            $headers = get_headers($url);
41
42
            $key   = array_search('HTTP/', $headers);
43
            $value = $headers[$key] ?? null;
44
45
            preg_match('[2-3]{1}\d{2}\sOK', $value, $matches);
46
47
            return count($matches) > 0;
48
        }
49
        catch (Exception $e) {
50
            return false;
51
        }
52
    }
53
54
    /**
55
     * Get the domain name from the URL.
56
     *
57
     * @param  string|null  $url
58
     * @param  string|null  $default
59
     *
60
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
61
     *
62
     * @return string
63
     */
64
    public function domain(string $url = null, string $default = null): string
65
    {
66
        if (is_null($url)) {
67
            return $default ?: $_SERVER['HTTP_HOST'] ?? 'localhost';
68
        }
69
70
        $this->validateUrl($url);
71
72
        return HttpBuilder::parse($url)->getHost();
0 ignored issues
show
Bug Best Practice introduced by
The expression return Helldar\Support\H...: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...
Bug Best Practice introduced by
The method Helldar\Support\Helpers\HttpBuilder::parse() is not static, but was called statically. ( Ignorable by Annotation )

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

72
        return HttpBuilder::/** @scrutinizer ignore-call */ parse($url)->getHost();
Loading history...
73
    }
74
75
    /**
76
     * Retrieving the current subdomain name.
77
     *
78
     * @param  string|null  $url
79
     * @param  string|null  $default
80
     *
81
     * @return string|null
82
     */
83
    public function subdomain(string $url = null, string $default = null): ?string
84
    {
85
        $host = explode('.', HttpBuilder::parse($url)->getHost());
0 ignored issues
show
Bug Best Practice introduced by
The method Helldar\Support\Helpers\HttpBuilder::parse() is not static, but was called statically. ( Ignorable by Annotation )

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

85
        $host = explode('.', HttpBuilder::/** @scrutinizer ignore-call */ parse($url)->getHost());
Loading history...
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

85
        $host = explode('.', HttpBuilder::parse(/** @scrutinizer ignore-type */ $url)->getHost());
Loading history...
86
87
        return count($host) > 2 ? reset($host) : $default;
88
    }
89
90
    public function host(string $url = null): ?string
91
    {
92
        if (! $this->isUrl($url)) {
93
            return null;
94
        }
95
96
        return HttpBuilder::make()
97
            ->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

97
            ->parse(/** @scrutinizer ignore-type */ $url, PHP_URL_SCHEME)
Loading history...
98
            ->parse($url, PHP_URL_HOST)
99
            ->compile();
100
    }
101
102
    public function scheme(string $url): ?string
103
    {
104
        $this->validateUrl($url);
105
106
        return HttpBuilder::parse($url)->getScheme();
0 ignored issues
show
Bug Best Practice introduced by
The method Helldar\Support\Helpers\HttpBuilder::parse() is not static, but was called statically. ( Ignorable by Annotation )

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

106
        return HttpBuilder::/** @scrutinizer ignore-call */ parse($url)->getScheme();
Loading history...
107
    }
108
109
    /**
110
     * Check the existence of the file and return the default value if it is missing.
111
     *
112
     * @param  string  $url
113
     * @param  string|null  $default
114
     *
115
     * @return string|null
116
     */
117
    public function image(string $url, string $default = null): ?string
118
    {
119
        return $this->isUrl($url)
120
            ? ($this->exists($url) ? $url : $default)
121
            : (File::exists($url) ? $url : $default);
0 ignored issues
show
Bug Best Practice introduced by
The method Helldar\Support\Helpers\Filesystem\File::exists() is not static, but was called statically. ( Ignorable by Annotation )

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

121
            : (File::/** @scrutinizer ignore-call */ exists($url) ? $url : $default);
Loading history...
122
    }
123
}
124