MakesHttpRequests::put()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Integrations\Traits;
4
5
use Exception;
6
use Integrations\Exceptions\FailedActionException;
7
use Integrations\Exceptions\NotFoundException;
8
use Integrations\Exceptions\ValidationException;
9
use Psr\Http\Message\ResponseInterface;
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(
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
            $verb, $uri,
67
            empty($payload) ? [] : ['form_params' => $payload]
68
        );
69
70
        if (! $this->isSuccessFul($response)) {
71
            return $this->handleRequestError($response);
72
        }
73
74
        $responseBody = (string) $response->getBody();
75
76
        return json_decode($responseBody, true) ?: $responseBody;
77
    }
78
79
    public function isSuccessFul($response): bool
80
    {
81
        if (! $response) {
82
            return false;
83
        }
84
85
        return (int) substr($response->getStatusCode(), 0, 1) === 2;
86
    }
87
88
    protected function handleRequestError(ResponseInterface $response)
89
    {
90
        if ($response->getStatusCode() === 422) {
91
            throw new ValidationException(json_decode((string) $response->getBody(), true));
92
        }
93
94
        if ($response->getStatusCode() === 404) {
95
            throw new NotFoundException();
96
        }
97
98
        if ($response->getStatusCode() === 400) {
99
            throw new FailedActionException((string) $response->getBody());
100
        }
101
102
        throw new Exception((string) $response->getBody());
103
    }
104
}
105