1 | <?php |
||
21 | class HttpGuzzlClient implements HttpClientInterface |
||
22 | { |
||
23 | const DEFAULT_CONNECTION_TIMEOUT = 5; |
||
24 | const DEFAULT_TIMEOUT = 30; |
||
25 | |||
26 | /** |
||
27 | * @var ClientInterface |
||
28 | */ |
||
29 | private $oGuzzelClient; |
||
30 | |||
31 | /** |
||
32 | * @var AuthEntity |
||
33 | */ |
||
34 | private $oAuthEntity; |
||
35 | |||
36 | /** |
||
37 | * @param ClientInterface $oGuzzelClient |
||
38 | * @param AuthEntity $oAuthEntity |
||
39 | */ |
||
40 | 7 | public function __construct( |
|
48 | |||
49 | /** |
||
50 | * @param string $sUrl |
||
51 | * @return HttpClientResponseInterface |
||
52 | * @throws HttpConnectionException |
||
53 | */ |
||
54 | 3 | public function get($sUrl) |
|
55 | { |
||
56 | 3 | $_aRequestOptions = $this->getDefaultRequestOptions(); |
|
57 | |||
58 | try |
||
59 | { |
||
60 | 3 | $_oResponse = $this->oGuzzelClient->request('GET', $sUrl, $_aRequestOptions); |
|
61 | 2 | return new HttpGuzzlResponse($_oResponse); |
|
62 | } |
||
63 | 1 | catch (ClientException $oException) // 400 level errors |
|
64 | { |
||
65 | throw new HttpConnectionException( |
||
66 | sprintf('Client error: Calling %s returned %d', $this->oGuzzelClient->getConfig('base_uri') . $sUrl, $oException->getCode()), |
||
67 | $oException->getCode(), |
||
68 | $oException |
||
69 | ); |
||
70 | } |
||
71 | 1 | catch (ServerException $oException) // 500 level errors |
|
72 | { |
||
73 | throw new HttpConnectionException( |
||
74 | sprintf('Server error: Calling %s returned %d', $this->oGuzzelClient->getConfig('base_uri') . $sUrl, $oException->getCode()), |
||
75 | $oException->getCode(), |
||
76 | $oException |
||
77 | ); |
||
78 | } |
||
79 | 1 | catch (TooManyRedirectsException $oException) // too many redirects to follow |
|
80 | { |
||
81 | throw new HttpConnectionException( |
||
82 | sprintf('Request to %s failed due to too many redirects', $this->oGuzzelClient->getConfig('base_uri') . $sUrl), |
||
83 | HttpConnectionException::ERROR_CODE_TOO_MANY_REDIRECT_EXCEPTION, |
||
84 | $oException |
||
85 | ); |
||
86 | } |
||
87 | 1 | catch (ConnectException $oException) // networking error |
|
88 | { |
||
89 | throw new HttpConnectionException( |
||
90 | sprintf('Cannot connect to %s due to some networking error', $this->oGuzzelClient->getConfig('base_uri') . $sUrl), |
||
91 | HttpConnectionException::ERROR_CODE_CONNECT_EXCEPTION, |
||
92 | $oException |
||
93 | ); |
||
94 | } |
||
95 | 1 | catch (RequestException $oException) // networking error (connection timeout, DNS errors, etc.) |
|
96 | { |
||
97 | throw new HttpConnectionException( |
||
98 | sprintf('Cannot connect to %s due to networking error', $this->oGuzzelClient->getConfig('base_uri') . $sUrl), |
||
99 | HttpConnectionException::ERROR_CODE_REQUEST_EXCEPTION, |
||
100 | $oException |
||
101 | ); |
||
102 | } |
||
103 | 1 | catch (\Exception $oException) |
|
104 | { |
||
105 | 1 | throw new HttpConnectionException( |
|
106 | 1 | sprintf('Can\'t get response from "%s"', $this->oGuzzelClient->getConfig('base_uri') . $sUrl), |
|
107 | 1 | HttpConnectionException::ERROR_CODE_UNKNOWN, |
|
108 | $oException |
||
109 | ); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * @param string $sUrl |
||
115 | * @param mixed $mPostData |
||
116 | * @return HttpGuzzlResponse |
||
117 | */ |
||
118 | 2 | public function postJsonData($sUrl, $mPostData) |
|
122 | |||
123 | /** |
||
124 | * @param string $sUrl |
||
125 | * @param mixed $mPutData |
||
126 | * @return HttpGuzzlResponse |
||
127 | */ |
||
128 | public function putJsonData($sUrl, $mPutData) |
||
132 | |||
133 | |||
134 | /** |
||
135 | * @param string $sUrl |
||
136 | * @return HttpGuzzlResponse |
||
137 | */ |
||
138 | 2 | public function delete($sUrl) |
|
144 | |||
145 | /** |
||
146 | * @param $sMethod |
||
147 | * @param $sUrl |
||
148 | * @param $mData |
||
149 | * @return HttpGuzzlResponse |
||
150 | */ |
||
151 | 2 | private function sendJsonDataWithMethod($sMethod, $sUrl, $mData) |
|
160 | |||
161 | /** |
||
162 | * Returns default options for the HTTP request. |
||
163 | * If an username and password is provided, auth |
||
164 | * header will be applied as well. |
||
165 | * |
||
166 | * @return array<string,integer|string> |
||
|
|||
167 | */ |
||
168 | 7 | private function getDefaultRequestOptions() |
|
187 | |||
188 | } |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.