Rest::post()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.584
c 0
b 0
f 0
cc 4
nc 4
nop 4
1
<?php
2
3
namespace WeDevBr\Bankly\Traits;
4
5
use Illuminate\Http\Client\RequestException;
6
use Illuminate\Support\Facades\Http;
7
use Ramsey\Uuid\Uuid;
8
use WeDevBr\Bankly\Auth\Auth;
9
use WeDevBr\Bankly\Inputs\DocumentAnalysis;
10
11
/**
12
 * Trait Rest
13
 * @author Rafael Teixeira <[email protected]>
14
 * @package WeDevBr\Bankly
15
 */
16
trait Rest
17
{
18
    /** @var array */
19
    protected $headers = [];
20
21
    /** @var string */
22
    protected $apiUrl;
23
24
    /** @var string */
25
    protected $apiVersion = '1.0';
26
27
    /**
28
     * @param string $apiUrl
29
     * @return self
30
     */
31
    public function setApiUrl(string $apiUrl)
32
    {
33
        $this->apiUrl = $apiUrl;
34
        return $this;
35
    }
36
37
    /**
38
     * @param string $apiVersion
39
     * @return self
40
     */
41
    public function setApiVersion(string $apiVersion)
42
    {
43
        $this->apiVersion = $apiVersion;
44
        return $this;
45
    }
46
47
    /**
48
     * @param array $header
49
     * @return void
50
     */
51
    public function setHeaders($header)
52
    {
53
        $this->headers = array_merge($this->headers, $header);
54
    }
55
56
    /**
57
     * @param string $endpoint
58
     * @return bool
59
     */
60
    private function requireCorrelationId(string $endpoint)
61
    {
62
        $not_required_endpoints = [
63
            '/banklist',
64
            '/connect/token'
65
        ];
66
67
        return !in_array($endpoint, $not_required_endpoints);
68
    }
69
70
    /**
71
     * @param string $endpoint
72
     * @param array|string|null $query
73
     * @param string|null $correlationId
74
     * @return array|mixed
75
     * @throws RequestException
76
     */
77
    public function get(string $endpoint, $query = null, $correlationId = null)
78
    {
79
        if (is_null($correlationId) && $this->requireCorrelationId($endpoint)) {
80
            $correlationId = Uuid::uuid4()->toString();
81
        }
82
83
        $this->setHeaders([
84
            'API-Version' => $this->apiVersion,
85
            'x-correlation-id' => $correlationId
86
        ]);
87
88
        $token = Auth::login()->getToken();
89
        return Http::withToken($token)
90
            ->withHeaders($this->headers)
91
            ->get($this->getFinalUrl($endpoint), $query)
92
            ->throw()
93
            ->json();
94
    }
95
96
    /**
97
     * @param string $endpoint
98
     * @param array $body
99
     * @param string|null $correlationId
100
     * @param bool $asJson
101
     * @return array|mixed
102
     * @throws RequestException
103
     */
104 View Code Duplication
    private function post(string $endpoint, array $body = [], $correlationId = null, bool $asJson = false)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        if (is_null($correlationId) && $this->requireCorrelationId($endpoint)) {
107
            $correlationId = Uuid::uuid4()->toString();
108
        }
109
110
        $this->setHeaders([
111
            'API-Version' => $this->apiVersion,
112
            'x-correlation-id' => $correlationId
113
        ]);
114
115
        $bodyFormat = $asJson ? 'json' : 'form_params';
116
        $token = Auth::login()->getToken();
117
118
        return Http::withToken($token)
119
            ->withHeaders($this->headers)
120
            ->bodyFormat($bodyFormat)
121
            ->post($this->getFinalUrl($endpoint), $body)
122
            ->throw()
123
            ->json();
124
    }
125
126
    /**
127
     * @param string $endpoint
128
     * @param array $body
129
     * @param string|null $correlationId
130
     * @param bool $asJson
131
     * @param bool $attachment
132
     * @param DocumentAnalysis $document
133
     * @return array|mixed
134
     * @throws RequestException
135
     */
136
    private function put(
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
137
        string $endpoint,
138
        array $body = [],
139
        $correlationId = null,
140
        bool $asJson = false,
141
        bool $attachment = false,
142
        DocumentAnalysis $document = null
143
    ) {
144
        if (is_null($correlationId) && $this->requireCorrelationId($endpoint)) {
145
            $correlationId = Uuid::uuid4()->toString();
146
        }
147
148
        $this->setHeaders([
149
            'API-Version' => $this->apiVersion,
150
            'x-correlation-id' => $correlationId
151
        ]);
152
153
        $bodyFormat = $asJson ? 'json' : 'form_params';
154
        $token = Auth::login()->getToken();
155
156
        $request = Http::withToken($token)
157
            ->withHeaders($this->headers)
158
            ->bodyFormat($bodyFormat);
159
160
        if ($attachment && !is_null($document)) {
161
            $request->attach($document->getFieldName(), $document->getFileContents(), $document->getFileName());
162
        }
163
164
        return $request->put($this->getFinalUrl($endpoint), $body)
165
            ->throw()
166
            ->json();
167
    }
168
169
    /**
170
     * @param string $endpoint
171
     * @param array $body
172
     * @param string|null $correlationId
173
     * @param bool $asJson
174
     * @return array|mixed
175
     * @throws RequestException
176
     */
177 View Code Duplication
    private function patch(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
        string $endpoint,
179
        array $body = [],
180
        $correlationId = null,
181
        bool $asJson = false
182
    ) {
183
        if (is_null($correlationId) && $this->requireCorrelationId($endpoint)) {
184
            $correlationId = Uuid::uuid4()->toString();
185
        }
186
187
        $this->setHeaders([
188
            'API-Version' => $this->apiVersion,
189
            'x-correlation-id' => $correlationId
190
        ]);
191
192
        $bodyFormat = $asJson ? 'json' : 'form_params';
193
        $token = Auth::login()->getToken();
194
195
        $request = Http::withToken($token)
196
            ->withHeaders($this->headers)
197
            ->bodyFormat($bodyFormat);
198
199
        return $request->patch($this->getFinalUrl($endpoint), $body)
200
            ->throw()
201
            ->json();
202
    }
203
204
    /**
205
     * Http delete method.
206
     *
207
     * @param string $endpoint
208
     * @return array|mixed
209
     * @throws RequestException
210
     */
211
    private function delete(string $endpoint)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
212
    {
213
        $token = Auth::login()->getToken();
214
        $request = Http::withToken($token)
215
            ->withHeaders($this->headers);
216
217
        return $request->delete($this->getFinalUrl($endpoint))
218
            ->throw()
219
            ->json();
220
    }
221
222
    /**
223
     * @param string $endpoint
224
     * @return string
225
     */
226
    private function getFinalUrl(string $endpoint)
227
    {
228
        if (is_null($this->apiUrl)) {
229
            $this->apiUrl = config('bankly')['api_url'];
230
        }
231
232
        return $this->apiUrl . $endpoint;
233
    }
234
}
235