1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Tmdb package. |
4
|
|
|
* |
5
|
|
|
* (c) Vincent Faliès <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author Vincent Faliès <[email protected]> |
11
|
|
|
* @copyright Copyright (c) 2017 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
namespace VfacTmdb\lib\Guzzle; |
16
|
|
|
|
17
|
|
|
use VfacTmdb\Interfaces\HttpRequestInterface; |
18
|
|
|
use GuzzleHttp\Exception\RequestException; |
19
|
|
|
use VfacTmdb\Exceptions\NotFoundException; |
20
|
|
|
use VfacTmdb\Exceptions\ServerErrorException; |
21
|
|
|
use VfacTmdb\Exceptions\HttpErrorException; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* HTTP Client class for all HTTP request |
25
|
|
|
* @package Tmdb |
26
|
|
|
* @author Vincent Faliès <[email protected]> |
27
|
|
|
* @copyright Copyright (c) 2017 |
28
|
|
|
*/ |
29
|
|
|
class Client implements HttpRequestInterface |
30
|
|
|
{ |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Client variable |
34
|
|
|
* @var \GuzzleHttp\ClientInterface |
35
|
|
|
*/ |
36
|
|
|
protected $guzzleClient; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor |
40
|
|
|
* @param \GuzzleHttp\ClientInterface $guzzleClient |
41
|
|
|
*/ |
42
|
353 |
|
public function __construct(\GuzzleHttp\ClientInterface $guzzleClient) |
43
|
|
|
{ |
44
|
353 |
|
$this->guzzleClient = $guzzleClient; |
45
|
353 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Send response method from specific http method |
49
|
|
|
* @param string $method Http method (GET, POST, DELETE) |
50
|
|
|
* @param string $url |
51
|
|
|
* @param array $options |
52
|
|
|
* @param array $form_params |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
12 |
|
private function sendResponse(string $method, string $url, array $options = [], array $form_params = array()) |
56
|
|
|
{ |
57
|
|
|
try { |
58
|
12 |
|
$params = array_merge($options, [ |
59
|
12 |
|
'headers' => ['Content-Type' => 'application/json;charset=utf-8'], |
60
|
12 |
|
'body' => json_encode($form_params) |
61
|
|
|
]); |
62
|
|
|
|
63
|
12 |
|
return $this->guzzleClient->request($method, $url, $params); |
64
|
9 |
|
} catch (RequestException $e) { |
65
|
9 |
|
if (is_null($e->getResponse())) { |
66
|
3 |
|
throw new HttpErrorException; |
67
|
|
|
} |
68
|
6 |
|
switch ((int) $e->getResponse()->getStatusCode()) { |
69
|
6 |
|
case 404: |
70
|
3 |
|
throw new NotFoundException($e->getMessage()); |
71
|
|
|
default: |
72
|
3 |
|
throw new ServerErrorException($e->getMessage()); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get response method |
79
|
|
|
* @param string $url |
80
|
|
|
* @return mixed |
81
|
|
|
*/ |
82
|
4 |
|
public function getResponse(string $url) |
83
|
|
|
{ |
84
|
4 |
|
return $this->sendResponse('GET', $url); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Post response method |
89
|
|
|
* @param string $url |
90
|
|
|
* @param array $options |
91
|
|
|
* @param array $form_params |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
4 |
|
public function postResponse(string $url, array $options = [], array $form_params = array()) |
95
|
|
|
{ |
96
|
4 |
|
return $this->sendResponse('POST', $url, $options, $form_params); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Delete response method |
101
|
|
|
* @param string $url |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
4 |
|
public function deleteResponse(string $url) |
105
|
|
|
{ |
106
|
4 |
|
return $this->sendResponse('DELETE', $url); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|