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