Passed
Pull Request — main (#22)
by Andrey
28:59 queued 16:23
created

Http::exists()   A

Complexity

Conditions 2
Paths 6

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 3
b 0
f 0
nc 6
nop 1
dl 0
loc 15
rs 9.9666
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;
8
use Helldar\Support\Facades\Helpers\HttpBuilder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\Support\Helpers\HttpBuilder. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
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
    public function isUrl(?string $url): bool
20
    {
21
        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
    public function validateUrl(?string $url): void
32
    {
33
        if (! $this->isUrl($url)) {
34
            throw new NotValidUrlException($url);
35
        }
36
    }
37
38
    /**
39
     * Check if the specified URL exists.
40
     *
41
     * @param  string|null  $url
42
     *
43
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
44
     *
45
     * @return bool
46
     */
47
    public function exists(?string $url): bool
48
    {
49
        $this->validateUrl($url);
50
51
        try {
52
            $headers = get_headers($url);
53
54
            $key   = array_search('HTTP/', $headers);
55
            $value = $headers[$key] ?? null;
56
57
            preg_match('/HTTP\/\d{1}\.?\d?\s[2-3]\d{2}/i', $value, $matches);
58
59
            return count($matches) > 0;
60
        } catch (Exception $e) {
61
            return false;
62
        }
63
    }
64
65
    /**
66
     * Get the domain name from the URL.
67
     *
68
     * @param  string|null  $url
69
     *
70
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
71
     *
72
     * @return string
73
     */
74
    public function domain(?string $url): string
75
    {
76
        $this->validateUrl($url);
77
78
        return HttpBuilder::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...
79
    }
80
81
    /**
82
     * Get the subdomain name from the URL.
83
     *
84
     * @param  string|null  $url
85
     *
86
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
87
     *
88
     * @return string|null
89
     */
90
    public function subdomain(?string $url): ?string
91
    {
92
        $this->validateUrl($url);
93
94
        $host = explode('.', HttpBuilder::parse($url)->getHost());
95
96
        return count($host) > 2 ? reset($host) : null;
97
    }
98
99
    /**
100
     * Get the scheme and host from the URL.
101
     *
102
     * @param  string|null  $url
103
     *
104
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
105
     *
106
     * @return string
107
     */
108
    public function host(?string $url): string
109
    {
110
        $this->validateUrl($url);
111
112
        return HttpBuilder::same()
113
            ->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

113
            ->parse(/** @scrutinizer ignore-type */ $url, PHP_URL_SCHEME)
Loading history...
114
            ->parse($url, PHP_URL_HOST)
115
            ->compile();
116
    }
117
118
    /**
119
     * Get the scheme name from the URL.
120
     *
121
     * @param  string|null  $url
122
     *
123
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
124
     *
125
     * @return string
126
     */
127
    public function scheme(?string $url): string
128
    {
129
        $this->validateUrl($url);
130
131
        return HttpBuilder::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...
132
    }
133
134
    /**
135
     * Check the existence of the file and return the default value if it is missing.
136
     *
137
     * @param  string  $url
138
     * @param  string|null  $default
139
     *
140
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
141
     *
142
     * @return string|null
143
     */
144
    public function image(string $url, string $default = null): ?string
145
    {
146
        return $this->isUrl($url)
147
            ? ($this->exists($url) ? $url : $default)
148
            : (File::exists($url) ? $url : $default);
149
    }
150
}
151