DefaultServiceMethod::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\ServiceMethod;
10
11
use LogicException;
12
use Psr\Http\Message\RequestInterface;
13
use Psr\Http\Message\ResponseInterface;
14
use Tebru\Retrofit\Call;
15
use Tebru\Retrofit\CallAdapter;
16
use Tebru\Retrofit\Internal\RequestBuilder;
17
use Tebru\Retrofit\ParameterHandler;
18
use Tebru\Retrofit\ResponseBodyConverter;
19
use Tebru\Retrofit\Internal\ServiceMethod;
20
21
/**
22
 * Class DefaultServiceMethod
23
 *
24
 * This is the class that [@see Call]s will interact with. Its main responsibility is taking
25
 * known request parameters and applying arguments supplied at runtime to build a PSR-7 request
26
 * object. Additionally, it converts responses and adapts [@Call]s.
27
 *
28
 * @author Nate Brunette <[email protected]>
29
 */
30
final class DefaultServiceMethod implements ServiceMethod
31
{
32
    /**
33
     * Request method
34
     *
35
     * @var string
36
     */
37
    private $method;
38
39
    /**
40
     * Request base url
41
     *
42
     * @var string
43
     */
44
    private $baseUrl;
45
46
    /**
47
     * Request path
48
     *
49
     * @var string
50
     */
51
    private $path;
52
53
    /**
54
     * @var array
55
     */
56
    private $headers;
57
58
    /**
59
     * Array of parameter handlers
60
     *
61
     * @var ParameterHandler[]
62
     */
63
    private $parameterHandlers;
64
65
    /**
66
     * The call adapter to use
67
     *
68
     * @var CallAdapter
69
     */
70
    private $callAdapter;
71
72
    /**
73
     * Response body converter
74
     *
75
     * @var ResponseBodyConverter
76
     */
77
    private $responseBodyConverter;
78
79
    /**
80
     * Error body converter
81
     *
82
     * @var ResponseBodyConverter
83
     */
84
    private $errorBodyConverter;
85
86
    /**
87
     * Constructor
88
     *
89
     * @param string $method
90
     * @param string $baseUrl
91
     * @param string $uri
92
     * @param array $headers
93
     * @param array $parameterHandlers
94
     * @param CallAdapter $callAdapter
95
     * @param ResponseBodyConverter $responseBodyConverter
96
     * @param ResponseBodyConverter $errorBodyConverter
97
     */
98 27
    public function __construct(
99
        string $method,
100
        string $baseUrl,
101
        string $uri,
102
        array $headers,
103
        array $parameterHandlers,
104
        CallAdapter $callAdapter,
105
        ResponseBodyConverter $responseBodyConverter,
106
        ResponseBodyConverter $errorBodyConverter
107
    ) {
108 27
        $this->method = $method;
109 27
        $this->baseUrl = $baseUrl;
110 27
        $this->path = $uri;
111 27
        $this->headers = $headers;
112 27
        $this->parameterHandlers = $parameterHandlers;
113 27
        $this->callAdapter = $callAdapter;
114 27
        $this->responseBodyConverter = $responseBodyConverter;
115 27
        $this->errorBodyConverter = $errorBodyConverter;
116 27
    }
117
118
    /**
119
     * Apply runtime arguments and build request
120
     *
121
     * @param array $args
122
     * @return RequestInterface
123
     * @throws \LogicException
124
     */
125 16
    public function toRequest(array $args): RequestInterface
126
    {
127 16
        if (\count($this->parameterHandlers) !== \count($args)) {
128 1
            throw new LogicException(sprintf(
129
                'Retrofit: Incompatible number of arguments. Expected %d and got %s. This either ' .
130
                'means that the service method was not called with the correct number of parameters, ' .
131 1
                'or there is not an annotation for every parameter.',
132 1
                \count($this->parameterHandlers),
133 1
                \count($args)
134
            ));
135
        }
136
137 15
        $requestBuilder = new RequestBuilder(
138 15
            $this->method,
139 15
            $this->baseUrl,
140 15
            $this->path,
141 15
            $this->headers
142
        );
143
144 15
        foreach ($this->parameterHandlers as $index => $parameterHandler) {
145 9
            $parameterHandler->apply($requestBuilder, $args[$index]);
146
        }
147
148 15
        return $requestBuilder->build();
149
    }
150
151
    /**
152
     * Take a response and convert it to expected value
153
     *
154
     * @param ResponseInterface $response
155
     * @return mixed
156
     */
157 15
    public function toResponseBody(ResponseInterface $response)
158
    {
159 15
        return $this->responseBodyConverter->convert($response->getBody());
160
    }
161
162
    /**
163
     * Take a response and convert it to expected value
164
     *
165
     * @param ResponseInterface $response
166
     * @return mixed
167
     */
168 1
    public function toErrorBody(ResponseInterface $response)
169
    {
170 1
        return $this->errorBodyConverter->convert($response->getBody());
171
    }
172
173
    /**
174
     * Take a [@see Call] and adapt to expected value
175
     *
176
     * @param Call $call
177
     * @return mixed
178
     */
179 16
    public function adapt(Call $call)
180
    {
181 16
        return $this->callAdapter->adapt($call);
182
    }
183
}
184