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   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRawCertificateFields() 0 4 1
A getIssuer() 0 4 1
A getDomain() 0 4 1
A getAdditionalDomains() 0 8 1
A validFromDate() 0 4 1
A expirationDate() 0 4 1
A isExpired() 0 4 1
A isValid() 0 12 3
A appliesToUrl() 0 18 4
A wildcardHostCoversHost() 0 14 3
A createForHostName() 0 6 1
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