Completed
Push — master ( 60354b...a4c346 )
by Steve
11:43
created

Check::getVersions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * Magento Version Identification
4
 *
5
 * PHP version 5
6
 *
7
 * @author    Steve Robbins <[email protected]>
8
 * @copyright 2015 Steve Robbins
9
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
10
 * @link      https://github.com/steverobbins/magento-version-identification-php
11
 */
12
13
namespace Mvi;
14
15
/**
16
 * Checks a Magento URL and tries to determine the edition and version
17
 */
18
class Check
19
{
20
    /**
21
     * The URL we're checking
22
     *
23
     * @var string
24
     */
25
    private $url;
26
27
    /**
28
     * Initialize
29
     *
30
     * @param string $url
31
     */
32
    public function __construct($url = null)
33
    {
34
        if ($url !== null) {
35
            $this->setUrl($url);
36
        }
37
    }
38
39
    /**
40
     * Set the URL to check
41
     *
42
     * @param string $url
43
     *
44
     * @return Mvi\Check
45
     */
46
    public function setUrl($url)
47
    {
48
        $this->validateUrl($url);
49
        $this->url = $url;
50
        return $this;
51
    }
52
53
    /**
54
     * Get the edition and version for the url
55
     *
56
     * @return array|boolean
57
     */
58
    public function getInfo()
59
    {
60
        $versions = $this->getVersions();
61
        foreach ($versions as $file => $hash) {
62
            $md5 = md5(@file_get_contents($this->url . $file));
63
            if (isset($hash[$md5])) {
64
                return $hash[$md5];
65
            }
66
        }
67
        return false;
68
    }
69
70
    /**
71
     * Get version information from json
72
     *
73
     * @return array
74
     */
75
    public function getVersions()
76
    {
77
        return json_decode(
78
            file_get_contents(dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'version.json'),
79
            true
80
        );
81
    }
82
83
    /**
84
     * Validate the URL to check
85
     *
86
     * @param string $url
87
     *
88
     * @return void
89
     */
90
    protected function validateUrl($url)
91
    {
92
        if (!(substr($url, 0, 7) === 'http://' || substr($url, 0, 8) === 'https://')) {
93
            throw new \InvalidArgumentException('The URL must start with "http://" or "https://"');
94
        }
95
        if (substr($url, -1) !== '/') {
96
            throw new \InvalidArgumentException('The URL must end in a slash (/)');
97
        }
98
    }
99
}
100