GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 937f47...67970b )
by Daniel
02:41
created

Url::getPort()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace LiquidWeb\SslCertificate;
4
5
use League\Uri\Parser as UriParser;
6
use LiquidWeb\SslCertificate\Exceptions\InvalidUrl;
7
8
class Url
9
{
10
    /** @var string */
11
    protected $inputUrl;
12
13
    /** @var array */
14
    protected $parsedUrl;
15
16
    /** @var string */
17
    protected $validatedURL;
18
19
    /** @var string */
20
    protected $ipAddress;
21
22 21
    public function __toString()
23
    {
24 21
        return $this->getInputUrl();
25 21
    }
26 4
27
    private static function verifyAndGetDNS($domain): ?string
28
    {
29 19
        $domainIp = gethostbyname($domain);
30
        if (! filter_var($domainIp, FILTER_VALIDATE_IP)) {
31
            return null;
32 22
        }
33
34 22
        return $domainIp;
35 22
    }
36 22
37
    public function __construct(string $url)
38
    {
39 22
        $this->inputUrl = $url;
40
        $parser = new UriParser();
41 18
        $this->parsedUrl = $parser($this->inputUrl);
42
43
        // Verify parsing has a host
44
        if (is_null($this->parsedUrl['host'])) {
45 18
            try {
46
                $this->parsedUrl = $parser('https://'.$this->inputUrl);
47
            } catch (\Exception $e) {
48
                throw InvalidUrl::couldNotValidate($url);
49
            }
50 22
            if (is_null($this->parsedUrl['host'])) {
51 1
                throw InvalidUrl::couldNotDetermineHost($url);
52
            }
53
        }
54 21
55 19
        if (! filter_var($this->getValidUrl(), FILTER_VALIDATE_URL)) {
56 19
            throw InvalidUrl::couldNotValidate($url);
57
        }
58 7
59
        $this->ipAddress = self::verifyAndGetDNS($this->parsedUrl['host']);
60 7
        $this->validatedURL = $url;
61
    }
62
63 6
    public function getIp(): ?string
64
    {
65 6
        if (null === $this->ipAddress) {
66
            throw InvalidUrl::couldNotResolveDns($this->inputUrl);
67
        }
68 22
        return $this->ipAddress;
69
    }
70 22
71
    public function getInputUrl(): string
72
    {
73 2
        return $this->inputUrl;
74
    }
75 2
76
    public function getHostName(): string
77
    {
78 22
        return $this->parsedUrl['host'];
79
    }
80 22
81
    public function getValidatedURL(): string
82
    {
83 11
        return $this->validatedURL;
84
    }
85 11
86
    public function getPort(): string
87
    {
88 22
        return (isset($this->parsedUrl['port'])) ? $this->parsedUrl['port'] : '443';
89
    }
90 22
91 1
    public function getTestURL(): string
92
    {
93
        return "{$this->getHostName()}:{$this->getPort()}";
94 21
    }
95
96
    public function getValidUrl(): string
97
    {
98
        if ($this->getPort() === '80') {
99
            return 'http://'.$this->getHostName().'/';
100
        }
101
102
        return 'https://'.$this->getHostName().'/';
103
    }
104
}
105