Client::wasSuccessfulRequest()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 18
ccs 0
cts 12
cp 0
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LauLamanApps\eCurring\Http\Adapter\Guzzle;
6
7
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
8
use GuzzleHttp\Exception\RequestException;
9
use LauLamanApps\eCurring\Exception\eCurringException;
10
use LauLamanApps\eCurring\Http\Adapter\Guzzle\Exception\Handler;
11
use LauLamanApps\eCurring\Http\Adapter\Guzzle\Exception\NotFoundException;
12
use LauLamanApps\eCurring\Http\ClientInterface;
13
use LauLamanApps\eCurring\Http\Endpoint\Exception\EndpointCouldNotBeMappedException;
14
use LauLamanApps\eCurring\Http\Endpoint\MapperInterface;
15
use LauLamanApps\eCurring\Http\Exception\ApiCallException;
16
use LauLamanApps\eCurring\Resource\Curser\Pagination;
17
use Psr\Http\Message\ResponseInterface;
18
19
final class Client implements ClientInterface
20
{
21
    /**
22
     * @var GuzzleClientInterface
23
     */
24
    private $guzzleClient;
25
26
    /**
27
     * @var MapperInterface
28
     */
29
    private $endpointMapper;
30
31
    public function __construct(
32
        GuzzleClientInterface $guzzleClient,
33
        MapperInterface $urlMapper
34
    ) {
35
        $this->guzzleClient = $guzzleClient;
36
        $this->endpointMapper = $urlMapper;
37
    }
38
39
    /**
40
     * @throws EndpointCouldNotBeMappedException
41
     * @throws eCurringException
42
     * @throws NotFoundException
43
     */
44
    public function getEndpoint(string $endpoint, ?array $urlBits = [], ?Pagination $page = null): ResponseInterface
45
    {
46
        $options = $page ? ['query' => $page->getQueryOptions()] : [];
47
48
        return $this->get(
49
            $this->endpointMapper->map($endpoint, $urlBits),
50
            $options
51
        );
52
    }
53
54
    /**
55
     * @throws eCurringException
56
     * @throws NotFoundException
57
     * @return ResponseInterface
58
     */
59
    public function getUrl(string $url): ResponseInterface
60
    {
61
        return $this->get($url);
62
    }
63
64
    public function getJson(ResponseInterface $response): string
65
    {
66
        return (string) $response->getBody();
67
    }
68
69
    /**
70
     * @throws NotFoundException
71
     * @throws eCurringException
72
     */
73
    private function get(string $url, ?array $options = []): ResponseInterface
74
    {
75
        try {
76
            $response = $this->guzzleClient->get($url, $options);
77
78
            $this->wasSuccessfulRequest($response);
79
80
            return $response;
81
        } catch (RequestException $exception) {
82
            Handler::handleRequestException($exception);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
83
        }
84
    }
85
86
    /**
87
     * @throws eCurringException
88
     */
89
    public function postEndpoint(string $endpoint, array $data, array $urlBits = null): ResponseInterface
90
    {
91
        return $this->post(
92
            $this->endpointMapper->map($endpoint, $urlBits),
0 ignored issues
show
Bug introduced by
It seems like $urlBits can also be of type null; however, parameter $bits of LauLamanApps\eCurring\Ht...\MapperInterface::map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

92
            $this->endpointMapper->map($endpoint, /** @scrutinizer ignore-type */ $urlBits),
Loading history...
93
            ['json' => $data]
94
        );
95
    }
96
97
    /**
98
     * @throws ApiCallException
99
     * @throws eCurringException
100
     * @throws NotFoundException
101
     */
102
    private function post(string $url, ?array $options = []): ResponseInterface
103
    {
104
        try {
105
            $response = $this->guzzleClient->post($url, $options);
106
            $this->wasSuccessfulRequest($response);
107
108
            return $response;
109
        } catch (RequestException $exception) {
110
            Handler::handleRequestException($exception);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
111
        }
112
    }
113
114
    /**
115
     * @throws eCurringException
116
     */
117
    public function patchEndpoint(string $endpoint, array $data, array $urlBits = null): ResponseInterface
118
    {
119
        return $this->patch(
120
            $this->endpointMapper->map($endpoint, $urlBits),
0 ignored issues
show
Bug introduced by
It seems like $urlBits can also be of type null; however, parameter $bits of LauLamanApps\eCurring\Ht...\MapperInterface::map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

120
            $this->endpointMapper->map($endpoint, /** @scrutinizer ignore-type */ $urlBits),
Loading history...
121
            ['json' => $data]
122
        );
123
    }
124
125
    /**
126
     * @throws ApiCallException
127
     * @throws eCurringException
128
     * @throws NotFoundException
129
     */
130
    private function patch(string $url, ?array $options = []): ResponseInterface
131
    {
132
        try {
133
            $response = $this->guzzleClient->patch($url, $options);
134
            $this->wasSuccessfulRequest($response);
135
136
            return $response;
137
        } catch (RequestException $exception) {
138
            Handler::handleRequestException($exception);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
139
        }
140
    }
141
142
    /**
143
     * @throws eCurringException
144
     */
145
    public function deleteEndpoint(string $endpoint, array $data, array $urlBits = null): void
146
    {
147
        $this->delete(
148
            $this->endpointMapper->map($endpoint, $urlBits),
0 ignored issues
show
Bug introduced by
It seems like $urlBits can also be of type null; however, parameter $bits of LauLamanApps\eCurring\Ht...\MapperInterface::map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

148
            $this->endpointMapper->map($endpoint, /** @scrutinizer ignore-type */ $urlBits),
Loading history...
149
            ['json' => $data]
150
        );
151
    }
152
153
    /**
154
     * @throws ApiCallException
155
     * @throws eCurringException
156
     * @throws NotFoundException
157
     */
158
    private function delete(string $url, ?array $options = []): ResponseInterface
159
    {
160
        try {
161
            $response = $this->guzzleClient->delete($url, $options);
162
            $this->wasSuccessfulRequest($response);
163
164
            return $response;
165
        } catch (RequestException $exception) {
166
            Handler::handleRequestException($exception);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
167
        }
168
    }
169
170
    /**
171
     * @throws ApiCallException
172
     */
173
    private function wasSuccessfulRequest(ResponseInterface $response): void
174
    {
175
        switch ($response->getStatusCode()) {
176
            case 200:
177
                return;
178
179
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
180
            case 201:
181
                return;
182
183
                break;
184
            default:
185
                throw new ApiCallException(
186
                    sprintf(
187
                        '%s %s: %s',
188
                        $response->getStatusCode(),
189
                        $response->getReasonPhrase(),
190
                        $response->getBody()->getContents()
191
                    )
192
                );
193
        }
194
    }
195
}
196