|
1
|
|
|
<?php namespace GameScan\Core\Request\Api\Http; |
|
2
|
|
|
|
|
3
|
|
|
use GameScan\Core\Request\Api\ApiRequestInterface; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Client |
|
7
|
|
|
* @package GameScan\Core\Request\Api\Http |
|
8
|
|
|
*/ |
|
9
|
|
|
class Client implements ApiRequestInterface |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
protected $guzzle; |
|
13
|
|
|
protected $headers = array(); |
|
14
|
|
|
protected $parameters = array(); |
|
15
|
|
|
protected $curlConfig = array(); |
|
16
|
|
|
|
|
17
|
14 |
|
public function __construct() |
|
18
|
|
|
{ |
|
19
|
14 |
|
$this->guzzle = new \GuzzleHttp\Client(); |
|
20
|
14 |
|
$this->clean(); |
|
21
|
14 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* {@inheritdoc} |
|
25
|
|
|
*/ |
|
26
|
14 |
|
public function clean() |
|
27
|
|
|
{ |
|
28
|
14 |
|
$this->resetHeaders(); |
|
29
|
14 |
|
$this->resetParameters(); |
|
30
|
14 |
|
} |
|
31
|
|
|
|
|
32
|
14 |
|
private function resetHeaders() |
|
33
|
|
|
{ |
|
34
|
14 |
|
$this->headers = array(); |
|
35
|
14 |
|
} |
|
36
|
|
|
|
|
37
|
14 |
|
private function resetParameters() |
|
38
|
|
|
{ |
|
39
|
14 |
|
$this->parameters = array(); |
|
40
|
14 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Http Request Timeout |
|
44
|
|
|
* @param int $timeout |
|
45
|
|
|
* @throws \Exception |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setTimeout($timeout) |
|
48
|
|
|
{ |
|
49
|
|
|
if (!is_int($timeout)) { |
|
50
|
|
|
throw new \Exception("Timeout must be an integer value"); |
|
51
|
|
|
} |
|
52
|
|
|
$this->curlConfig[CURLOPT_TIMEOUT] = $timeout; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
14 |
|
public function setHeaders(array $headers) |
|
59
|
|
|
{ |
|
60
|
14 |
|
foreach ($headers as $headerKey => $headerValue) { |
|
61
|
4 |
|
$this->headers[$headerKey] = $headerValue; |
|
62
|
14 |
|
} |
|
63
|
14 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
14 |
|
public function setParameters(array $parameters) |
|
69
|
|
|
{ |
|
70
|
14 |
|
foreach ($parameters as $parameterKey => $parameterValue) { |
|
71
|
4 |
|
$this->parameters[$parameterKey] = $parameterValue; |
|
72
|
14 |
|
} |
|
73
|
14 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
*/ |
|
78
|
14 |
|
public function get($ressourceToGrab, array $parameters = null) |
|
79
|
|
|
{ |
|
80
|
14 |
|
$config = $this->getConfig(); |
|
81
|
14 |
|
if ($parameters !== null) { |
|
82
|
|
|
foreach ($parameters as $parameterKey => $parameterValue) { |
|
83
|
|
|
$config["query"][$parameterKey] = $parameterValue; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
14 |
|
$request = $this->guzzle->createRequest("GET", $ressourceToGrab, $config); |
|
87
|
14 |
|
$response = $this->guzzle->send($request); |
|
88
|
10 |
|
$this->checkHttpStatus($response); |
|
89
|
10 |
|
return (string) $response->getBody(); |
|
90
|
2 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get http request config for guzzle |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
14 |
|
protected function getConfig() |
|
97
|
|
|
{ |
|
98
|
14 |
|
$config = array(); |
|
99
|
14 |
|
$config['headers'] = $this->headers; |
|
100
|
14 |
|
$config['config'] = array( |
|
101
|
14 |
|
'curl' => $this->curlConfig |
|
102
|
14 |
|
); |
|
103
|
14 |
|
$config['query'] = $this->parameters; |
|
104
|
14 |
|
$config['cookies'] = true; |
|
105
|
14 |
|
return $config; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
10 |
|
protected function checkHttpStatus($response) |
|
112
|
|
|
{ |
|
113
|
10 |
|
$statusCode = (int) $response->getStatusCode(); |
|
114
|
10 |
|
if ($statusCode < 200 || $statusCode > 299) { |
|
115
|
|
|
throw new ApiStatusCodeException("Not successful http status code", $statusCode); |
|
116
|
|
|
} |
|
117
|
10 |
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|