Completed
Push — master ( 38612a...1c3c5f )
by Julián
02:35
created

ResourceAbstract   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 14
eloc 28
dl 0
loc 106
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getErrorCode() 0 4 1
A __construct() 0 3 1
A handleRequest() 0 9 2
A handleException() 0 15 6
A getData() 0 3 2
A getBaseDataWithAccounts() 0 13 2
1
<?php
2
3
namespace JulianBustamante\Plaid\Resources;
4
5
use GuzzleHttp\Exception\ClientException;
6
use JulianBustamante\Plaid\Exceptions\InvalidPublicTokenException;
7
use JulianBustamante\Plaid\Exceptions\InvalidAccessTokenException;
8
use JulianBustamante\Plaid\Resources\Errors\ErrorAbstract;
9
use JulianBustamante\Plaid\ServiceAbstract;
10
11
abstract class ResourceAbstract
12
{
13
    /**
14
     * Plaid service instance.
15
     *
16
     * @var \JulianBustamante\Plaid\ServiceAbstract
17
     */
18
    protected $plaid;
19
20
    /**
21
     * Guzzle Response.
22
     */
23
    protected $response;
24
25
    /**
26
     * @param \JulianBustamante\Plaid\ServiceAbstract $plaid
27
     */
28
    public function __construct(ServiceAbstract $plaid)
29
    {
30
        $this->plaid  = $plaid;
31
    }
32
33
    /**
34
     * Handles resources requests.
35
     *
36
     * @param \Closure $callback
37
     *
38
     * @return \JulianBustamante\Plaid\Resources\ResourceAbstract
39
     */
40
    protected function handleRequest(\Closure $callback): ResourceAbstract
41
    {
42
        try {
43
            $this->response = $callback($this->plaid->getClient());
44
        } catch (ClientException $exception) {
45
            $this->handleException($exception);
46
        }
47
48
        return $this;
49
    }
50
51
    /**
52
     * Handles Plaid error codes and throws exceptions.
53
     *
54
     * @param \GuzzleHttp\Exception\ClientException $exception
55
     */
56
    protected function handleException(ClientException $exception): void
57
    {
58
        if ($exception->getCode() === 400
59
            && $exception->hasResponse()
60
            && $error_code = $this->getErrorCode($exception->getResponse())) {
61
62
            switch ($error_code) {
63
                case ErrorAbstract::INVALID_PUBLIC_TOKEN:
64
                    throw new InvalidPublicTokenException($exception->getMessage());
65
                case ErrorAbstract::INVALID_ACCESS_TOKEN:
66
                    throw new InvalidAccessTokenException($exception->getMessage());
67
            }
68
        }
69
70
        throw $exception;
71
    }
72
73
    /**
74
     * Extracts the Plaid error code from the response.
75
     *
76
     * @param $response
77
     *
78
     * @return bool
79
     */
80
    private function getErrorCode($response)
81
    {
82
        $body = \GuzzleHttp\json_decode($response->getBody(), true);
83
        return $body['error_code'] ?? null;
84
    }
85
86
    /**
87
     * Returns base data required for all the requests.
88
     *
89
     * @param $access_token
90
     * @param $account_ids
91
     *
92
     * @return array
93
     */
94
    protected function getBaseDataWithAccounts($access_token, array $account_ids): array
95
    {
96
        $data = [
97
            'json' => [
98
                    'access_token' => $access_token,
99
                ] + $this->plaid->getAPIKeys(),
100
        ];
101
102
        if (! empty($account_ids)) {
103
            $data['json']['options']['account_ids'] = $account_ids;
104
        }
105
106
        return $data;
107
    }
108
109
    /**
110
     * Returns decoded json response.
111
     *
112
     * @return mixed
113
     */
114
    protected function getData()
115
    {
116
        return $this->response !== null ? \GuzzleHttp\json_decode($this->response->getBody(), true) : null;
117
    }
118
}
119