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 ( b1bd2d...4d90e6 )
by Nick
02:35
created

Reader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 92%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 8
c 6
b 0
f 0
lcom 0
cbo 2
dl 0
loc 89
ccs 23
cts 25
cp 0.92
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B readFromUrl() 0 30 3
A readFromFile() 0 8 2
A certResourceToString() 0 8 1
A prepareReadFromUrlOptions() 0 8 2
1
<?php
2
3
namespace Punkstar\Ssl;
4
5
class Reader
6
{
7
    const DEFAULT_CONNECTION_TIMEOUT = 5;
8
9
    const OPT_CONNECTION_TIMEOUT = 'connection_timeout';
10
11
    /**
12
     * Connect to a URL and retrieve the SSL certificate.
13
     *
14
     * Available options:
15
     *
16
     *     - connection_timeout: Timeout when connection to the URL, specified in seconds.
17
     *
18
     * @param $url
19
     * @param array $options
20
     * @return Certificate
21
     * @throws Exception
22
     */
23 1
    public function readFromUrl($url, $options = [])
24
    {
25 1
        $urlHost = parse_url($url, PHP_URL_HOST);
26
27 1
        if ($urlHost === null) {
28
            $urlHost = $url;
29
        }
30
31 1
        $options = $this->prepareReadFromUrlOptions($options);
32
33 1
        $streamContext = stream_context_create(array(
34
            "ssl" => array(
35
                "capture_peer_cert" => TRUE,
36
                "verify_peer" => FALSE,
37
                "verify_peer_name" => FALSE
38
            )
39 1
        ));
40
41 1
        $stream = @stream_socket_client("ssl://" . $urlHost . ":443", $errorNumber, $errorString, $options[self::OPT_CONNECTION_TIMEOUT], STREAM_CLIENT_CONNECT, $streamContext);
42
43 1
        if ($stream) {
44 1
            $streamParams = stream_context_get_params($stream);
45
46 1
            $certResource = $streamParams['options']['ssl']['peer_certificate'];
47
48 1
            return new Certificate($this->certResourceToString($certResource));
49
        } else {
50
            throw new Exception(sprintf("Unable to connect to %s", $urlHost), Exception::CONNECTION_PROBLEM);
51
        }
52
    }
53
    
54
    /**
55
     * @param $file
56
     * @return Certificate
57
     * @throws Exception
58
     */
59 8
    public function readFromFile($file)
60
    {
61 8
        if (!file_exists($file)) {
62 1
            throw new Exception(sprintf("File '%s' does not exist", $file), Exception::FILE_NOT_FOUND);
63
        }
64
65 7
        return new Certificate(file_get_contents($file));
66
    }
67
68
    /**
69
     * @param $certResource
70
     * @return string
71
     */
72 1
    protected function certResourceToString($certResource)
73
    {
74 1
        $output = null;
75
76 1
        openssl_x509_export($certResource, $output);
77
78 1
        return $output;
79
    }
80
81
    /**
82
     * @param $options
83
     * @return mixed
84
     */
85 1
    protected function prepareReadFromUrlOptions($options)
86
    {
87 1
        if (!isset($options[self::OPT_CONNECTION_TIMEOUT])) {
88 1
            $options[self::OPT_CONNECTION_TIMEOUT] = self::DEFAULT_CONNECTION_TIMEOUT;
89
        }
90
91 1
        return $options;
92
    }
93
}
94