MakesHttpRequests::put()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace TestMonitor\ActiveCampaign;
4
5
use Psr\Http\Message\ResponseInterface;
6
use TestMonitor\ActiveCampaign\Exceptions\NotFoundException;
7
use TestMonitor\ActiveCampaign\Exceptions\ValidationException;
8
use TestMonitor\ActiveCampaign\Exceptions\FailedActionException;
9
10
/**
11
 * Class MakesHttpRequests.
12
 *
13
 * @property \GuzzleHttp\Client $guzzle
14
 */
15
trait MakesHttpRequests
16
{
17
    /**
18
     * Make a GET request to ActiveCampaign and return the response.
19
     *
20
     * @param  string $uri
21
     * @param array $payload
22
     *
23
     * @throws \TestMonitor\ActiveCampaign\Exceptions\FailedActionException
24
     * @throws \TestMonitor\ActiveCampaign\Exceptions\NotFoundException
25
     * @throws \TestMonitor\ActiveCampaign\Exceptions\ValidationException
26
     *
27
     * @return mixed
28
     */
29
    private function get($uri, $payload = [])
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
30
    {
31
        return $this->request('GET', $uri, $payload);
32
    }
33
34
    /**
35
     * Make a POST request to ActiveCampaign and return the response.
36
     *
37
     * @param  string $uri
38
     * @param  array $payload
39
     *
40
     * @throws \TestMonitor\ActiveCampaign\Exceptions\FailedActionException
41
     * @throws \TestMonitor\ActiveCampaign\Exceptions\NotFoundException
42
     * @throws \TestMonitor\ActiveCampaign\Exceptions\ValidationException
43
     *
44
     * @return mixed
45
     */
46
    private function post($uri, array $payload = [])
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
47
    {
48
        return $this->request('POST', $uri, $payload);
49
    }
50
51
    /**
52
     * Make a PUT request to ActiveCampaign and return the response.
53
     *
54
     * @param  string $uri
55
     * @param  array $payload
56
     *
57
     * @throws \TestMonitor\ActiveCampaign\Exceptions\FailedActionException
58
     * @throws \TestMonitor\ActiveCampaign\Exceptions\NotFoundException
59
     * @throws \TestMonitor\ActiveCampaign\Exceptions\ValidationException
60
     *
61
     * @return mixed
62
     */
63
    private function put($uri, array $payload = [])
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
64
    {
65
        return $this->request('PUT', $uri, $payload);
66
    }
67
68
    /**
69
     * Make a DELETE request to ActiveCampaign and return the response.
70
     *
71
     * @param  string $uri
72
     * @param  array $payload
73
     *
74
     * @throws \TestMonitor\ActiveCampaign\Exceptions\FailedActionException
75
     * @throws \TestMonitor\ActiveCampaign\Exceptions\NotFoundException
76
     * @throws \TestMonitor\ActiveCampaign\Exceptions\ValidationException
77
     *
78
     * @return mixed
79
     */
80
    private function delete($uri, array $payload = [])
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
81
    {
82
        return $this->request('DELETE', $uri, $payload);
83
    }
84
85
    /**
86
     * Make request to ActiveCampaign and return the response.
87
     *
88
     * @param  string $verb
89
     * @param  string $uri
90
     * @param  array $payload
91
     *
92
     * @throws \TestMonitor\ActiveCampaign\Exceptions\FailedActionException
93
     * @throws \TestMonitor\ActiveCampaign\Exceptions\NotFoundException
94
     * @throws \TestMonitor\ActiveCampaign\Exceptions\ValidationException
95
     *
96
     * @return mixed
97
     */
98
    private function request($verb, $uri, array $payload = [])
99
    {
100
        $response = $this->guzzle->request(
101
            $verb,
102
            $uri,
103
            $payload
104
        );
105
106
        if (! in_array($response->getStatusCode(), [200, 201])) {
107
            return $this->handleRequestError($response);
108
        }
109
110
        $responseBody = (string) $response->getBody();
111
112
        return json_decode($responseBody, true) ?: $responseBody;
113
    }
114
115
    /**
116
     * @param  \Psr\Http\Message\ResponseInterface $response
117
     *
118
     * @throws \TestMonitor\ActiveCampaign\Exceptions\ValidationException
119
     * @throws \TestMonitor\ActiveCampaign\Exceptions\NotFoundException
120
     * @throws \TestMonitor\ActiveCampaign\Exceptions\FailedActionException
121
     * @throws \Exception
122
     *
123
     * @return void
124
     */
125
    private function handleRequestError(ResponseInterface $response)
126
    {
127
        if ($response->getStatusCode() == 422) {
128
            throw new ValidationException(json_decode((string) $response->getBody(), true));
129
        }
130
131
        if ($response->getStatusCode() == 404) {
132
            throw new NotFoundException();
133
        }
134
135
        if ($response->getStatusCode() == 400) {
136
            throw new FailedActionException((string) $response->getBody());
137
        }
138
139
        throw new \Exception((string) $response->getBody());
140
    }
141
}
142