Completed
Push — master ( 2ac042...4e5e3b )
by Nate
02:40
created

ReturnHandler::__invoke()   B

Complexity

Conditions 9
Paths 49

Size

Total Lines 54
Code Lines 35

Duplication

Lines 15
Ratio 27.78 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 15
loc 54
rs 7.255
cc 9
eloc 35
nc 49
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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