Completed
Push — master ( 0e12ee...cb89b4 )
by Nate
02:30
created

ReturnHandler::handle()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 12
nc 4
nop 0
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\Annotation\ResponseType;
10
use Tebru\Retrofit\Annotation\Returns;
11
use Tebru\Retrofit\Exception\RetrofitException;
12
13
/**
14
 * Class ReturnHandler
15
 *
16
 * @author Nate Brunette <[email protected]>
17
 */
18
class ReturnHandler extends Handler
19
{
20
    /** {@inheritdoc} */
21
    public function handle()
22
    {
23
        if (!$this->annotations->exists(Returns::NAME)) {
24
            return null;
25
        }
26
27
        /** @var Returns $returnAnnotation */
28
        $returnAnnotation = $this->annotations->get(Returns::NAME);
29
        $return = $returnAnnotation->getReturn();
30
        $this->methodBodyBuilder->setReturnType($return);
31
32
        if ('Response' === $return) {
33
            if (!$this->annotations->exists(ResponseType::NAME)) {
34
                throw new RetrofitException('When using a Response return type, an @ResponseType must also be set.');
35
            }
36
37
            /** @var ResponseType $responseAnnotation */
38
            $responseAnnotation = $this->annotations->get(ResponseType::NAME);
39
            $responseType = $responseAnnotation->getType();
40
41
            $this->methodBodyBuilder->setResponseType($responseType);
42
        }
43
    }
44
}
45