AbstractApi   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 83.87%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 145
ccs 26
cts 31
cp 0.8387
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A get() 0 10 2
A post() 0 4 1
A postRaw() 0 6 1
A put() 0 6 1
A patch() 0 6 1
A delete() 0 6 1
A handleErrors() 0 11 2
A createJsonBody() 0 4 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace AdamPaterson\ApiClient\Foundation\Api;
6
7
use AdamPaterson\ApiClient\Foundation\Exception\Domain\NotFoundException;
8
use AdamPaterson\ApiClient\Foundation\Exception\Domain\UnknownErrorException;
9
use Http\Client\HttpClient;
10
use Psr\Http\Message\ResponseInterface;
11
use AdamPaterson\ApiClient\Foundation\Http\RequestBuilder;
12
use AdamPaterson\ApiClient\Foundation\Contract\HydratorInterface as Hydrator;
13
14
/**
15
 * Class AbstractApi
16
 *
17
 * @package AdamPaterson\ApiClient\Foundation\Api
18
 */
19
abstract class AbstractApi
20
{
21
    /**
22
     * @var \Http\Client\HttpClient
23
     */
24
    protected $httpClient;
25
26
    /**
27
     * @var \AdamPaterson\ApiClient\Foundation\Contract\HydratorInterface
28
     */
29
    protected $hydrator;
30
31
    /**
32
     * @var \AdamPaterson\ApiClient\Foundation\Http\RequestBuilder
33
     */
34
    protected $requestBuilder;
35
36
    /**
37
     * AbstractApi constructor.
38
     *
39
     * @param \Http\Client\HttpClient                                       $httpClient
40
     * @param \AdamPaterson\ApiClient\Foundation\Contract\HydratorInterface $hydrator
41
     * @param \AdamPaterson\ApiClient\Foundation\Http\RequestBuilder        $requestBuilder
42
     */
43 15
    public function __construct(HttpClient $httpClient, Hydrator $hydrator, RequestBuilder $requestBuilder)
44
    {
45 15
        $this->httpClient = $httpClient;
46 15
        $this->requestBuilder = $requestBuilder;
47 15
        $this->hydrator = $hydrator;
48 15
    }
49
50
    /**
51
     * @param string $path
52
     * @param array  $params
53
     * @param array  $headers
54
     *
55
     * @return \Psr\Http\Message\ResponseInterface
56
     */
57 3
    protected function get(string $path, array $params = [], array $headers = []): ResponseInterface
58
    {
59 3
        if (count($params) > 0) {
60 3
            $path .= '?'.http_build_query($params);
61
        }
62
63 3
        return $this->httpClient->sendRequest(
64 3
            $this->requestBuilder->create('GET', $path, $headers)
65
        );
66
    }
67
68
    /**
69
     * @param string $path
70
     * @param array  $params
71
     * @param array  $headers
72
     *
73
     * @return \Psr\Http\Message\ResponseInterface
74
     */
75 3
    protected function post(string $path, array $params = [], $headers = []): ResponseInterface
76
    {
77 3
        return $this->postRaw($path, $this->createJsonBody($params), $headers);
78
    }
79
80
    /**
81
     * @param string $path
82
     * @param        $body
83
     * @param array  $headers
84
     *
85
     * @return \Psr\Http\Message\ResponseInterface
86
     */
87 3
    protected function postRaw(string $path, $body, array $headers = []): ResponseInterface
88
    {
89 3
        return $this->httpClient->sendRequest(
90 3
            $this->requestBuilder->create('POST', $path, $headers, $body)
91
        );
92
    }
93
94
    /**
95
     * @param string $path
96
     * @param array  $params
97
     * @param array  $headers
98
     *
99
     * @return \Psr\Http\Message\ResponseInterface
100
     */
101 3
    protected function put(string $path, array $params = [], array $headers = []): ResponseInterface
102
    {
103 3
        return $this->httpClient->sendRequest(
104 3
            $this->requestBuilder->create('PUT', $path, $headers, $this->createJsonBody($params))
0 ignored issues
show
Bug introduced by
It seems like $this->createJsonBody($params) targeting AdamPaterson\ApiClient\F...ctApi::createJsonBody() can also be of type string; however, AdamPaterson\ApiClient\F...equestBuilder::create() does only seem to accept null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
105
        );
106
    }
107
108
    /**
109
     * @param string $path
110
     * @param array  $params
111
     * @param array  $headers
112
     *
113
     * @return \Psr\Http\Message\ResponseInterface
114
     */
115 3
    protected function patch(string $path, array $params = [], array $headers = []): ResponseInterface
116
    {
117 3
        return $this->httpClient->sendRequest(
118 3
            $this->requestBuilder->create('PATCH', $path, $headers, $this->createJsonBody($params))
0 ignored issues
show
Bug introduced by
It seems like $this->createJsonBody($params) targeting AdamPaterson\ApiClient\F...ctApi::createJsonBody() can also be of type string; however, AdamPaterson\ApiClient\F...equestBuilder::create() does only seem to accept null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
119
        );
120
    }
121
122
    /**
123
     * @param string $path
124
     * @param array  $params
125
     * @param array  $headers
126
     *
127
     * @return \Psr\Http\Message\ResponseInterface
128
     */
129 3
    protected function delete(string $path, array $params = [], array $headers = []): ResponseInterface
130
    {
131 3
        return $this->httpClient->sendRequest(
132 3
            $this->requestBuilder->create('DELETE', $path, $headers, $this->createJsonBody($params))
0 ignored issues
show
Bug introduced by
It seems like $this->createJsonBody($params) targeting AdamPaterson\ApiClient\F...ctApi::createJsonBody() can also be of type string; however, AdamPaterson\ApiClient\F...equestBuilder::create() does only seem to accept null, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
133
        );
134
    }
135
136
    /**
137
     * @param \Psr\Http\Message\ResponseInterface $response
138
     *
139
     * @throws \AdamPaterson\ApiClient\Foundation\Exception\Domain\NotFoundException
140
     * @throws \AdamPaterson\ApiClient\Foundation\Exception\Domain\UnknownErrorException
141
     */
142
    protected function handleErrors(ResponseInterface $response)
143
    {
144
        switch ($response->getStatusCode()) {
145
            case 404:
146
                throw new NotFoundException();
147
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
148
            default:
149
                throw new UnknownErrorException();
150
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
151
        }
152
    }
153
154
    /**
155
     * @param array $params
156
     *
157
     * @return null|string
158
     */
159 12
    private function createJsonBody(array $params)
160
    {
161 12
        return (count($params) === 0) ? null : json_encode($params, empty($params) ? JSON_FORCE_OBJECT : 0);
162
    }
163
}
164