1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Tebru\Retrofit\Generation\Handler; |
8
|
|
|
|
9
|
|
|
use LogicException; |
10
|
|
|
use Tebru\Retrofit\Exception\RetrofitException; |
11
|
|
|
use Tebru\Retrofit\Generation\Handler; |
12
|
|
|
use Tebru\Retrofit\Generation\HandlerContext; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ResponseHandler |
16
|
|
|
* |
17
|
|
|
* @author Nate Brunette <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class ResponseHandler implements Handler |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Create response |
23
|
|
|
* |
24
|
|
|
* @param HandlerContext $context |
25
|
|
|
* @return null |
26
|
|
|
* @throws LogicException |
27
|
|
|
* @throws RetrofitException |
28
|
|
|
*/ |
29
|
|
|
public function __invoke(HandlerContext $context) |
30
|
|
|
{ |
31
|
|
|
$callback = $context->annotations()->getCallback(); |
32
|
|
|
|
33
|
|
|
$context->body()->add('$request = new \GuzzleHttp\Psr7\Request("%s", $requestUrl, $headers, $body);', strtoupper($context->annotations()->getRequestMethod())); |
34
|
|
|
$context->body()->add('$beforeSendEvent = new \Tebru\Retrofit\Event\BeforeSendEvent($request);'); |
35
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.beforeSend", $beforeSendEvent);'); |
36
|
|
|
$context->body()->add('$request = $beforeSendEvent->getRequest();'); |
37
|
|
|
$context->body()->add('try {'); |
38
|
|
|
|
39
|
|
View Code Duplication |
if (null !== $callback) { |
40
|
|
|
if ($context->annotations()->isCallbackOptional()) { |
41
|
|
|
$context->body()->add('if (%s !== null) {', $callback); |
42
|
|
|
$context->body()->add('$response = $this->client->sendAsync($request, %s);', $callback); |
43
|
|
|
$context->body()->add('} else {'); |
44
|
|
|
$context->body()->add('$response = $this->client->send($request);'); |
45
|
|
|
$context->body()->add('}'); |
46
|
|
|
} else { |
47
|
|
|
$context->body()->add('$response = $this->client->sendAsync($request, %s);', $callback); |
48
|
|
|
} |
49
|
|
|
} else { |
50
|
|
|
$context->body()->add('$response = $this->client->send($request);'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$context->body()->add('} catch (\Exception $exception) {'); |
54
|
|
|
$context->body()->add('$apiExceptionEvent = new \Tebru\Retrofit\Event\ApiExceptionEvent($exception, $request);'); |
55
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.apiException", $apiExceptionEvent);'); |
56
|
|
|
$context->body()->add('$exception = $apiExceptionEvent->getException();'); |
57
|
|
|
$context->body()->add('throw new \Tebru\Retrofit\Exception\RetrofitApiException(get_class($this), $exception->getMessage(), $exception->getCode(), $exception);'); |
58
|
|
|
$context->body()->add('}'); |
59
|
|
|
|
60
|
|
|
if (null !== $callback && $context->annotations()->isCallbackOptional()) { |
61
|
|
|
$context->body()->add('if (%s !== null) {', $callback); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if (null === $callback || (null !== $callback && $context->annotations()->isCallbackOptional())) { |
65
|
|
|
$context->body()->add('$afterSendEvent = new \Tebru\Retrofit\Event\AfterSendEvent($request, $response);'); |
66
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.afterSend", $afterSendEvent);'); |
67
|
|
|
$context->body()->add('$response = $afterSendEvent->getResponse();'); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if (null !== $callback && $context->annotations()->isCallbackOptional()) { |
71
|
|
|
$context->body()->add('}'); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|