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.

Downloader   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 86
ccs 55
cts 55
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
B prepareCertificateResponse() 0 28 4
A downloadRevocationListFromUrl() 0 12 1
B downloadCertificateFromUrl() 0 41 3
1
<?php
2
3
namespace LiquidWeb\SslCertificate;
4
5
use Throwable;
6
use phpseclib\File\X509;
7
use LiquidWeb\SslCertificate\Exceptions\Handler;
8
9
class Downloader
10
{
11 12
    public static function downloadCertificateFromUrl(string $url, int $timeout = 30): array
12
    {
13
        // Trusted variable to keep track of SSL trust
14 12
        $trusted = true;
15 12
        $sslConfig = StreamConfig::configSecure();
16 12
        $parsedUrl = new Url($url);
17 12
        $client = null;
18
19
        try {
20 12
            $client = stream_socket_client(
21 12
                "ssl://{$parsedUrl->getTestURL()}",
22 12
                $errorNumber,
23 12
                $errorDescription,
24 12
                $timeout,
25 12
                STREAM_CLIENT_CONNECT,
26 12
                $sslConfig->getContext()
27
            );
28 4
            unset($sslConfig);
29 8
        } catch (Throwable $thrown) {
30
            // Try again in insecure mode
31 8
            $sslConfig = StreamConfig::configInsecure();
32 8
            $trusted = false;
33
34
            try {
35
                // As the URL failed verification we set to false
36 8
                $client = stream_socket_client(
37 8
                    'ssl://'.$parsedUrl->getTestURL(),
38 8
                    $errorNumber,
39 8
                    $errorDescription,
40 8
                    $timeout,
41 8
                    STREAM_CLIENT_CONNECT,
42 8
                    $sslConfig->getContext()
43
                );
44 2
                unset($sslConfig);
45 6
            } catch (Throwable $thrown) {
46 6
                (new Handler($thrown))->downloadHandler($parsedUrl);
47
            }
48
        }
49
50 6
        return self::prepareCertificateResponse($client, $trusted, $parsedUrl);
51
    }
52
53 6
    private static function prepareCertificateResponse($resultClient, bool $trusted, Url $parsedUrl): array
54
    {
55 6
        $response = stream_context_get_options($resultClient);
56 6
        $connectionInfo = stream_get_meta_data($resultClient)['crypto'];
57 6
        unset($resultClient);
58 6
        $mainCert = openssl_x509_parse($response['ssl']['peer_certificate'], true);
59
60 6
        $full_chain = [];
61 6
        if (count($response['ssl']['peer_certificate_chain']) > 1) {
62 4
            foreach ($response['ssl']['peer_certificate_chain'] as $cert) {
63 4
                $parsedCert = openssl_x509_parse($cert, true);
64 4
                $isChain = ! ($parsedCert['hash'] === $mainCert['hash']);
65 4
                if ($isChain === true) {
66 4
                    array_push($full_chain, $parsedCert);
67
                }
68
            }
69
        }
70
71
        return [
72 6
            'inputDomain' => $parsedUrl->getInputUrl(),
73 6
            'tested' => $parsedUrl->getTestURL(),
74 6
            'trusted' => $trusted,
75 6
            'dns-resolves-to' => $parsedUrl->getIp(),
76 6
            'cert' => $mainCert,
77 6
            'full_chain' => $full_chain,
78 6
            'connection' => $connectionInfo,
79
        ];
80
    }
81
82 2
    public static function downloadRevocationListFromUrl(string $url): array
83
    {
84 2
        $parsedUrl = new Url($url);
85 2
        $csrConfig = StreamConfig::configCrl();
86 2
        $file = file_get_contents($parsedUrl->getValidatedURL(), false, $csrConfig->getContext());
87 2
        unset($csrConfig, $parsedUrl);
88 2
        $x509 = new X509();
89 2
        $crl = $x509->loadCRL($file);
90 2
        unset($x509, $file);
91
92 2
        return $crl;
93
    }
94
}
95