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 ( bca83a...1ac405 )
by François
03:36
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 2015 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
namespace fkooman\VPN\Server\Ca;
18
19
use GuzzleHttp\Client;
20
use GuzzleHttp\Exception\TransferException;
21
22
/**
23
 * Fetch and store the CRL used by OpenVPN.
24
 */
25
class CrlFetcher
26
{
27
    /** @var string */
28
    private $crlUrl;
29
30
    /** @var string */
31
    private $crlPath;
32
33
    /** @var \GuzzleHttp\Client */
34
    private $client;
35
36
    /**
37
     * @param string $crlUrl  the URL location to fetch from
38
     * @param string $crlPath the folder to write to
39
     */
40
    public function __construct($crlUrl, $crlPath, Client $client = null)
41
    {
42
        $this->crlUrl = $crlUrl;
43
        $this->crlPath = $crlPath;
44
        if (null === $client) {
45
            $client = new Client();
46
        }
47
        $this->client = $client;
48
    }
49
50
    /**
51
     * Fetch and store the CRL.
52
     */
53
    public function fetch()
54
    {
55
        $crlFile = sprintf('%s/ca.crl', $this->crlPath);
56
        $tmpFile = sprintf('%s.tmp', $crlFile);
57
58
        try {
59
            $response = $this->client->get($this->crlUrl);
60
            $crlData = $response->getBody();
61
62
            if (false === @file_put_contents($tmpFile, $crlData)) {
63
                // unable to write tmp file
64
                return ['ok' => false, 'error' => 'unable to write CRL'];
65
            }
66
67
            if (false === @rename($tmpFile, $crlFile)) {
68
                // unable to rename tmp file to crl file
69
                if (false === @unlink($tmpFile)) {
70
                    return ['ok' => false, 'error' => 'unable to delete temporary CRL'];
71
                }
72
73
                return ['ok' => false, 'error' => 'unable to rename temporary CRL to active CRL'];
74
            }
75
76
            // succeeded        
77
            return ['ok' => true];
78
        } catch (TransferException $e) {
79
            // Guzzle catch all exception
80
            return ['ok' => false, 'error' => 'unable to download CRL'];
81
        }
82
    }
83
}
84