1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (c) 2015 Nate Brunette. |
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Tebru\Retrofit\Generation\Handler; |
8
|
|
|
|
9
|
|
|
use Tebru\Retrofit\Generation\Handler; |
10
|
|
|
use Tebru\Retrofit\Generation\HandlerContext; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ResponseHandler |
14
|
|
|
* |
15
|
|
|
* @author Nate Brunette <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class ResponseHandler implements Handler |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Create response |
21
|
|
|
* |
22
|
|
|
* @param HandlerContext $context |
23
|
|
|
* @return null |
24
|
|
|
*/ |
25
|
|
|
public function __invoke(HandlerContext $context) |
26
|
|
|
{ |
27
|
|
|
$callback = $context->annotations()->getCallback(); |
28
|
|
|
|
29
|
|
|
$context->body()->add('$request = new \GuzzleHttp\Psr7\Request("%s", $requestUrl, $headers, $body);', strtoupper($context->annotations()->getRequestMethod())); |
30
|
|
|
$context->body()->add('$beforeSendEvent = new \Tebru\Retrofit\Event\BeforeSendEvent($request);'); |
31
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.beforeSend", $beforeSendEvent);'); |
32
|
|
|
$context->body()->add('$request = $beforeSendEvent->getRequest();'); |
33
|
|
|
$context->body()->add('try {'); |
34
|
|
|
|
35
|
|
View Code Duplication |
if (null !== $callback) { |
|
|
|
|
36
|
|
|
if ($context->annotations()->isCallbackOptional()) { |
37
|
|
|
$context->body()->add('if (%s !== null) {', $callback); |
38
|
|
|
$context->body()->add('$response = $this->client->sendAsync($request, %s);', $callback); |
39
|
|
|
$context->body()->add('} else {'); |
40
|
|
|
$context->body()->add('$response = $this->client->send($request);'); |
41
|
|
|
$context->body()->add('}'); |
42
|
|
|
} else { |
43
|
|
|
$context->body()->add('$response = $this->client->sendAsync($request, %s);', $callback); |
44
|
|
|
} |
45
|
|
|
} else { |
46
|
|
|
$context->body()->add('$response = $this->client->send($request);'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$context->body()->add('} catch (\Exception $exception) {'); |
50
|
|
|
$context->body()->add('$apiExceptionEvent = new \Tebru\Retrofit\Event\ApiExceptionEvent($exception, $request);'); |
51
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.apiException", $apiExceptionEvent);'); |
52
|
|
|
$context->body()->add('$exception = $apiExceptionEvent->getException();'); |
53
|
|
|
$context->body()->add('throw new \Tebru\Retrofit\Exception\RetrofitApiException(get_class($this), $exception->getMessage(), $exception->getCode(), $exception);'); |
54
|
|
|
$context->body()->add('}'); |
55
|
|
|
$context->body()->add('$afterSendEvent = new \Tebru\Retrofit\Event\AfterSendEvent($request, $response);'); |
56
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.afterSend", $afterSendEvent);'); |
57
|
|
|
$context->body()->add('$response = $afterSendEvent->getResponse();'); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.