| Total Complexity | 4 |
| Total Lines | 56 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 13 | class JSONFetcher implements JSONGetter, JSONPoster |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * @var \GuzzleHttp\Client |
||
| 17 | */ |
||
| 18 | private $client; |
||
| 19 | /** |
||
| 20 | * JSONFetcher constructor. |
||
| 21 | * |
||
| 22 | * @param \GuzzleHttp\Client $client |
||
|
1 ignored issue
–
show
|
|||
| 23 | */ |
||
| 24 | public function __construct(Client $client) |
||
| 25 | { |
||
| 26 | $this->client = $client; |
||
| 27 | } |
||
| 28 | /** |
||
|
3 ignored issues
–
show
|
|||
| 29 | * {@inheritdoc} |
||
| 30 | */ |
||
|
1 ignored issue
–
show
|
|||
| 31 | public function get(string $url, array $params = [], array $options = []) |
||
| 32 | { |
||
| 33 | $reqOpts = array_merge([ |
||
| 34 | 'query' => $params, |
||
| 35 | 'headers' => [ |
||
| 36 | 'Accept' => 'application/json', |
||
| 37 | ], |
||
| 38 | ], $options); |
||
| 39 | return $this->request("GET", $url, $reqOpts); |
||
| 40 | } |
||
| 41 | /** |
||
| 42 | * @param string $method |
||
|
1 ignored issue
–
show
|
|||
| 43 | * @param string $url |
||
|
1 ignored issue
–
show
|
|||
| 44 | * @param array $options |
||
|
1 ignored issue
–
show
|
|||
| 45 | * |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | protected function request(string $method, string $url, array $options) |
||
| 49 | { |
||
| 50 | $response = $this->client->request($method, $url, $options); |
||
| 51 | // TODO: Handle request errors (ex.: authorization error with 403 status code) |
||
| 52 | $data = $response->getBody()->getContents(); |
||
| 53 | return json_decode($data, true); |
||
| 54 | } |
||
| 55 | /** |
||
|
4 ignored issues
–
show
|
|||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
|
1 ignored issue
–
show
|
|||
| 58 | public function post(string $url, array $params = [], $body = null, array $options = []) |
||
| 69 | } |
||
| 70 | } |
||
| 71 |