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 ( e7108a...b466b1 )
by Freek
03:14 queued 01:19
created

SslCertificate::createFromHostName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Spatie\SslCertificate;
4
5
use Carbon\Carbon;
6
7
class SslCertificate
8
{
9
    /** @var array */
10
    protected $rawCertificateFields = [];
11
12
    public static function createForHostName(string $url, int $timeout = 30): SslCertificate
13
    {
14
        $rawCertificateFields = Downloader::downloadCertificateFromUrl($url, $timeout);
15
16
        return new static($rawCertificateFields);
17
    }
18
19
    public function __construct(array $rawCertificateFields)
20
    {
21
        $this->rawCertificateFields = $rawCertificateFields;
22
    }
23
24
    public function getRawCertificateFields(): array
25
    {
26
        return $this->rawCertificateFields;
27
    }
28
29
    public function getIssuer(): string
30
    {
31
        return $this->rawCertificateFields['issuer']['CN'];
32
    }
33
34
    public function getDomain(): string
35
    {
36
        return $this->rawCertificateFields['subject']['CN'] ?? '';
37
    }
38
39
    public function getAdditionalDomains(): array
40
    {
41
        $additionalDomains = explode(', ', $this->rawCertificateFields['extensions']['subjectAltName'] ?? '');
42
43
        return array_map(function (string $domain) {
44
            return str_replace('DNS:', '', $domain);
45
        }, $additionalDomains);
46
    }
47
48
    public function validFromDate(): Carbon
49
    {
50
        return Carbon::createFromTimestampUTC($this->rawCertificateFields['validFrom_time_t']);
51
    }
52
53
    public function expirationDate(): Carbon
54
    {
55
        return Carbon::createFromTimestampUTC($this->rawCertificateFields['validTo_time_t']);
56
    }
57
58
    public function isExpired(): bool
59
    {
60
        return $this->expirationDate()->isPast();
61
    }
62
63
    public function isValid(string $url = null)
64
    {
65
        if (!Carbon::now()->between($this->validFromDate(), $this->expirationDate())) {
66
            return false;
67
        }
68
69
        if (!empty($url)) {
70
            return $this->appliesToUrl($url ?? $this->getDomain());
71
        }
72
73
        return true;
74
    }
75
76
    public function appliesToUrl(string $url): bool
77
    {
78
        $host = (new Url($url))->getHostName();
79
80
        $certificateHosts = array_merge([$this->getDomain()], $this->getAdditionalDomains());
81
82
        foreach ($certificateHosts as $certificateHost) {
83
            if ($host === $certificateHost) {
84
                return true;
85
            }
86
87
            if ($this->wildcardHostCoversHost($certificateHost, $host)) {
88
                return true;
89
            }
90
        }
91
92
        return false;
93
    }
94
95
    protected function wildcardHostCoversHost(string $wildcardHost, string $host): bool
96
    {
97
        if ($host == $wildcardHost) {
98
            return true;
99
        }
100
101
        if (!starts_with($wildcardHost, '*')) {
102
            return false;
103
        }
104
105
        $wildcardHostWithoutWildcard = substr($wildcardHost, 2);
106
107
        return ends_with($host, $wildcardHostWithoutWildcard);
108
    }
109
}
110