Completed
Pull Request — dev (#127)
by Steve
40:22 queued 37:37
created

MageReport   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 9
c 3
b 0
f 0
lcom 1
cbo 3
dl 0
loc 104
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A checkAll() 0 14 2
A check() 0 6 1
A getPatchName() 0 5 1
A parseResponse() 0 13 4
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
    ];
57
58
    /**
59
     * Set the URL
60
     *
61
     * @param string $url
62
     */
63
    public function __construct($url)
64
    {
65
        $this->url = $url;
66
    }
67
68
    /**
69
     * Check all patches
70
     *
71
     * @return array
72
     */
73
    public function checkAll()
74
    {
75
        $results = [];
76
        $request = new Request(self::BASE_URL);
77
        $paths = $this->patches;
78
        array_walk($paths, function (&$value) {
79
            $value = $value . '?s=' . $this->url;
80
        });
81
        $responses = $request->getMany($paths);
82
        foreach ($responses as $path => $response) {
83
            $results[$this->getPatchName($path)] = $this->parseResponse($response);
84
        }
85
        return $results;
86
    }
87
88
    /**
89
     * Check if a given patch is installed
90
     *
91
     * @param string $endpoint
92
     *
93
     * @return integer
94
     */
95
    public function check($endpoint)
96
    {
97
        $request = new Request(self::BASE_URL);
98
        $response = $request->get($endpoint . '?s=' . $this->url);
99
        return $this->parseResponse($response);
100
    }
101
102
    /**
103
     * Get patch name from path
104
     *
105
     * @param string $path
106
     *
107
     * @return string
108
     */
109
    protected function getPatchName($path)
110
    {
111
        $bits = explode('?', $path);
112
        return str_replace('scan/result/supee', 'SUPEE-', $bits[0]);
113
    }
114
115
    /**
116
     * Derive if patched or not based on the response
117
     *
118
     * @param Response $response
119
     *
120
     * @return string
121
     */
122
    protected function parseResponse(Response $response)
123
    {
124
        $body = json_decode($response->getBody());
125
        if (is_object($body)) {
126
            switch ($body->result) {
127
                case self::RESULT_SUCCESS:
128
                    return Patch::PATCHED;
129
                case self::RESULT_FAIL:
130
                    return Patch::UNPATCHED;
131
            }
132
        }
133
        return Patch::UNKNOWN;
134
    }
135
}
136