Passed
Pull Request — main (#22)
by Andrey
26:45 queued 11:50
created

Http::image()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 3
nc 8
nop 2
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
        } catch (Exception $e) {
49
            return false;
50
        }
51
    }
52
53
    /**
54
     * Get the domain name from the URL.
55
     *
56
     * @param  string|null  $url
57
     * @param  string|null  $default
58
     *
59
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
60
     *
61
     * @return string
62
     */
63
    public function domain(string $url = null, string $default = null): string
64
    {
65
        if (is_null($url)) {
66
            return $default ?: $_SERVER['HTTP_HOST'] ?? 'localhost';
67
        }
68
69
        $this->validateUrl($url);
70
71
        return 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

71
        return HttpBuilder::/** @scrutinizer ignore-call */ parse($url)->getHost();
Loading history...
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...
72
    }
73
74
    /**
75
     * Retrieving the current subdomain name.
76
     *
77
     * @param  string|null  $url
78
     * @param  string|null  $default
79
     *
80
     * @return string|null
81
     */
82
    public function subdomain(string $url = null, string $default = null): ?string
83
    {
84
        $host = explode('.', HttpBuilder::parse($url)->getHost());
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

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

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

96
            ->parse(/** @scrutinizer ignore-type */ $url, PHP_URL_SCHEME)
Loading history...
97
            ->parse($url, PHP_URL_HOST)
98
            ->compile();
99
    }
100
101
    public function scheme(string $url): ?string
102
    {
103
        $this->validateUrl($url);
104
105
        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

105
        return HttpBuilder::/** @scrutinizer ignore-call */ parse($url)->getScheme();
Loading history...
106
    }
107
108
    /**
109
     * Check the existence of the file and return the default value if it is missing.
110
     *
111
     * @param  string  $url
112
     * @param  string|null  $default
113
     *
114
     * @return string|null
115
     */
116
    public function image(string $url, string $default = null): ?string
117
    {
118
        return $this->isUrl($url)
119
            ? ($this->exists($url) ? $url : $default)
120
            : (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

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