Completed
Push — master ( 1e2294...0c3db1 )
by Mr
13:48
created

Downloader::curlExec()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace EasyRSA;
6
7
use EasyRSA\Interfaces\ConfigInterface;
8
use EasyRSA\Interfaces\DownloaderInterface;
9
use RuntimeException;
10
11
class Downloader implements DownloaderInterface
12
{
13
    /**
14
     * Url via which possible to get the latest release of EasyRSA.
15
     */
16
    public const URL_LATEST_RELEASE = 'https://api.github.com/repos/OpenVPN/easy-rsa/releases/latest';
17
18
    /**
19
     * @var \EasyRSA\Interfaces\ConfigInterface
20
     */
21
    private ConfigInterface $config;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
22
23
    /**
24
     * Downloader constructor, need configuration for normal usage.
25
     *
26
     * @param \EasyRSA\Interfaces\ConfigInterface $config
27
     */
28
    public function __construct(ConfigInterface $config)
29
    {
30
        $this->config = $config;
31
    }
32
33
    /**
34
     * Exec some operation by cURL.
35
     *
36
     * TODO: Rewrite to Guzzle
37
     *
38
     * @param string      $url
39
     * @param string|null $filename
40
     *
41
     * @return string|null
42
     */
43
    private function curlExec(string $url, string $filename = null): ?string
44
    {
45
        $curl = curl_init();
46
47
        curl_setopt($curl, CURLOPT_USERAGENT, 'useragent');
48
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
49
        curl_setopt($curl, CURLOPT_URL, $url);
50
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
51
52
        // If filename is not set
53
        if (null !== $filename) {
54
            $fp = fopen($filename, 'wb+');
55
            curl_setopt($curl, CURLOPT_FILE, $fp);
56
        }
57
58
        $result = curl_exec($curl);
59
        curl_close($curl);
60
61
        return is_bool($result) ? null : $result;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function getLatestVersion(): string
68
    {
69
        $json = $this->curlExec(self::URL_LATEST_RELEASE);
70
        $json = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
71
72
        return $json['tarball_url'];
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function downloadLatestVersion(): ?string
79
    {
80
        // Get full path to archive file
81
        $filename = $this->config->getArchive();
82
83
        // Get url with latest release
84
        $latest = $this->getLatestVersion();
85
86
        // Download and return status
87
        return $this->curlExec($latest, $filename);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function extractArchive(): array
94
    {
95
        $scripts = $this->config->getScripts();
96
        $archive = $this->config->getArchive();
97
        $result  = [];
98
99
        // Extract only if folder exist
100
        if (mkdir($scripts, 0755, true) || is_dir($scripts)) {
101
            exec("/usr/bin/env tar xfvz $archive -C $scripts --strip-components=1", $result);
102
        } else {
103
            throw new RuntimeException("Folder $scripts can't be created");
104
        }
105
106
        return $result;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getEasyRSA(): void
113
    {
114
        $this->downloadLatestVersion();
115
        $this->extractArchive();
116
    }
117
}
118