Failed Conditions
Pull Request — dev (#132)
by Steve
02:11
created

src/MageScan/Request.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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|boolean
45
     */
46
    protected $url;
47
48
    /**
49
     * Client cache
50
     *
51
     * @var 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;
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);
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 headers
99
     *
100
     * @param string|boolean $path
101
     * @param array          $params
102
     *
103
     * @return GuzzleHttp\Psr7\Response
104
     */
105
    public function head($path = false, array $params = [])
106
    {
107
        return $this->client->head($path ? '/' . $path : null, $params);
108
    }
109
110
    /**
111
     * Get many paths asyncronously
112
     *
113
     * @param string[] $paths
114
     * @param array    $params
115
     *
116
     * @return GuzzleHttp\Psr7\Response[]
117
     */
118 View Code Duplication
    public function getMany($paths, array $params = [])
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        $promises = [];
121
        foreach ($paths as $path) {
122
            $promises[$path] = $this->client->getAsync('/' . $path, $params);
123
        }
124
        return Promise\unwrap($promises);
125
    }
126
127
    /**
128
     * Get headers of many paths asyncronously
129
     *
130
     * @param string[] $paths
131
     * @param array    $params
132
     *
133
     * @return GuzzleHttp\Psr7\Response[]
134
     */
135 View Code Duplication
    public function headMany($paths, array $params = [])
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
136
    {
137
        $promises = [];
138
        foreach ($paths as $path) {
139
            $promises[$path] = $this->client->headAsync('/' . $path, $params);
140
        }
141
        return Promise\unwrap($promises);
142
    }
143
144
    /**
145
     * Post to a path
146
     *
147
     * @param string $path
148
     * @param array  $params
149
     *
150
     * @return GuzzleHttp\Psr7\Response
151
     */
152
    public function post($path, array $params = [])
153
    {
154
        return $this->client->post('/' . $path, $params);
155
    }
156
157
    /**
158
     * Manipulate header data into a parsable format
159
     *
160
     * @param string $rawData
161
     *
162
     * @return array
163
     */
164
    public function parseHeader($rawData)
165
    {
166
        $data = [];
167
        foreach (explode("\n", trim($rawData)) as $line) {
168
            $bits = explode(': ', $line);
169
            if (count($bits) > 1) {
170
                $key = $bits[0];
171
                unset($bits[0]);
172
                $data[$key] = trim(implode(': ', $bits));
173
            }
174
        }
175
        return $data;
176
    }
177
178
    /**
179
     * Parse out the count from the response
180
     *
181
     * @param string  $response
182
     * @param string  $pattern
183
     * @param boolean $returnAll
184
     *
185
     * @return string|array|boolean
186
     */
187
    public function findMatchInResponse($response, $pattern, $returnAll = false)
188
    {
189
        if (empty($response)) {
190
            return false;
191
        }
192
        if (preg_match($pattern, $response, $match)
193
            && (isset($match[1]) || $returnAll)
194
        ) {
195
            return $returnAll ? $match : $match[1];
196
        }
197
        return false;
198
    }
199
}
200