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\Exception\RetrofitException; |
10
|
|
|
use Tebru\Retrofit\Generation\Handler; |
11
|
|
|
use Tebru\Retrofit\Generation\HandlerContext; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ReturnHandler |
15
|
|
|
* |
16
|
|
|
* @author Nate Brunette <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ReturnHandler implements Handler |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Create request object and handle send/response |
22
|
|
|
* |
23
|
|
|
* @param HandlerContext $context |
24
|
|
|
* @return null |
25
|
|
|
* @throws RetrofitException |
26
|
|
|
*/ |
27
|
|
|
public function __invoke(HandlerContext $context) |
28
|
|
|
{ |
29
|
|
|
$callback = $context->annotations()->getCallback(); |
30
|
|
|
|
31
|
|
View Code Duplication |
if ($callback !== null) { |
|
|
|
|
32
|
|
|
if ($context->annotations()->isCallbackOptional()) { |
33
|
|
|
$context->body()->add('if (%s !== null) {', $callback); |
34
|
|
|
$context->body()->add('$returnEvent = new \Tebru\Retrofit\Event\ReturnEvent(null);'); |
35
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);'); |
36
|
|
|
$context->body()->add('return $returnEvent->getReturn();'); |
37
|
|
|
$context->body()->add('}'); |
38
|
|
|
} else { |
39
|
|
|
$context->body()->add('$returnEvent = new \Tebru\Retrofit\Event\ReturnEvent(null);'); |
40
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);'); |
41
|
|
|
$context->body()->add('return $returnEvent->getReturn();'); |
42
|
|
|
|
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$returnType = 'array'; |
48
|
|
|
if (null !== $context->annotations()->getReturnType()) { |
49
|
|
|
$returnType = $context->annotations()->getReturnType(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$responseReturn = (null !== $context->annotations()->getResponseType()); |
53
|
|
|
if ($responseReturn) { |
54
|
|
|
$returnType = $context->annotations()->getResponseType(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$deserializationContext = (null !== $context->annotations()->getDeserializationContext()) |
58
|
|
|
? $context->annotations()->getDeserializationContext() |
59
|
|
|
: []; |
60
|
|
|
|
61
|
|
|
if ('Response' === $returnType && false === $responseReturn) { |
62
|
|
|
throw new RetrofitException('A method return a Response must include a @ResponseType annotation.'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$context->body()->add( |
66
|
|
|
'$retrofitResponse = new \Tebru\Retrofit\Http\Response($response, "%s", $this->serializer, %s);', |
67
|
|
|
$returnType, |
68
|
|
|
$context->printer()->printArray($deserializationContext) |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
if ($responseReturn) { |
72
|
|
|
$context->body()->add('$return = $retrofitResponse;'); |
73
|
|
|
} else { |
74
|
|
|
$context->body()->add('$return = $retrofitResponse->body();'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$context->body()->add('$returnEvent = new \Tebru\Retrofit\Event\ReturnEvent($return);'); |
78
|
|
|
$context->body()->add('$this->eventDispatcher->dispatch("retrofit.return", $returnEvent);'); |
79
|
|
|
$context->body()->add('return $returnEvent->getReturn();'); } |
80
|
|
|
} |
81
|
|
|
|
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.