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.

Update   A
last analyzed

Complexity

Total Complexity 31

Size/Duplication

Total Lines 253
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 253
c 0
b 0
f 0
rs 9.92
ccs 0
cts 120
cp 0
wmc 31

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSha1() 0 3 1
B deleteFile() 0 24 8
A __construct() 0 12 1
A getVersion() 0 3 1
B copyTo() 0 31 9
A getUrl() 0 3 1
A isNewer() 0 3 1
A getPublicKey() 0 3 1
A getName() 0 3 1
B getFile() 0 49 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Deployer\Component\PharUpdate;
6
7
use Deployer\Component\PharUpdate\Exception\FileException;
8
use Deployer\Component\PharUpdate\Exception\LogicException;
9
use Deployer\Component\PharUpdate\Version\Comparator;
10
use Deployer\Component\PharUpdate\Version\Version;
11
use Phar;
12
use SplFileObject;
13
use UnexpectedValueException;
14
15
/**
16
 * Manages an individual update.
17
 *
18
 * @author Kevin Herrera <[email protected]>
19
 */
20
class Update
21
{
22
    /**
23
     * The temporary file path.
24
     *
25
     * @var string|null
26
     */
27
    private $file;
28
29
    /**
30
     * The name of the update file.
31
     *
32
     * @var string
33
     */
34
    private $name;
35
36
    /**
37
     * The URL where the public key can be downloaded from.
38
     *
39
     * @var string
40
     */
41
    private $publicKey;
42
43
    /**
44
     * The SHA1 file checksum.
45
     *
46
     * @var string
47
     */
48
    private $sha1;
49
50
    /**
51
     * The URL where the update can be downloaded from.
52
     *
53
     * @var string
54
     */
55
    private $url;
56
57
    /**
58
     * The version of the update.
59
     *
60
     * @var Version
61
     */
62
    private $version;
63
64
    /**
65
     * Sets the update information.
66
     *
67
     * @param string  $name    The name of the update file.
68
     * @param string  $sha1    The SHA1 file checksum.
69
     * @param string  $url     The URL where the update can be downloaded from.
70
     * @param Version $version The version of the update.
71
     * @param string  $key     The URL where the public key can be downloaded
72
     *                         from.
73
     */
74
    public function __construct(
75
        string $name,
76
        string $sha1,
77
        string $url,
78
        Version $version,
79
        string $key = null,
80
    ) {
81
        $this->name = $name;
82
        $this->publicKey = $key;
83
        $this->sha1 = $sha1;
84
        $this->url = $url;
85
        $this->version = $version;
86
    }
87
88
    /**
89
     * Copies the update file to the destination.
90
     *
91
     * @param string $file The target file.
92
     *
93
     * @throws Exception\Exception
94
     * @throws FileException If the file could not be replaced.
95
     */
96
    public function copyTo(string $file): void
97
    {
98
        if (null === $this->file) {
99
            throw LogicException::create(
100
                'The update file has not been downloaded.',
101
            );
102
        }
103
104
        $mode = 0o755;
105
106
        if (file_exists($file)) {
107
            $mode = fileperms($file) & 511;
108
        }
109
110
        if (false === @copy($this->file, $file)) {
111
            throw FileException::lastError();
112
        }
113
114
        if (false === @chmod($file, $mode)) {
115
            throw FileException::lastError();
116
        }
117
118
        $key = $file . '.pubkey';
119
120
        if (file_exists($this->file . '.pubkey')) {
121
            if (false === @copy($this->file . '.pubkey', $key)) {
122
                throw FileException::lastError();
123
            }
124
        } elseif (file_exists($key)) {
125
            if (false === @unlink($key)) {
126
                throw FileException::lastError();
127
            }
128
        }
129
    }
130
131
    /**
132
     * Cleans up by deleting the temporary update file.
133
     *
134
     * @throws FileException If the file could not be deleted.
135
     */
136
    public function deleteFile(): void
137
    {
138
        if ($this->file) {
139
            if (file_exists($this->file)) {
140
                if (false === @unlink($this->file)) {
141
                    throw FileException::lastError();
142
                }
143
            }
144
145
            if (file_exists($this->file . '.pubkey')) {
146
                if (false === @unlink($this->file . '.pubkey')) {
147
                    throw FileException::lastError();
148
                }
149
            }
150
151
            $dir = dirname($this->file);
152
153
            if (file_exists($dir)) {
154
                if (false === @rmdir($dir)) {
155
                    throw FileException::lastError();
156
                }
157
            }
158
159
            $this->file = null;
160
        }
161
    }
162
163
    /**
164
     * Downloads the update file to a temporary location.
165
     *
166
     * @return string The temporary file path.
167
     *
168
     * @throws Exception\Exception
169
     * @throws FileException            If the SHA1 checksum differs.
170
     * @throws UnexpectedValueException If the Phar is corrupt.
171
     */
172
    public function getFile(): ?string
173
    {
174
        if (null === $this->file) {
175
            unlink($this->file = tempnam(sys_get_temp_dir(), 'upd'));
176
            mkdir($this->file);
177
178
            $this->file .= DIRECTORY_SEPARATOR . $this->name;
179
180
            $in = new SplFileObject($this->url, 'rb', false);
181
            $out = new SplFileObject($this->file, 'wb', false);
182
183
            while (false === $in->eof()) {
184
                $out->fwrite($in->fgets());
185
            }
186
187
            unset($in, $out);
188
189
            if ($this->publicKey) {
190
                $in = new SplFileObject($this->publicKey, 'r', false);
191
                $out = new SplFileObject($this->file . '.pubkey', 'w', false);
192
193
                while (false === $in->eof()) {
194
                    $out->fwrite($in->fgets());
195
                }
196
197
                unset($in, $out);
198
            }
199
200
            if ($this->sha1 !== ($sha1 = sha1_file($this->file))) {
201
                $this->deleteFile();
202
203
                throw FileException::create(
204
                    'Mismatch of the SHA1 checksum (%s) of the downloaded file (%s).',
205
                    $this->sha1,
206
                    $sha1,
207
                );
208
            }
209
210
            // double check
211
            try {
212
                new Phar($this->file);
213
            } catch (UnexpectedValueException $exception) {
214
                $this->deleteFile();
215
216
                throw $exception;
217
            }
218
        }
219
220
        return $this->file;
221
    }
222
223
    /**
224
     * Returns name of the update file.
225
     */
226
    public function getName(): string
227
    {
228
        return $this->name;
229
    }
230
231
    /**
232
     * Returns the URL where the public key can be downloaded from.
233
     */
234
    public function getPublicKey(): string
235
    {
236
        return $this->publicKey;
237
    }
238
239
    /**
240
     * Returns the SHA1 file checksum.
241
     */
242
    public function getSha1(): string
243
    {
244
        return $this->sha1;
245
    }
246
247
    /**
248
     * Returns the URL where the update can be downloaded from.
249
     */
250
    public function getUrl(): string
251
    {
252
        return $this->url;
253
    }
254
255
    /**
256
     * Returns the version of the update.
257
     */
258
    public function getVersion(): Version
259
    {
260
        return $this->version;
261
    }
262
263
    /**
264
     * Checks if this update is newer than the version given.
265
     *
266
     * @param Version $version The current version.
267
     *
268
     * @return boolean TRUE if the update is newer, FALSE if not.
269
     */
270
    public function isNewer(Version $version): bool
271
    {
272
        return Comparator::isGreaterThan($this->version, $version);
273
    }
274
}
275