1 | <?php |
||
24 | final class HttpClientCall implements Call |
||
25 | { |
||
26 | /** |
||
27 | * A retrofit http client implementation |
||
28 | * |
||
29 | * @var HttpClient |
||
30 | */ |
||
31 | private $client; |
||
32 | |||
33 | /** |
||
34 | * A web service resource as a method model |
||
35 | * |
||
36 | * @var ServiceMethod |
||
37 | */ |
||
38 | private $serviceMethod; |
||
39 | |||
40 | /** |
||
41 | * The runtime arguments that a request should be constructed with |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $args; |
||
46 | |||
47 | /** |
||
48 | * The constructed request |
||
49 | * |
||
50 | * @var RequestInterface |
||
51 | */ |
||
52 | private $request; |
||
53 | |||
54 | /** |
||
55 | * Constructor |
||
56 | * |
||
57 | * @param HttpClient $client |
||
58 | * @param ServiceMethod $serviceMethod |
||
59 | * @param array $args |
||
60 | */ |
||
61 | 25 | public function __construct(HttpClient $client, ServiceMethod $serviceMethod, array $args) |
|
67 | |||
68 | /** |
||
69 | * Execute request synchronously |
||
70 | * |
||
71 | * A [@see Response] will be returned |
||
72 | * |
||
73 | * @return Response |
||
74 | */ |
||
75 | 18 | public function execute(): Response |
|
81 | |||
82 | /** |
||
83 | * Execute request asynchronously |
||
84 | * |
||
85 | * This method accepts two optional callbacks. |
||
86 | * |
||
87 | * onResponse() will be called for any request that gets a response, |
||
88 | * whether it was successful or not. It will send a [@see Response] as |
||
89 | * the parameter. |
||
90 | * |
||
91 | * onFailure() will be called in the event a network request failed. It |
||
92 | * will send the [@see Throwable] that was encountered. |
||
93 | * |
||
94 | * Example of method signatures: |
||
95 | * |
||
96 | * $call->enqueue( |
||
97 | * function (\Tebru\Retrofit\Call $call, \Tebru\Retrofit\Response $response) {}, |
||
98 | * function (\Throwable $throwable) {} |
||
99 | * ); |
||
100 | * |
||
101 | * @param callable $onResponse On any response |
||
102 | * @param callable $onFailure On any network request failure |
||
103 | * @return Call |
||
104 | * @throws \LogicException |
||
105 | */ |
||
106 | 6 | public function enqueue(?callable $onResponse = null, ?callable $onFailure = null): Call |
|
107 | { |
||
108 | 6 | $this->client->sendAsync( |
|
109 | 6 | $this->request(), |
|
110 | function (ResponseInterface $response) use ($onResponse) { |
||
111 | 4 | if ($onResponse !== null) { |
|
112 | 4 | $onResponse($this->createResponse($response)); |
|
113 | } |
||
114 | 6 | }, |
|
115 | function (Throwable $throwable) use ($onFailure) { |
||
116 | 2 | if ($onFailure === null) { |
|
117 | 1 | throw $throwable; |
|
118 | } |
||
119 | |||
120 | 1 | $onFailure($throwable); |
|
121 | 6 | } |
|
122 | ); |
||
123 | |||
124 | 6 | return $this; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * When making requests asynchronously, call wait() to execute the requests |
||
129 | * |
||
130 | * @return void |
||
131 | */ |
||
132 | 6 | public function wait(): void |
|
136 | |||
137 | /** |
||
138 | * Get the PSR-7 request |
||
139 | * |
||
140 | * @return RequestInterface |
||
141 | */ |
||
142 | 24 | public function request(): RequestInterface |
|
150 | |||
151 | /** |
||
152 | * Create a [@see Response] from a PSR-7 response |
||
153 | * |
||
154 | * @param ResponseInterface $response |
||
155 | * @return RetrofitResponse |
||
156 | */ |
||
157 | 22 | private function createResponse(ResponseInterface $response): RetrofitResponse |
|
188 | } |
||
189 |