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

Request   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 8
Bugs 0 Features 1
Metric Value
wmc 17
c 8
b 0
f 1
lcom 1
cbo 1
dl 0
loc 140
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A getUrl() 0 4 1
A get() 0 4 2
A getMany() 0 8 2
A post() 0 4 1
A parseHeader() 0 13 3
B findMatchInResponse() 0 12 6
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;
16
17
use GuzzleHttp\Client;
18
use GuzzleHttp\Promise;
19
20
/**
21
 * Make a cURL request to a url
22
 *
23
 * @category  MageScan
24
 * @package   MageScan
25
 * @author    Steve Robbins <[email protected]>
26
 * @copyright 2015 Steve Robbins
27
 * @license   http://creativecommons.org/licenses/by/4.0/ CC BY 4.0
28
 * @link      https://github.com/steverobbins/magescan
29
 */
30
class Request
31
{
32
    const REQUEST_TIMEOUT = 15.0;
33
34
    /**
35
     * If true, SSL does not have to be verified
36
     *
37
     * @var boolean
38
     */
39
    protected $insecure = false;
40
41
    /**
42
     * The base URL of the Magento application
43
     *
44
     * @var string
45
     */
46
    protected $url;
47
48
    /**
49
     * Client cache
50
     *
51
     * @var GuzzleHttp\Client
52
     */
53
    protected $client;
54
55
    /**
56
     * Initialize request object
57
     *
58
     * @param string  $baseUri
59
     * @param boolean $verify
60
     */
61
    public function __construct($baseUri = false, $verify = true)
62
    {
63
        $this->url = $baseUri;
0 ignored issues
show
Documentation Bug introduced by
It seems like $baseUri can also be of type false. However, the property $url is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
64
        $params = [
65
            'verify'   => $verify,
66
            'http_errors' => false
67
        ];
68
        if ($this->url !== false) {
69
            $params['base_uri'] = $this->url;
70
        }
71
        $this->client = new Client($params);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \GuzzleHttp\Client($params) of type object<GuzzleHttp\Client> is incompatible with the declared type object<MageScan\GuzzleHttp\Client> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
72
    }
73
74
    /**
75
     * Get the base url of this request
76
     *
77
     * @return string
78
     */
79
    public function getUrl()
80
    {
81
        return $this->url;
82
    }
83
84
    /**
85
     * Get a path
86
     *
87
     * @param string|boolean $path
88
     * @param array          $params
89
     *
90
     * @return GuzzleHttp\Psr7\Response
91
     */
92
    public function get($path = false, array $params = [])
93
    {
94
        return $this->client->get($path ? '/' . $path : null, $params);
95
    }
96
97
    /**
98
     * Get many paths asyncronously
99
     *
100
     * @param string[] $paths
101
     * @param array    $params
102
     *
103
     * @return GuzzleHttp\Psr7\Response[]
104
     */
105
    public function getMany($paths, array $params = [])
106
    {
107
        $promises = [];
108
        foreach ($paths as $path) {
109
            $promises[$path] = $this->client->getAsync('/' . $path, $params);
110
        }
111
        return Promise\unwrap($promises);
112
    }
113
114
    /**
115
     * Post to a path
116
     *
117
     * @param string $path
118
     * @param array  $params
119
     *
120
     * @return GuzzleHttp\Psr7\Response
121
     */
122
    public function post($path, array $params = [])
123
    {
124
        return $this->client->post('/' . $path, $params);
125
    }
126
127
    /**
128
     * Manipulate header data into a parsable format
129
     *
130
     * @param string $rawData
131
     *
132
     * @return array
133
     */
134
    public function parseHeader($rawData)
135
    {
136
        $data = [];
137
        foreach (explode("\n", trim($rawData)) as $line) {
138
            $bits = explode(': ', $line);
139
            if (count($bits) > 1) {
140
                $key = $bits[0];
141
                unset($bits[0]);
142
                $data[$key] = trim(implode(': ', $bits));
143
            }
144
        }
145
        return $data;
146
    }
147
148
    /**
149
     * Parse out the count from the response
150
     *
151
     * @param string  $response
152
     * @param string  $pattern
153
     * @param boolean $returnAll
154
     *
155
     * @return string|array|boolean
156
     */
157
    public function findMatchInResponse($response, $pattern, $returnAll = false)
158
    {
159
        if (empty($response)) {
160
            return false;
161
        }
162
        if (preg_match($pattern, $response, $match)
163
            && (isset($match[1]) || $returnAll)
164
        ) {
165
            return $returnAll ? $match : $match[1];
166
        }
167
        return false;
168
    }
169
}
170