1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skobkin\Bundle\PointToolsBundle\Service; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\ClientInterface; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @todo Refactor DTO deserialization |
10
|
|
|
*/ |
11
|
|
|
class AbstractApi |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var ClientInterface HTTP-client from Guzzle |
15
|
|
|
*/ |
16
|
|
|
protected $client; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string Authentication token for API |
20
|
|
|
*/ |
21
|
|
|
protected $authToken; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string CSRF-token for API |
25
|
|
|
*/ |
26
|
|
|
protected $csRfToken; |
27
|
|
|
|
28
|
|
|
|
29
|
|
|
public function __construct(ClientInterface $httpClient) |
30
|
|
|
{ |
31
|
|
|
$this->client = $httpClient; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $path Request path |
36
|
|
|
* @param array $parameters Key => Value array of query parameters |
37
|
|
|
* |
38
|
|
|
* @return ResponseInterface |
39
|
|
|
*/ |
40
|
|
|
public function sendGetRequest(string $path, array $parameters = []): ResponseInterface |
41
|
|
|
{ |
42
|
12 |
|
return $this->client->request('GET', $path, ['query' => $parameters]); |
43
|
|
|
} |
44
|
12 |
|
|
45
|
12 |
|
/** |
46
|
|
|
* @param string $path Request path |
47
|
12 |
|
* @param array $parameters Key => Value array of request data |
48
|
12 |
|
* |
49
|
|
|
* @return ResponseInterface |
50
|
12 |
|
*/ |
51
|
|
|
public function sendPostRequest(string $path, array $parameters = []): ResponseInterface |
52
|
|
|
{ |
53
|
|
|
return $request = $this->client->request('POST', $path, ['form_params' => $parameters]); |
|
|
|
|
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Make GET request and return data from response |
58
|
|
|
* |
59
|
|
|
* @param string $path Path template |
60
|
|
|
* @param array $parameters Parameters array used to fill path template |
61
|
|
|
* @param bool $decodeJsonResponse Decode JSON or return plaintext |
62
|
|
|
* @param bool $decodeJsonToObjects Decode JSON objects to PHP objects instead of arrays |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
|
|
public function getGetRequestData($path, array $parameters = [], bool $decodeJsonResponse = false, bool $decodeJsonToObjects = false) |
67
|
|
|
{ |
68
|
|
|
$response = $this->sendGetRequest($path, $parameters); |
69
|
|
|
|
70
|
|
|
return $this->processResponse($response, $decodeJsonResponse, $decodeJsonToObjects); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Make POST request and return data from response |
75
|
|
|
* |
76
|
|
|
* @param string $path Path template |
77
|
|
|
* @param array $parameters Parameters array used to fill path template |
78
|
|
|
* @param bool $decodeJson Decode JSON or return plaintext |
79
|
|
|
* @param bool $decodeToObjects Decode JSON objects to PHP objects instead of arrays |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function getPostRequestData($path, array $parameters = [], bool $decodeJson = false, bool $decodeToObjects = false) |
84
|
|
|
{ |
85
|
|
|
$response = $this->sendPostRequest($path, $parameters); |
86
|
|
|
|
87
|
|
|
return $this->processResponse($response, $decodeJson, $decodeToObjects); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get HTTP client base URL |
92
|
|
|
* |
93
|
|
|
* @return string Base URL of client |
94
|
|
|
*/ |
95
|
|
|
public function getBaseUrl(): string |
96
|
|
|
{ |
97
|
|
|
return (string) $this->client->getConfig('base_uri'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param ResponseInterface $response |
102
|
|
|
* @param bool $decodeJson |
103
|
|
|
* @param bool $decodeToObjects |
104
|
|
|
* |
105
|
|
|
* @return string|array|object |
106
|
|
|
*/ |
107
|
|
|
private function processResponse(ResponseInterface $response, bool $decodeJson = false, bool $decodeToObjects = false) |
108
|
|
|
{ |
109
|
|
|
if ($decodeJson) { |
110
|
|
|
if ($decodeToObjects) { |
111
|
|
|
return json_decode($response->getBody()); |
112
|
|
|
} else { |
113
|
|
|
return json_decode($response->getBody(), true); |
114
|
|
|
} |
115
|
|
|
} else { |
116
|
|
|
return $response->getBody(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.