1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace WSW\SiftScience\Client; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use GuzzleHttp\Client as HttpClient; |
7
|
|
|
use GuzzleHttp\Exception\RequestException; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use WSW\SiftScience\Collections\Headers; |
10
|
|
|
use WSW\SiftScience\Exceptions\SiftScienceRequestException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Client |
14
|
|
|
* |
15
|
|
|
* @package WSW\SiftScience\Client |
16
|
|
|
* @author Ronaldo Matos Rodrigues <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class Client |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var \GuzzleHttp\Client |
22
|
|
|
*/ |
23
|
|
|
private $client; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Headers |
27
|
|
|
*/ |
28
|
|
|
private $headers; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param ClientInterface $client |
32
|
|
|
*/ |
33
|
|
|
public function __construct(ClientInterface $client = null) |
34
|
|
|
{ |
35
|
|
|
$this->client = $client ?: new HttpClient(); |
|
|
|
|
36
|
|
|
$this->headers = new Headers(); |
37
|
|
|
$this->addHeader('Content-Type', 'application/json'); |
38
|
|
|
$this->addHeader('Accept', 'application/json'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $key |
43
|
|
|
* @param string $value |
44
|
|
|
* |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function addHeader($key, $value) |
48
|
|
|
{ |
49
|
|
|
$this->headers->add([$key => $value]); |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $url |
56
|
|
|
* @param string $body |
57
|
|
|
* @throws SiftScienceRequestException |
58
|
|
|
* |
59
|
|
|
* @return ResponseInterface |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function post($url, $body) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
$response = $this->client->request('POST', $url, [ |
65
|
|
|
'headers' => $this->headers->getValues(), |
66
|
|
|
'body' => $body, |
67
|
|
|
'verify' => false |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
return $response; |
71
|
|
|
} catch (RequestException $e) { |
72
|
|
|
throw new SiftScienceRequestException($e); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $url |
78
|
|
|
* @throws SiftScienceRequestException |
79
|
|
|
* |
80
|
|
|
* @return ResponseInterface |
81
|
|
|
*/ |
82
|
|
View Code Duplication |
public function get($url) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
try { |
85
|
|
|
$response = $this->client->request('GET', $url, [ |
86
|
|
|
'headers' => $this->headers->getValues(), |
87
|
|
|
'verify' => false |
88
|
|
|
]); |
89
|
|
|
|
90
|
|
|
return $response; |
91
|
|
|
} catch (RequestException $e) { |
92
|
|
|
throw new SiftScienceRequestException($e); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
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 given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.