Passed
Push — master ( fe8874...312ccc )
by Paul
02:35
created

AbstractRequest::getRequestOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace CCT\Component\Rest\Http;
6
7
use CCT\Component\Rest\Exception\InvalidParameterException;
8
use CCT\Component\Rest\Exception\ServiceUnavailableException;
9
use CCT\Component\Rest\Http\Definition\QueryParams;
10
use CCT\Component\Rest\Http\Definition\RequestHeaders;
11
use GuzzleHttp\Client as GuzzleClient;
12
use GuzzleHttp\Exception\ConnectException;
13
use GuzzleHttp\Exception\RequestException;
14
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
15
16
abstract class AbstractRequest implements RequestInterface
17
{
18
    /**
19
     * @var GuzzleClient
20
     */
21
    protected $client;
22
23
    /**
24
     * Request headers
25
     *
26
     * @var RequestHeaders
27
     */
28
    protected $headers;
29
30
    /**
31
     * @param GuzzleClient $client
32
     */
33 4
    public function __construct(GuzzleClient $client)
34
    {
35 4
        $this->client = $client;
36 4
    }
37
38
    /**
39
     * @param string $uri
40
     * @param QueryParams|null $queryParams
41
     *
42
     * @return ResponseInterface|\Symfony\Component\HttpFoundation\Response
43
     */
44 3
    protected function requestGet($uri, QueryParams $queryParams = null)
45
    {
46 3
        return $this->execute(self::METHOD_GET, $uri, [], $queryParams);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->execute(se... array(), $queryParams) also could return the type Psr\Http\Message\ResponseInterface which is incompatible with the documented return type CCT\Component\Rest\Http\...HttpFoundation\Response.
Loading history...
47
    }
48
49
    /**
50
     * @param string $uri
51
     * @param QueryParams|null $queryParams
52
     *
53
     * @return ResponseInterface|\Symfony\Component\HttpFoundation\Response
54
     */
55
    protected function requestDelete($uri, QueryParams $queryParams = null)
56
    {
57
        return $this->execute(self::METHOD_DELETE, $uri, [], $queryParams);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->execute(se... array(), $queryParams) also could return the type Psr\Http\Message\ResponseInterface which is incompatible with the documented return type CCT\Component\Rest\Http\...HttpFoundation\Response.
Loading history...
58
    }
59
60
    /**
61
     * @param string $uri
62
     * @param array|object $formData
63
     * @param QueryParams|null $queryParams
64
     *
65
     * @return ResponseInterface|\Symfony\Component\HttpFoundation\Response
66
     */
67
    protected function requestPost($uri, $formData, QueryParams $queryParams = null)
68
    {
69
        return $this->execute(self::METHOD_POST, $uri, $formData, $queryParams);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->execute(se...formData, $queryParams) also could return the type Psr\Http\Message\ResponseInterface which is incompatible with the documented return type CCT\Component\Rest\Http\...HttpFoundation\Response.
Loading history...
70
    }
71
72
    /**
73
     * @param string $uri
74
     * @param array|object $formData
75
     * @param QueryParams|null $queryParams
76
     *
77
     * @return ResponseInterface|\Symfony\Component\HttpFoundation\Response
78
     */
79
    protected function requestPatch($uri, $formData, QueryParams $queryParams = null)
80
    {
81
        return $this->execute(self::METHOD_PATCH, $uri, $formData, $queryParams);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->execute(se...formData, $queryParams) also could return the type Psr\Http\Message\ResponseInterface which is incompatible with the documented return type CCT\Component\Rest\Http\...HttpFoundation\Response.
Loading history...
82
    }
83
84
    /**
85
     * @param string $uri
86
     * @param array|object $formData
87
     * @param QueryParams|null $queryParams
88
     *
89
     * @return ResponseInterface|\Symfony\Component\HttpFoundation\Response
90
     */
91
    protected function requestPut($uri, $formData, QueryParams $queryParams = null)
92
    {
93
        return $this->execute(self::METHOD_PUT, $uri, $formData, $queryParams);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->execute(se...formData, $queryParams) also could return the type Psr\Http\Message\ResponseInterface which is incompatible with the documented return type CCT\Component\Rest\Http\...HttpFoundation\Response.
Loading history...
94
    }
95
96
    /**
97
     * @param string $method
98
     * @param string $uri
99
     * @param array|object $formData
100
     * @param QueryParams|null $queryParams
101
     *
102
     * @return PsrResponseInterface|\Symfony\Component\HttpFoundation\Response
103
     */
104 3
    protected function execute($method, string $uri, $formData = [], QueryParams $queryParams = null)
105
    {
106 3
        $options = $this->getRequestOptions($formData);
107
108 3
        $queryParams = $queryParams ?: new QueryParams();
109 3
        $uri = $this->addQueryParamsToUri($uri, $queryParams);
110
111 3
        $response = $this->sendRequest($method, $uri, $options);
112
113 3
        return $response;
114
    }
115
116
    /**
117
     * @param array|object $formData
118
     *
119
     * @return array
120
     */
121
    protected function getRequestOptions($formData = [])
122
    {
123
        return [
124
            'form_params' => $formData,
125
            'headers' => $this->getHeaders()->toArray()
126
        ];
127
    }
128
129
    /**
130
     * @param string $method
131
     * @param string $uri
132
     * @param array $options
133
     *
134
     * @throws ServiceUnavailableException
135
     *
136
     * @return Response|object
137
     */
138 3
    protected function sendRequest($method, string $uri, $options = [])
139
    {
140
        try {
141 3
            $response = $this->client->request($method, $uri, $options);
142
        } catch (ConnectException $e) {
143
            throw new ServiceUnavailableException($e->getRequest(), $e->getMessage());
144
        } catch (RequestException $e) {
145
            if (null === $e->getResponse()->getBody()) {
146
                throw $e;
147
            }
148
            $response = $e->getResponse();
149
        }
150
151 3
        return $response;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $response also could return the type Psr\Http\Message\ResponseInterface which is incompatible with the documented return type object|CCT\Component\Rest\Http\Response.
Loading history...
152
    }
153
154
    /**
155
     * Adds a query string params to the URI.
156
     *
157
     * @param string $uri
158
     * @param QueryParams $queryParams
159
     *
160
     * @return string
161
     */
162 3
    protected function addQueryParamsToUri(string $uri, QueryParams $queryParams)
163
    {
164 3
        if (false !== strpos($uri, '?')) {
165
            throw new InvalidParameterException(sprintf(
166
                'It was not possible to normalize the URI as the current URI %s already 
167
                has the interrogation char in its string.' .
168
                $uri
169
            ));
170
        }
171
172 3
        return $uri . $queryParams->toString();
173
    }
174
175
    /**
176
     * Set headers for request
177
     *
178
     * @param RequestHeaders $headers
179
     */
180 3
    protected function setHeaders(RequestHeaders $headers)
181
    {
182 3
        $this->headers = $headers;
183 3
    }
184
185
    /**
186
     * Get headers for request
187
     *
188
     * @return RequestHeaders
189
     */
190 3
    protected function getHeaders(): RequestHeaders
191
    {
192 3
        return $this->headers;
193
    }
194
}
195