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 ( 1d2318...ea7909 )
by Nick
07:43
created

Reader::readFromUrl()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0593

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 28
ccs 13
cts 16
cp 0.8125
rs 8.8571
cc 3
eloc 16
nc 4
nop 1
crap 3.0593
1
<?php
2
3
namespace Punkstar\Ssl;
4
5
class Reader
6
{
7
    const CONNECTION_TIMEOUT = 30;
8
9
    /**
10
     * @param $url
11
     * @return Certificate
12
     * @throws Exception
13
     */
14 1
    public function readFromUrl($url)
15
    {
16 1
        $urlHost = parse_url($url, PHP_URL_HOST);
17
18 1
        if ($urlHost === null) {
19
            $urlHost = $url;
20
        }
21
22 1
        $streamContext = stream_context_create(array(
23
            "ssl" => array(
24 1
                "capture_peer_cert" => TRUE,
25 1
                "verify_peer" => FALSE,
26
                "verify_peer_name" => FALSE
27 1
            )
28 1
        ));
29
30 1
        $stream = @stream_socket_client("ssl://" . $urlHost . ":443", $errorNumber, $errorString, self::CONNECTION_TIMEOUT, STREAM_CLIENT_CONNECT, $streamContext);
31
32 1
        if ($stream) {
33 1
            $streamParams = stream_context_get_params($stream);
34
35 1
            $certResource = $streamParams['options']['ssl']['peer_certificate'];
36
37 1
            return new Certificate($this->certResourceToString($certResource));
38
        } else {
39
            throw new Exception(sprintf("Unable to connect to %s", $urlHost));
40
        }
41
    }
42
    
43
    /**
44
     * @param $file
45
     * @return Certificate
46
     * @throws Exception
47
     */
48 8
    public function readFromFile($file)
49
    {
50 8
        if (!file_exists($file)) {
51 1
            throw new Exception(sprintf("File '%s' does not exist", $file), Exception::FILE_NOT_FOUND);
52
        }
53
54 7
        return new Certificate(file_get_contents($file));
55
    }
56
57
    /**
58
     * @param $certResource
59
     * @return string
60
     */
61 1
    protected function certResourceToString($certResource)
62
    {
63 1
        $output = null;
64
65 1
        openssl_x509_export($certResource, $output);
66
67 1
        return $output;
68
    }
69
}
70