1 | <?php |
||
2 | |||
3 | namespace Pagantis\OrdersApiClient\Method; |
||
4 | |||
5 | use Httpful\Mime; |
||
6 | use Httpful\Request; |
||
7 | use Httpful\Response; |
||
8 | use Pagantis\OrdersApiClient\Exception\HttpException; |
||
9 | use Pagantis\OrdersApiClient\Model\ApiConfiguration; |
||
10 | |||
11 | /** |
||
12 | * Class AbstractMethod |
||
13 | * |
||
14 | * @package Pagantis\OrdersApiClient\Method |
||
15 | */ |
||
16 | abstract class AbstractMethod implements MethodInterface |
||
17 | { |
||
18 | const SLASH = '/'; |
||
19 | |||
20 | /** |
||
21 | * @var ApiConfiguration $apiConfiguration |
||
22 | */ |
||
23 | protected $apiConfiguration; |
||
24 | |||
25 | /** |
||
26 | * @var Response |
||
27 | */ |
||
28 | protected $response; |
||
29 | |||
30 | /** |
||
31 | * @var Request |
||
32 | */ |
||
33 | protected $request; |
||
34 | |||
35 | /** |
||
36 | * AbstractMethod constructor. |
||
37 | * |
||
38 | * @param ApiConfiguration $apiConfiguration |
||
39 | */ |
||
40 | public function __construct(ApiConfiguration $apiConfiguration) |
||
41 | { |
||
42 | $this->apiConfiguration = $apiConfiguration; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * @return bool|Response |
||
47 | */ |
||
48 | public function getResponse() |
||
49 | { |
||
50 | if ($this->response instanceof Response) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
51 | return $this->response; |
||
52 | } |
||
53 | |||
54 | return false; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * @return bool|string |
||
59 | */ |
||
60 | public function getResponseAsJson() |
||
61 | { |
||
62 | $response = $this->getResponse(); |
||
63 | if ($response instanceof Response) { |
||
0 ignored issues
–
show
|
|||
64 | return $response->raw_body; |
||
65 | } |
||
66 | |||
67 | return false; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @param $array |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | protected function addGetParameters($array) |
||
76 | { |
||
77 | $query = ''; |
||
78 | if (is_array($array)) { |
||
79 | $query = http_build_query(array_filter($array)); |
||
80 | } |
||
81 | |||
82 | return empty($query) ? '' : '?' . $query; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * @param $code |
||
87 | * @param null $message |
||
0 ignored issues
–
show
|
|||
88 | * |
||
89 | * @throws HttpException |
||
90 | */ |
||
91 | protected function parseHttpException($code, $message = null) |
||
92 | { |
||
93 | if (!in_array($code, array(HttpException::HTTP_UNPROCESSABLE_ENTITY, HttpException::HTTP_CONFLICT))) { |
||
94 | $message = null; |
||
95 | } |
||
96 | |||
97 | $objHttpException = new HttpException($code, $message); |
||
98 | $status = $objHttpException->getStatus(); |
||
99 | |||
100 | if (!array_key_exists($code, $status)) { |
||
101 | throw new HttpException(HttpException::HTTP_INTERNAL_SERVER_ERROR); |
||
102 | } |
||
103 | |||
104 | throw $objHttpException; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @return Request |
||
109 | */ |
||
110 | protected function getRequest() |
||
111 | { |
||
112 | return Request::init() |
||
113 | ->expects(Mime::JSON) |
||
114 | ->authenticateWithBasic($this->apiConfiguration->getPublicKey(), $this->apiConfiguration->getPrivateKey()) |
||
115 | ->timeoutIn(30) |
||
116 | ->addHeaders($this->apiConfiguration->getHeaders()) |
||
117 | ; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param Response $response |
||
122 | * |
||
123 | * @return $this |
||
124 | * @throws HttpException |
||
125 | */ |
||
126 | protected function setResponse(Response $response) |
||
127 | { |
||
128 | if (!$response->hasErrors()) { |
||
129 | $this->response = $response; |
||
130 | return $this; |
||
131 | } |
||
132 | |||
133 | return $this->parseHttpException($response->code, $response->raw_body); |
||
134 | } |
||
135 | } |
||
136 |