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 ( 35efb6...ee0423 )
by François
02:44
created

CrlFetcher::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
/**
3
 * Copyright 2016 François Kooman <[email protected]>.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace fkooman\VPN\Server;
19
20
use GuzzleHttp\Client;
21
use RuntimeException;
22
23
/**
24
 * Fetch and store the CRL used by OpenVPN.
25
 */
26
class CrlFetcher
27
{
28
    /** @var string */
29
    private $crlUrl;
30
31
    /** @var string */
32
    private $crlPath;
33
34
    /** @var \GuzzleHttp\Client */
35
    private $client;
36
37
    /**
38
     * @param string $crlUrl  the URL location to fetch from
39
     * @param string $crlPath the folder to write to
40
     */
41
    public function __construct($crlUrl, $crlPath, Client $client = null)
42
    {
43
        $this->crlUrl = $crlUrl;
44
        $this->crlPath = $crlPath;
45
        if (null === $client) {
46
            $client = new Client();
47
        }
48
        $this->client = $client;
49
    }
50
51
    /**
52
     * Fetch and store the CRL.
53
     */
54
    public function fetch()
55
    {
56
        $crlFile = sprintf('%s/ca.crl', $this->crlPath);
57
        $tmpFile = sprintf('%s.tmp', $crlFile);
58
59
        $response = $this->client->get($this->crlUrl);
60
        $crlData = $response->getBody();
61
62
        if (!is_dir($this->crlPath)) {
63
            @mkdir($this->crlPath);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
64
        }
65
66
        if (false === @file_put_contents($tmpFile, $crlData)) {
67
            // unable to write tmp file
68
            throw new RuntimeException('unable to write CRL');
69
        }
70
71
        if (false === @rename($tmpFile, $crlFile)) {
72
            // unable to rename tmp file to crl file
73
            if (false === @unlink($tmpFile)) {
74
                throw new RuntimeException('unable to delete temporary CRL');
75
            }
76
77
            throw new RuntimeException('unable to rename temporary CRL to active CRL');
78
        }
79
    }
80
}
81