Completed
Pull Request — master (#43)
by Pádraic
02:52 queued 52s
created

Sha256Strategy::download()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 14
loc 14
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
/**
3
 * Humbug
4
 *
5
 * @category   Humbug
6
 * @package    Humbug
7
 * @copyright  Copyright (c) 2017 Pádraic Brady (http://blog.astrumfutura.com)
8
 * @license    https://github.com/padraic/phar-updater/blob/master/LICENSE New BSD License
9
 *
10
 * This class is partially patterned after Composer's self-update.
11
 */
12
13
namespace Humbug\SelfUpdate\Strategy;
14
15
use Humbug\SelfUpdate\Updater;
16
use Humbug\SelfUpdate\Exception\HttpRequestException;
17
use Humbug\SelfUpdate\Exception\InvalidArgumentException;
18
19 View Code Duplication
final class Sha256Strategy extends ShaStrategyAbstract
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
20
{
21
    /**
22
     * Download the remote Phar file.
23
     *
24
     * @param Updater $updater
25
     * @return void
26
     */
27
    public function download(Updater $updater)
28
    {
29
        parent::download($updater);
30
31
        $tmpVersion = hash_file('sha256', $updater->getTempPharFile());
32
33
        if ($tmpVersion !== $updater->getNewVersion()) {
34
            throw new HttpRequestException(sprintf(
35
                'Download file appears to be corrupted or outdated. The file '
36
                    . 'received does not have the expected SHA-256 hash: %s.',
37
                $updater->getNewVersion()
38
            ));
39
        }
40
    }
41
42
    /**
43
     * Retrieve the current version available remotely.
44
     *
45
     * @param Updater $updater
46
     * @return string|bool
47
     */
48
    public function getCurrentRemoteVersion(Updater $updater)
49
    {
50
        /** Switch remote request errors to HttpRequestExceptions */
51
        set_error_handler(array($updater, 'throwHttpRequestException'));
52
        $version = file_get_contents($this->getVersionUrl());
53
        restore_error_handler();
54
        if (false === $version) {
55
            throw new HttpRequestException(sprintf(
56
                'Request to URL failed: %s', $this->getVersionUrl()
57
            ));
58
        }
59
        if (empty($version)) {
60
            throw new HttpRequestException(
61
                'Version request returned empty response.'
62
            );
63
        }
64
        if (!preg_match('%^[a-z0-9]{64}%', $version, $matches)) {
65
            throw new HttpRequestException(
66
                'Version request returned incorrectly formatted response.'
67
            );
68
        }
69
70
        return $matches[0];
71
    }
72
73
    /**
74
     * Retrieve the current version of the local phar file.
75
     *
76
     * @param Updater $updater
77
     * @return string
78
     */
79
    public function getCurrentLocalVersion(Updater $updater)
80
    {
81
        return hash_file('sha256', $updater->getLocalPharFile());
82
    }
83
}
84