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

Sha256Strategy::getCurrentRemoteVersion()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 24
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 24
loc 24
rs 8.6845
cc 4
eloc 14
nc 4
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
     * Retrieve the current version available remotely.
23
     *
24
     * @param Updater $updater
25
     * @return string|bool
26
     */
27
    public function getCurrentRemoteVersion(Updater $updater)
28
    {
29
        /** Switch remote request errors to HttpRequestExceptions */
30
        set_error_handler(array($updater, 'throwHttpRequestException'));
31
        $version = humbug_get_contents($this->getVersionUrl());
32
        restore_error_handler();
33
        if (false === $version) {
34
            throw new HttpRequestException(sprintf(
35
                'Request to URL failed: %s', $this->getVersionUrl()
36
            ));
37
        }
38
        if (empty($version)) {
39
            throw new HttpRequestException(
40
                'Version request returned empty response.'
41
            );
42
        }
43
        if (!preg_match('%^[a-z0-9]{64}%', $version, $matches)) {
44
            throw new HttpRequestException(
45
                'Version request returned incorrectly formatted response.'
46
            );
47
        }
48
49
        return $matches[0];
50
    }
51
52
    /**
53
     * Retrieve the current version of the local phar file.
54
     *
55
     * @param Updater $updater
56
     * @return string
57
     */
58
    public function getCurrentLocalVersion(Updater $updater)
59
    {
60
        return hash_file('sha256', $updater->getLocalPharFile());
61
    }
62
}
63