Completed
Push — master ( 53898c...f3a8c7 )
by Nate
02:40
created

ResponseHandler::__invoke()   D

Complexity

Conditions 10
Paths 24

Size

Total Lines 45
Code Lines 32

Duplication

Lines 13
Ratio 28.89 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 13
loc 45
rs 4.8196
cc 10
eloc 32
nc 24
nop 1

How to fix   Complexity   

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) 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