Completed
Push — master ( 172781...56894e )
by Freek
01:26
created

MakesHttpRequests::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace OhDear\PhpSdk;
4
5
use Exception;
6
use Psr\Http\Message\ResponseInterface;
7
use OhDear\PhpSdk\Exceptions\NotFoundException;
8
use OhDear\PhpSdk\Exceptions\ValidationException;
9
use OhDear\PhpSdk\Exceptions\FailedActionException;
10
11
trait MakesHttpRequests
12
{
13
    /**
14
     * @param  string $uri
15
     *
16
     * @return mixed
17
     */
18
    protected function get(string $uri)
19
    {
20
        return $this->request('GET', $uri);
21
    }
22
23
    /**
24
     * @param  string $uri
25
     * @param  array $payload
26
     *
27
     * @return mixed
28
     */
29
    protected function post(string $uri, array $payload = [])
30
    {
31
        return $this->request('POST', $uri, $payload);
32
    }
33
34
    /**
35
     * @param  string $uri
36
     * @param  array $payload
37
     *
38
     * @return mixed
39
     */
40
    protected function put(string $uri, array $payload = [])
41
    {
42
        return $this->request('PUT', $uri, $payload);
43
    }
44
45
    /**
46
     * @param  string $uri
47
     * @param  array $payload
48
     *
49
     * @return mixed
50
     */
51
    protected function delete(string $uri, array $payload = [])
52
    {
53
        return $this->request('DELETE', $uri, $payload);
54
    }
55
56
    /**
57
     * @param  string $verb
58
     * @param  string $uri
59
     * @param  array $payload
60
     *
61
     * @return mixed
62
     */
63
    protected function request(string $verb, string $uri, array $payload = [])
64
    {
65
        $response = $this->client->request($verb, $uri,
0 ignored issues
show
Bug introduced by
The property client does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66
            empty($payload) ? [] : ['form_params' => $payload]
67
        );
68
69
        if (! $this->isSuccessFul($response)) {
70
            return $this->handleRequestError($response);
71
        }
72
73
        $responseBody = (string) $response->getBody();
74
75
        return json_decode($responseBody, true) ?: $responseBody;
76
    }
77
78
    public function isSuccessFul($response): bool
79
    {
80
        if (! $response) {
81
            return false;
82
        }
83
84
        return substr($response->getStatusCode(), 0, 1) === 2;
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of substr($response->getStatusCode(), 0, 1) (string) and 2 (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
85
    }
86
87
    protected function handleRequestError(ResponseInterface $response)
88
    {
89
        if ($response->getStatusCode() === 422) {
90
            throw new ValidationException(json_decode((string) $response->getBody(), true));
91
        }
92
93
        if ($response->getStatusCode() === 404) {
94
            throw new NotFoundException();
95
        }
96
97
        if ($response->getStatusCode() === 400) {
98
            throw new FailedActionException((string) $response->getBody());
99
        }
100
101
        throw new Exception((string) $response->getBody());
102
    }
103
}
104