Completed
Push — master ( 8612ec...4dae32 )
by Théo
02:13
created

ShaStrategyAbstract::setPharUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

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 5
nc 2
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 View Code Duplication
    public function download(Updater $updater)
0 ignored issues
show
Duplication introduced by
This method 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...
46
    {
47
        /** Switch remote request errors to HttpRequestExceptions */
48
        set_error_handler(array($updater, 'throwHttpRequestException'));
49
        $result = humbug_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
60
    /**
61
     * Set URL to phar file
62
     *
63
     * @param string $url
64
     */
65
    public function setPharUrl($url)
66
    {
67
        if (!$this->validateAllowedUrl($url)) {
68
            throw new InvalidArgumentException(
69
                sprintf('Invalid url passed as argument: %s.', $url)
70
            );
71
        }
72
        $this->pharUrl = $url;
73
    }
74
75
    /**
76
     * Get URL for phar file
77
     *
78
     * @return string
79
     */
80
    public function getPharUrl()
81
    {
82
        return $this->pharUrl;
83
    }
84
85
    /**
86
     * Set URL to version file
87
     *
88
     * @param string $url
89
     */
90
    public function setVersionUrl($url)
91
    {
92
        if (!$this->validateAllowedUrl($url)) {
93
            throw new InvalidArgumentException(
94
                sprintf('Invalid url passed as argument: %s.', $url)
95
            );
96
        }
97
        $this->versionUrl = $url;
98
    }
99
100
    /**
101
     * Get URL for version file
102
     *
103
     * @return string
104
     */
105
    public function getVersionUrl()
106
    {
107
        return $this->versionUrl;
108
    }
109
110
    protected function validateAllowedUrl($url)
111
    {
112
        return (
113
            filter_var($url, FILTER_VALIDATE_URL)
114
            && in_array(parse_url($url, PHP_URL_SCHEME), self::SUPPORTED_SCHEMES)
115
        );
116
    }
117
}
118