Code Duplication    Length = 46-46 lines in 2 locations

src/Strategy/Sha256Strategy.php 1 location

@@ 19-64 (lines=46) @@
16
use Humbug\SelfUpdate\Exception\HttpRequestException;
17
use Humbug\SelfUpdate\Exception\InvalidArgumentException;
18
19
class Sha256Strategy extends ShaStrategyAbstract
20
{
21
22
    /**
23
     * Retrieve the current version available remotely.
24
     *
25
     * @param Updater $updater
26
     * @return string|bool
27
     */
28
    public function getCurrentRemoteVersion(Updater $updater)
29
    {
30
        /** Switch remote request errors to HttpRequestExceptions */
31
        set_error_handler(array($updater, 'throwHttpRequestException'));
32
        $version = humbug_get_contents($this->getVersionUrl());
33
        restore_error_handler();
34
        if (false === $version) {
35
            throw new HttpRequestException(sprintf(
36
                'Request to URL failed: %s', $this->getVersionUrl()
37
            ));
38
        }
39
        if (empty($version)) {
40
            throw new HttpRequestException(
41
                'Version request returned empty response.'
42
            );
43
        }
44
        if (!preg_match('%^[a-z0-9]{64}%', $version, $matches)) {
45
            throw new HttpRequestException(
46
                'Version request returned incorrectly formatted response.'
47
            );
48
        }
49
50
        return $matches[0];
51
    }
52
53
    /**
54
     * Retrieve the current version of the local phar file.
55
     *
56
     * @param Updater $updater
57
     * @return string
58
     */
59
    public function getCurrentLocalVersion(Updater $updater)
60
    {
61
        return hash_file('sha256', $updater->getLocalPharFile());
62
    }
63
64
}

src/Strategy/ShaStrategy.php 1 location

@@ 19-64 (lines=46) @@
16
use Humbug\SelfUpdate\Exception\HttpRequestException;
17
use Humbug\SelfUpdate\Exception\InvalidArgumentException;
18
19
class ShaStrategy extends ShaStrategyAbstract
20
{
21
22
    /**
23
     * Retrieve the current version available remotely.
24
     *
25
     * @param Updater $updater
26
     * @return string|bool
27
     */
28
    public function getCurrentRemoteVersion(Updater $updater)
29
    {
30
        /** Switch remote request errors to HttpRequestExceptions */
31
        set_error_handler(array($updater, 'throwHttpRequestException'));
32
        $version = humbug_get_contents($this->getVersionUrl());
33
        restore_error_handler();
34
        if (false === $version) {
35
            throw new HttpRequestException(sprintf(
36
                'Request to URL failed: %s', $this->getVersionUrl()
37
            ));
38
        }
39
        if (empty($version)) {
40
            throw new HttpRequestException(
41
                'Version request returned empty response.'
42
            );
43
        }
44
        if (!preg_match('%^[a-z0-9]{40}%', $version, $matches)) {
45
            throw new HttpRequestException(
46
                'Version request returned incorrectly formatted response.'
47
            );
48
        }
49
50
        return $matches[0];
51
    }
52
53
    /**
54
     * Retrieve the current version of the local phar file.
55
     *
56
     * @param Updater $updater
57
     * @return string
58
     */
59
    public function getCurrentLocalVersion(Updater $updater)
60
    {
61
        return sha1_file($updater->getLocalPharFile());
62
    }
63
64
}
65