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

Http   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 9
Bugs 0 Features 0
Metric Value
eloc 30
c 9
b 0
f 0
dl 0
loc 115
rs 10
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 16 2
A validateUrl() 0 4 2
A subdomain() 0 5 2
A isUrl() 0 3 1
A host() 0 10 2
A domain() 0 9 3
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;
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
        }
52
        catch (Exception $e) {
53
            return false;
54
        }
55
    }
56
57
    /**
58
     * Get the domain name from the URL.
59
     *
60
     * @param  string|null  $url
61
     * @param  string|null  $default
62
     *
63
     * @throws \Helldar\Support\Exceptions\NotValidUrlException
64
     *
65
     * @return string
66
     */
67
    public function domain(string $url = null, string $default = null): string
68
    {
69
        if (is_null($url)) {
70
            return $default ?: $_SERVER['HTTP_HOST'] ?? 'localhost';
71
        }
72
73
        $this->validateUrl($url);
74
75
        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...
76
    }
77
78
    /**
79
     * Retrieving the current subdomain name.
80
     *
81
     * @param  string|null  $url
82
     * @param  string|null  $default
83
     *
84
     * @return string|null
85
     */
86
    public function subdomain(string $url = null, string $default = null): ?string
87
    {
88
        $host = explode('.', HttpBuilder::parse($url)->getHost());
89
90
        return count($host) > 2 ? reset($host) : $default;
91
    }
92
93
    public function host(string $url = null): ?string
94
    {
95
        if (! $this->isUrl($url)) {
96
            return null;
97
        }
98
99
        return HttpBuilder::same()
100
            ->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

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