1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* AnimeDb package. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
8
|
|
|
*/ |
9
|
|
|
namespace AnimeDb\Bundle\AniDbBrowserBundle\Service\Client; |
10
|
|
|
|
11
|
|
|
use AnimeDb\Bundle\AniDbBrowserBundle\Util\ResponseRepair; |
12
|
|
|
use Guzzle\Http\Client; |
13
|
|
|
|
14
|
|
|
class GuzzleClient implements ClientInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Client |
18
|
|
|
*/ |
19
|
|
|
protected $client; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var ResponseRepair |
23
|
|
|
*/ |
24
|
|
|
protected $repair; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $api_prefix; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $app_code; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $request_params = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param Client $client |
43
|
|
|
* @param ResponseRepair $repair |
44
|
|
|
* @param string $api_prefix |
45
|
|
|
* @param string $api_client |
46
|
|
|
* @param string $api_clientver |
47
|
|
|
* @param string $api_protover |
48
|
|
|
* @param string $app_code |
49
|
|
|
*/ |
50
|
50 |
|
public function __construct( |
51
|
|
|
Client $client, |
52
|
|
|
ResponseRepair $repair, |
53
|
|
|
$api_prefix, |
54
|
|
|
$api_client, |
55
|
|
|
$api_clientver, |
56
|
|
|
$api_protover, |
57
|
|
|
$app_code |
58
|
|
|
) { |
59
|
50 |
|
$this->client = $client; |
60
|
50 |
|
$this->app_code = $app_code; |
61
|
50 |
|
$this->repair = $repair; |
62
|
|
|
|
63
|
50 |
|
$query = []; |
64
|
50 |
|
if ($api_prefix) { |
65
|
36 |
|
parse_str(parse_url($api_prefix, PHP_URL_QUERY), $query); |
66
|
36 |
|
$this->api_prefix = parse_url($api_prefix, PHP_URL_PATH); |
|
|
|
|
67
|
36 |
|
} |
68
|
50 |
|
$this->request_params = [ |
69
|
50 |
|
'client' => $api_client, |
70
|
50 |
|
'clientver' => $api_clientver, |
71
|
50 |
|
'protover' => $api_protover, |
72
|
50 |
|
] + $query; |
73
|
50 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param int $timeout |
77
|
|
|
* |
78
|
|
|
* @return GuzzleClient |
79
|
|
|
*/ |
80
|
1 |
|
public function setTimeout($timeout) |
81
|
|
|
{ |
82
|
1 |
|
$this->client->setDefaultOption('timeout', $timeout); |
83
|
|
|
|
84
|
1 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $proxy |
89
|
|
|
* |
90
|
|
|
* @return GuzzleClient |
91
|
|
|
*/ |
92
|
1 |
|
public function setProxy($proxy) |
93
|
|
|
{ |
94
|
1 |
|
$this->client->setDefaultOption('proxy', $proxy); |
95
|
|
|
|
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $request |
101
|
|
|
* @param array $params |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
48 |
|
public function get($request, array $params = []) |
106
|
|
|
{ |
107
|
48 |
|
$request = $this->client->get( |
108
|
48 |
|
$this->api_prefix, |
109
|
48 |
|
null, |
110
|
|
|
[ |
111
|
48 |
|
'query' => $this->request_params + ['request' => $request] + $params, |
112
|
48 |
|
'headers' => $this->app_code ? ['User-Agent' => $this->app_code] : [], |
113
|
|
|
] |
114
|
48 |
|
); |
115
|
|
|
|
116
|
48 |
|
$response = $request->send(); |
117
|
|
|
|
118
|
48 |
|
if ($response->isError()) { |
119
|
24 |
|
throw new \RuntimeException(sprintf( |
120
|
24 |
|
'Failed execute request "%s" to the server "%s"', |
121
|
24 |
|
$request, |
122
|
24 |
|
$this->client->getBaseUrl() |
123
|
24 |
|
)); |
124
|
|
|
} |
125
|
|
|
|
126
|
24 |
|
return $this->repair->repair(gzdecode($response->getBody(true))); // repair |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
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 theid
property of an instance of theAccount
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.