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

HttpRequestAnnotHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 1
dl 0
loc 40
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 26 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