Client::setClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace TestMonitor\DoneDone;
4
5
use GuzzleHttp\Client as GuzzleClient;
6
use Psr\Http\Message\ResponseInterface;
7
use TestMonitor\DoneDone\Exceptions\Exception;
8
use TestMonitor\DoneDone\Exceptions\NotFoundException;
9
use TestMonitor\DoneDone\Exceptions\ValidationException;
10
use TestMonitor\DoneDone\Exceptions\FailedActionException;
11
use TestMonitor\DoneDone\Exceptions\UnauthorizedException;
12
13
class Client
14
{
15
    use Actions\ManagesAccounts,
0 ignored issues
show
introduced by
The trait TestMonitor\DoneDone\Actions\ManagesTasks requires some properties which are not provided by TestMonitor\DoneDone\Client: $priority, $assignee, $description, $status, $title
Loading history...
16
        Actions\ManagesPriorities,
17
        Actions\ManagesProjects,
18
        Actions\ManagesStatuses,
19
        Actions\ManagesTasks;
20
21
    /**
22
     * @var string
23
     */
24
    protected $username;
25
26
    /**
27
     * @var string
28
     */
29
    protected $token;
30
31
    /**
32
     * @var string
33
     */
34
    protected $url;
35
36
    /**
37
     * @var \GuzzleHttp\Client
38
     */
39
    protected $client;
40
41
    /**
42
     * Create a new client instance.
43
     *
44
     * @param string $username
45
     * @param string $token
46
     */
47 32
    public function __construct(string $username, string $token)
48
    {
49 32
        $this->username = $username;
50 32
        $this->token = $token;
51
    }
52
53
    /**
54
     * Returns an DoneDone client instance.
55
     *
56
     * @return \GuzzleHttp\Client
57
     */
58 32
    protected function client()
59
    {
60 32
        return $this->client ?? new GuzzleClient([
61 32
            'auth' => [
62 32
                $this->username,
63 32
                $this->token,
64 32
            ],
65 32
            'http_errors' => false,
66 32
            'base_uri' => 'https://2.donedone.com/public-api/',
67 32
            'headers' => [
68 32
                'Accept' => 'application/json',
69 32
                'Content-Type' => 'application/json; charset=utf-8',
70 32
            ],
71 32
        ]);
72
    }
73
74
    /**
75
     * @param \GuzzleHttp\Client $client
76
     */
77 32
    public function setClient(GuzzleClient $client)
78
    {
79 32
        $this->client = $client;
80
    }
81
82
    /**
83
     * Make a GET request to DoneDone servers and return the response.
84
     *
85
     * @param string $uri
86
     * @param array $payload
87
     *
88
     * @throws \GuzzleHttp\Exception\GuzzleException
89
     * @throws \TestMonitor\DoneDone\Exceptions\FailedActionException
90
     * @throws \TestMonitor\DoneDone\Exceptions\NotFoundException
91
     * @throws \TestMonitor\DoneDone\Exceptions\ValidationException
92
     *
93
     * @return mixed
94
     */
95 32
    protected function get($uri, array $payload = [])
96
    {
97 32
        return $this->request('GET', $uri, $payload);
98
    }
99
100
    /**
101
     * Make a POST request to DoneDone servers and return the response.
102
     *
103
     * @param string $uri
104
     * @param array $payload
105
     *
106
     * @throws \GuzzleHttp\Exception\GuzzleException
107
     * @throws \TestMonitor\DoneDone\Exceptions\FailedActionException
108
     * @throws \TestMonitor\DoneDone\Exceptions\NotFoundException
109
     * @throws \TestMonitor\DoneDone\Exceptions\ValidationException
110
     *
111
     * @return mixed
112
     */
113 1
    protected function post($uri, array $payload = [])
114
    {
115 1
        return $this->request('POST', $uri, $payload);
116
    }
117
118
    /**
119
     * Make request to DoneDone servers and return the response.
120
     *
121
     * @param string $verb
122
     * @param string $uri
123
     * @param array $payload
124
     *
125
     * @throws \GuzzleHttp\Exception\GuzzleException
126
     * @throws \TestMonitor\DoneDone\Exceptions\FailedActionException
127
     * @throws \TestMonitor\DoneDone\Exceptions\NotFoundException
128
     * @throws \TestMonitor\DoneDone\Exceptions\ValidationException
129
     *
130
     * @return mixed
131
     */
132 32
    protected function request($verb, $uri, array $payload = [])
133
    {
134 32
        $response = $this->client()->request(
135 32
            $verb,
136 32
            $uri,
137 32
            $payload
138 32
        );
139
140 32
        if (! in_array($response->getStatusCode(), [200, 201, 204, 206])) {
141 24
            return $this->handleRequestError($response);
142
        }
143
144 8
        $responseBody = (string) $response->getBody();
145
146 8
        return json_decode($responseBody, true) ?: $responseBody;
147
    }
148
149
    /**
150
     * @param  \Psr\Http\Message\ResponseInterface $response
151
     *
152
     * @throws \TestMonitor\DoneDone\Exceptions\ValidationException
153
     * @throws \TestMonitor\DoneDone\Exceptions\NotFoundException
154
     * @throws \TestMonitor\DoneDone\Exceptions\FailedActionException
155
     * @throws \Exception
156
     *
157
     * @return void
158
     */
159 24
    protected function handleRequestError(ResponseInterface $response)
160
    {
161 24
        if ($response->getStatusCode() == 422) {
162 7
            throw new ValidationException(json_decode((string) $response->getBody(), true));
163
        }
164
165 17
        if ($response->getStatusCode() == 404) {
166 5
            throw new NotFoundException((string) $response->getBody(), $response->getStatusCode());
0 ignored issues
show
Unused Code introduced by
The call to TestMonitor\DoneDone\Exc...xception::__construct() has too many arguments starting with (string)$response->getBody(). ( Ignorable by Annotation )

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

166
            throw /** @scrutinizer ignore-call */ new NotFoundException((string) $response->getBody(), $response->getStatusCode());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
167
        }
168
169 12
        if (in_array($response->getStatusCode(), [401, 402, 403])) {
170 5
            throw new UnauthorizedException((string) $response->getBody(), $response->getStatusCode());
171
        }
172
173 7
        if ($response->getStatusCode() == 400) {
174 5
            throw new FailedActionException((string) $response->getBody(), $response->getStatusCode());
175
        }
176
177 2
        throw new Exception((string) $response->getStatusCode());
178
    }
179
}
180