Passed
Pull Request — main (#22)
by Andrey
18:33 queued 03:32
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
     * Checks whether the string is URL address.
14
     *
15
     * @param  string|null  $url
16
     *
17
     * @return bool
18
     */
19
    public function isUrl(string $url = null): bool
20
    {
21
        return filter_var($url, FILTER_VALIDATE_URL) !== false;
22
    }
23
24
    public function validateUrl(string $url = null): void
25
    {
26
        if (! $this->isUrl($url)) {
27
            throw new NotValidUrlException($url);
28
        }
29
    }
30
31
    /**
32
     * Checks whether a URL exists.
33
     *
34
     * @param  string  $url
35
     *
36
     * @return bool
37
     */
38
    public function exists(string $url): bool
39
    {
40
        $this->validateUrl($url);
41
42
        try {
43
            $headers = get_headers($url);
44
45
            $key   = array_search('HTTP/', $headers);
46
            $value = $headers[$key] ?? null;
47
48
            preg_match('[2-3]{1}\d{2}\sOK', $value, $matches);
49
50
            return count($matches) > 0;
51
        } catch (Exception $e) {
52
            return false;
53
        }
54
    }
55
56
    /**
57
     * Get the domain name from the URL.
58
     *
59
     * @param  string|null  $url
60
     * @param  string|null  $default
61
     *
62
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
63
     *
64
     * @return string
65
     */
66
    public function domain(string $url = null, string $default = null): string
67
    {
68
        if (is_null($url)) {
69
            return $default ?: $_SERVER['HTTP_HOST'] ?? 'localhost';
70
        }
71
72
        $this->validateUrl($url);
73
74
        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...
75
    }
76
77
    /**
78
     * Retrieving the current subdomain name.
79
     *
80
     * @param  string|null  $url
81
     * @param  string|null  $default
82
     *
83
     * @return string|null
84
     */
85
    public function subdomain(string $url = null, string $default = null): ?string
86
    {
87
        $host = explode('.', HttpBuilder::parse($url)->getHost());
88
89
        return count($host) > 2 ? reset($host) : $default;
90
    }
91
92
    public function host(string $url = null): ?string
93
    {
94
        if (! $this->isUrl($url)) {
95
            return null;
96
        }
97
98
        return HttpBuilder::same()
99
            ->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

99
            ->parse(/** @scrutinizer ignore-type */ $url, PHP_URL_SCHEME)
Loading history...
100
            ->parse($url, PHP_URL_HOST)
101
            ->compile();
102
    }
103
104
    public function scheme(string $url): ?string
105
    {
106
        $this->validateUrl($url);
107
108
        return HttpBuilder::parse($url)->getScheme();
109
    }
110
111
    /**
112
     * Check the existence of the file and return the default value if it is missing.
113
     *
114
     * @param  string  $url
115
     * @param  string|null  $default
116
     *
117
     * @return string|null
118
     */
119
    public function image(string $url, string $default = null): ?string
120
    {
121
        return $this->isUrl($url)
122
            ? ($this->exists($url) ? $url : $default)
123
            : (File::exists($url) ? $url : $default);
124
    }
125
}
126