Completed
Branch develop (972ca0)
by Nate
02:45
created

ReturnHandler::__invoke()   C

Complexity

Conditions 9
Paths 49

Size

Total Lines 53
Code Lines 35

Duplication

Lines 15
Ratio 28.3 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 15
loc 53
rs 6.8963
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 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) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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