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

ResponseHandler::__invoke()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 34
Code Lines 27

Duplication

Lines 13
Ratio 38.24 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 34
rs 8.8571
cc 3
eloc 27
nc 3
nop 1
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 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
        $context->body()->add('$afterSendEvent = new \Tebru\Retrofit\Event\AfterSendEvent($request, $response);');
60
        $context->body()->add('$this->eventDispatcher->dispatch("retrofit.afterSend", $afterSendEvent);');
61
        $context->body()->add('$response = $afterSendEvent->getResponse();');
62
    }
63
}
64