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

ShaStrategyAbstract::download()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.439
cc 6
eloc 22
nc 6
nop 1
1
<?php
2
/**
3
 * Humbug
4
 *
5
 * @category   Humbug
6
 * @package    Humbug
7
 * @copyright  Copyright (c) 2015 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
abstract class ShaStrategyAbstract implements StrategyInterface
20
{
21
22
    /** @private */
23
    const SUPPORTED_SCHEMES = [
24
        'http',
25
        'https',
26
        'file',
27
    ];
28
29
    /**
30
     * @var string
31
     */
32
    protected $versionUrl;
33
34
    /**
35
     * @var string
36
     */
37
    protected $pharUrl;
38
39
    /**
40
     * Download the remote Phar file.
41
     *
42
     * @param Updater $updater
43
     * @return void
44
     */
45
    public function download(Updater $updater)
46
    {
47
        /** Switch remote request errors to HttpRequestExceptions */
48
        set_error_handler(array($updater, 'throwHttpRequestException'));
49
        $result = file_get_contents($this->getPharUrl());
50
        restore_error_handler();
51
        if (false === $result) {
52
            throw new HttpRequestException(sprintf(
53
                'Request to URL failed: %s', $this->getPharUrl()
54
            ));
55
        }
56
57
        file_put_contents($updater->getTempPharFile(), $result);
58
59
        if ($this instanceof ShaStrategy
60
            || $this instanceof Sha256Strategy
61
        ) {
62
            if ($this instanceof ShaStrategy) {
63
                $tmpVersion = sha1_file($updater->getTempPharFile());
64
                $algo = 'SHA-1';
65
            } else {
66
                $tmpVersion = hash_file('sha256', $updater->getTempPharFile());
67
                $algo = 'SHA-256';
68
            }
69
            if ($tmpVersion !== $updater->getNewVersion()) {
70
                throw new HttpRequestException(sprintf(
71
                    'Download file appears to be corrupted or outdated. The file '
72
                        . 'received does not have the expected %s hash: %s.',
73
                    $algo,
74
                    $updater->getNewVersion()
75
                ));
76
            }
77
        }
78
    }
79
80
    /**
81
     * Set URL to phar file
82
     *
83
     * @param string $url
84
     */
85
    public function setPharUrl($url)
86
    {
87
        if (!$this->validateAllowedUrl($url)) {
88
            throw new InvalidArgumentException(
89
                sprintf('Invalid url passed as argument: %s.', $url)
90
            );
91
        }
92
        $this->pharUrl = $url;
93
    }
94
95
    /**
96
     * Get URL for phar file
97
     *
98
     * @return string
99
     */
100
    public function getPharUrl()
101
    {
102
        return $this->pharUrl;
103
    }
104
105
    /**
106
     * Set URL to version file
107
     *
108
     * @param string $url
109
     */
110
    public function setVersionUrl($url)
111
    {
112
        if (!$this->validateAllowedUrl($url)) {
113
            throw new InvalidArgumentException(
114
                sprintf('Invalid url passed as argument: %s.', $url)
115
            );
116
        }
117
        $this->versionUrl = $url;
118
    }
119
120
    /**
121
     * Get URL for version file
122
     *
123
     * @return string
124
     */
125
    public function getVersionUrl()
126
    {
127
        return $this->versionUrl;
128
    }
129
130
    protected function validateAllowedUrl($url)
131
    {
132
        return (
133
            filter_var($url, FILTER_VALIDATE_URL)
134
            && in_array(parse_url($url, PHP_URL_SCHEME), self::SUPPORTED_SCHEMES)
135
        );
136
    }
137
}
138