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.

Reader::readFromUrl()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 4.074

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 46
ccs 20
cts 24
cp 0.8333
rs 8.6315
cc 4
eloc 27
nc 8
nop 2
crap 4.074
1
<?php
2
3
namespace Punkstar\Ssl;
4
5
class Reader
6
{
7
    /**
8
     * Default connection timeout in seconds.
9
     */
10
    const DEFAULT_CONNECTION_TIMEOUT = 5;
11
    
12
    /**
13
     * Default port on which to expect https.
14
     */
15
    const DEFAULT_PORT = 443;
16
    
17
    /**
18
     * Option Flag: Name of option for defining a connection timeout.
19
     */
20
    const OPT_CONNECTION_TIMEOUT = 'connection_timeout';
21
22
    /**
23
     * Connect to a URL and retrieve the SSL certificate.
24
     *
25
     * Available options:
26
     *
27
     *     - connection_timeout: Timeout when connection to the URL, specified in seconds.
28
     *
29
     * @param $url
30
     * @param array $options
31
     * @return Certificate
32
     * @throws Exception
33
     */
34 1
    public function readFromUrl($url, array $options = []): Certificate
35
    {
36 1
        $urlHost = parse_url($url, PHP_URL_HOST);
37
38 1
        if ($urlHost === null) {
39
            $urlHost = $url;
40
        }
41
    
42 1
        $urlPort = parse_url($url, PHP_URL_PORT);
43
    
44 1
        if ($urlPort === null) {
45 1
            $urlPort = self::DEFAULT_PORT;
46
        }
47
    
48 1
        $options = $this->prepareReadFromUrlOptions($options);
49
50 1
        $streamContext = stream_context_create(array(
51 1
            'ssl' => array(
52
                'capture_peer_cert' => TRUE,
53
                'verify_peer'       => FALSE,
54
                'verify_peer_name'  => FALSE
55
            )
56
        ));
57
58 1
        $stream = @stream_socket_client(
59 1
            sprintf('ssl://%s:%d', $urlHost, $urlPort),
60 1
            $errorNumber,
61 1
            $errorString,
62 1
            $options[self::OPT_CONNECTION_TIMEOUT],
63 1
            STREAM_CLIENT_CONNECT,
64 1
            $streamContext
65
        );
66
67 1
        if ($stream) {
68 1
            $streamParams = stream_context_get_params($stream);
69
70 1
            $certResource = $streamParams['options']['ssl']['peer_certificate'];
71
72 1
            return new Certificate($this->certResourceToString($certResource));
73
        }
74
        
75
        throw new Exception(
76
            sprintf('Unable to connect to %s:%d', $urlHost, $urlPort),
77
            Exception::CONNECTION_PROBLEM
78
        );
79
    }
80
    
81
    /**
82
     * @param $file
83
     * @return Certificate
84
     * @throws Exception
85
     */
86 13
    public function readFromFile($file): Certificate
87
    {
88 13
        if (!file_exists($file)) {
89 1
            throw new Exception(sprintf("File '%s' does not exist", $file), Exception::FILE_NOT_FOUND);
90
        }
91
92 12
        return new Certificate(file_get_contents($file));
93
    }
94
95
    /**
96
     * @param $certResource
97
     * @return string
98
     */
99 1
    protected function certResourceToString($certResource): string
100
    {
101 1
        $output = null;
102
103 1
        openssl_x509_export($certResource, $output);
104
105 1
        return $output;
106
    }
107
108
    /**
109
     * @param $options
110
     * @return array
111
     */
112 1
    protected function prepareReadFromUrlOptions(array $options): array
113
    {
114 1
        if (!isset($options[self::OPT_CONNECTION_TIMEOUT])) {
115 1
            $options[self::OPT_CONNECTION_TIMEOUT] = self::DEFAULT_CONNECTION_TIMEOUT;
116
        }
117
118 1
        return $options;
119
    }
120
}
121