FieldMapAnnotHandler   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 36
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 23 3
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\Encodable;
14
use Tebru\Retrofit\Annotation\FieldMap;
15
use Tebru\Retrofit\AnnotationHandler;
16
use Tebru\Retrofit\Converter;
17
use Tebru\Retrofit\Internal\ParameterHandler\FieldMapParamHandler;
18
use Tebru\Retrofit\ServiceMethodBuilder;
19
use Tebru\Retrofit\StringConverter;
20
21
/**
22
 * Class FieldMapAnnotHandler
23
 *
24
 * @author Nate Brunette <[email protected]>
25
 */
26
final class FieldMapAnnotHandler implements AnnotationHandler
27
{
28
    /**
29
     * Set the content type to form encoded and adds a parameter handler for a field map
30
     *
31
     * @param FieldMap|AbstractAnnotation $annotation The annotation to handle
32
     * @param ServiceMethodBuilder $serviceMethodBuilder Used to construct a [@see ServiceMethod]
33
     * @param Converter|StringConverter $converter Converter used to convert types before sending to service method
34
     * @param int|null $index The position of the parameter or null if annotation does not reference parameter
35
     * @return void
36
     * @throws \InvalidArgumentException
37
     */
38 4
    public function handle(
39
        AbstractAnnotation $annotation,
40
        ServiceMethodBuilder $serviceMethodBuilder,
41
        ?Converter $converter,
42
        ?int $index
43
    ): void {
44 4
        if (!$annotation instanceof Encodable) {
45 1
            throw new InvalidArgumentException('Retrofit: Annotation must be encodable');
46
        }
47
48 3
        if (!$converter instanceof StringConverter) {
49 1
            throw new InvalidArgumentException(sprintf(
50 1
                'Retrofit: Converter must be a StringConverter, %s found',
51 1
                \gettype($converter)
52
            ));
53
        }
54
55 2
        $serviceMethodBuilder->setIsFormUrlEncoded();
56 2
        $serviceMethodBuilder->addParameterHandler(
57 2
            $index,
58 2
            new FieldMapParamHandler($converter, $annotation->isEncoded())
59
        );
60 2
    }
61
}
62