Passed
Push — master ( 0a6b37...c8b837 )
by Nate
03:46
created

HttpRequestAnnotHandler::handle()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 17
nc 4
nop 4
crap 4
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Retrofit\Internal\AnnotationHandler;
10
11
use InvalidArgumentException;
12
use Tebru\AnnotationReader\AbstractAnnotation;
13
use Tebru\Retrofit\Annotation\HttpRequest;
14
use Tebru\Retrofit\AnnotationHandler;
15
use Tebru\Retrofit\Converter;
16
use Tebru\Retrofit\ServiceMethodBuilder;
17
18
/**
19
 * Class HttpRequestAnnotHandler
20
 *
21
 * @author Nate Brunette <[email protected]>
22
 */
23
final class HttpRequestAnnotHandler implements AnnotationHandler
24
{
25
    /**
26
     * Sets the request method, uri, and whether or not the request contains a body
27
     *
28
     * @param HttpRequest|AbstractAnnotation $annotation The annotation to handle
29
     * @param ServiceMethodBuilder $serviceMethodBuilder Used to construct a [@see ServiceMethod]
30
     * @param Converter|null $converter Converter used to convert types before sending to service method
31
     * @param int|null $index The position of the parameter or null if annotation does not reference parameter
32
     * @return void
33
     * @throws \LogicException
34
     * @throws \InvalidArgumentException
35
     */
36 18
    public function handle(
37
        AbstractAnnotation $annotation,
38
        ServiceMethodBuilder $serviceMethodBuilder,
39
        ?Converter $converter,
40
        ?int $index
41
    ): void {
42 18
        if (!$annotation instanceof HttpRequest) {
43 1
            throw new InvalidArgumentException('Retrofit: Annotation must be an HttpRequest');
44
        }
45
46 17
        if ($converter !== null) {
47 1
            throw new InvalidArgumentException(sprintf(
48 1
                'Retrofit: Converter must be null, %s found',
49 1
                gettype($converter)
50
            ));
51
        }
52
53 16
        $uri = $annotation->getValue();
54
55 16
        $serviceMethodBuilder->setMethod($annotation->getType());
56 16
        $serviceMethodBuilder->setPath($uri);
57
58 16
        if (!$annotation->hasBody()) {
59 11
            $serviceMethodBuilder->setHasBody($annotation->hasBody());
60
        }
61 16
    }
62
}
63