MageReport::check()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Mage Scan
4
 *
5
 * PHP version 5
6
 *
7
 * @category  MageScan
8
 * @package   MageScan
9
 * @author    Steve Robbins <[email protected]>
10
 * @copyright 2015 Steve Robbins
11
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
12
 * @link      https://github.com/steverobbins/magescan
13
 */
14
15
namespace MageScan\Check\Patch;
16
17
use GuzzleHttp\Psr7\Response;
18
use MageScan\Check\AbstractCheck;
19
use MageScan\Check\Patch;
20
use MageScan\Request;
21
22
/**
23
 * Check for installed, provided by magereport.com
24
 *
25
 * @category  MageScan
26
 * @package   MageScan
27
 * @author    Steve Robbins <[email protected]>
28
 * @copyright 2015 Steve Robbins
29
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
30
 * @link      https://github.com/steverobbins/magescan
31
 */
32
class MageReport extends AbstractCheck
33
{
34
    const RESULT_SUCCESS = 'ok';
35
    const RESULT_FAIL    = 'fail';
36
    const BASE_URL       = 'https://www.magereport.com/';
37
38
    /**
39
     * The URL we're scanning
40
     *
41
     * @var string
42
     */
43
    private $url;
44
45
    /**
46
     * List of patches to check for
47
     *
48
     * @param array $patches
49
     */
50
    public $patches = [
51
        'scan/result/supee5344',
52
        'scan/result/supee5994',
53
        'scan/result/supee6285',
54
        'scan/result/supee6482',
55
        'scan/result/supee6788',
56
        'scan/result/supee7405',
57
        'scan/result/supee8788',
58
    ];
59
60
    /**
61
     * Set the URL
62
     *
63
     * @param string $url
64
     */
65
    public function __construct($url)
66
    {
67
        $this->url = $url;
68
    }
69
70
    /**
71
     * Check all patches
72
     *
73
     * @return array
74
     */
75
    public function checkAll()
76
    {
77
        $results = [];
78
        $request = new Request(self::BASE_URL);
79
        $paths = $this->patches;
80
        array_walk($paths, function (&$value) {
81
            $value = $value . '?s=' . $this->url;
82
        });
83
        $responses = $request->getMany($paths);
84
        foreach ($responses as $path => $response) {
85
            $results[$this->getPatchName($path)] = $this->parseResponse($response);
86
        }
87
        return $results;
88
    }
89
90
    /**
91
     * Check if a given patch is installed
92
     *
93
     * @param string $endpoint
94
     *
95
     * @return integer
96
     */
97
    public function check($endpoint)
98
    {
99
        $request = new Request(self::BASE_URL);
100
        $response = $request->get($endpoint . '?s=' . $this->url);
101
        return $this->parseResponse($response);
102
    }
103
104
    /**
105
     * Get patch name from path
106
     *
107
     * @param string $path
108
     *
109
     * @return string
110
     */
111
    protected function getPatchName($path)
112
    {
113
        $bits = explode('?', $path);
114
        return str_replace('scan/result/supee', 'SUPEE-', $bits[0]);
115
    }
116
117
    /**
118
     * Derive if patched or not based on the response
119
     *
120
     * @param Response $response
121
     *
122
     * @return string
123
     */
124
    protected function parseResponse(Response $response)
125
    {
126
        $body = json_decode($response->getBody());
127
        if (is_object($body)) {
128
            switch ($body->result) {
129
                case self::RESULT_SUCCESS:
130
                    return Patch::PATCHED;
131
                case self::RESULT_FAIL:
132
                    return Patch::UNPATCHED;
133
            }
134
        }
135
        return Patch::UNKNOWN;
136
    }
137
}
138