1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Amelia\Monzo; |
4
|
|
|
|
5
|
|
|
use TypeError; |
6
|
|
|
use Amelia\Monzo\Api\Feed; |
7
|
|
|
use Amelia\Monzo\Api\Pots; |
8
|
|
|
use Amelia\Monzo\Api\Balance; |
9
|
|
|
use Amelia\Monzo\Api\Accounts; |
10
|
|
|
use Amelia\Monzo\Api\Webhooks; |
11
|
|
|
use Laravel\Socialite\Two\User; |
12
|
|
|
use Amelia\Monzo\Api\Transactions; |
13
|
|
|
use Amelia\Monzo\Api\ErrorHandling; |
14
|
|
|
use Amelia\Monzo\Exceptions\MonzoException; |
15
|
|
|
use Amelia\Monzo\Contracts\HasMonzoCredentials; |
16
|
|
|
use Amelia\Monzo\Contracts\Client as ClientContract; |
17
|
|
|
|
18
|
|
|
class Monzo |
19
|
|
|
{ |
20
|
|
|
use ErrorHandling, Accounts, Transactions, Balance, Webhooks, Feed, Pots; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A user's access token. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $token; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The refresh token for the API. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $refreshToken; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The account to use for querying. |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $account; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* An API client to use. |
45
|
|
|
* |
46
|
|
|
* @var \Amelia\Monzo\Contracts\Client |
47
|
|
|
*/ |
48
|
|
|
protected $client; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* A user, if given. |
52
|
|
|
* |
53
|
|
|
* @var \Amelia\Monzo\Contracts\HasMonzoCredentials |
54
|
|
|
*/ |
55
|
|
|
protected $user; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* A default user, if given. |
59
|
|
|
* |
60
|
|
|
* @var \Amelia\Monzo\Contracts\HasMonzoCredentials |
61
|
|
|
*/ |
62
|
|
|
protected static $defaultUser; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Default access token, if set. |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected static $defaultToken; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Default refresh token, if set. |
73
|
|
|
* |
74
|
|
|
* @var string |
75
|
|
|
*/ |
76
|
|
|
protected static $defaultRefreshToken; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Make a new monzo instance. |
80
|
|
|
* |
81
|
|
|
* @param \Amelia\Monzo\Contracts\Client $client |
82
|
|
|
*/ |
83
|
|
|
public function __construct(ClientContract $client) |
84
|
|
|
{ |
85
|
|
|
$this->client = $client; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the current user or access token. |
90
|
|
|
* |
91
|
|
|
* @param \Laravel\Socialite\Two\User|\Amelia\Monzo\Contracts\HasMonzoCredentials|string $user |
92
|
|
|
* @param string $refreshToken |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function as($user, string $refreshToken = null) |
96
|
|
|
{ |
97
|
|
|
// assume a raw access token was just passed in |
98
|
|
|
if (is_string($user)) { |
99
|
|
|
$this->token = $user; |
100
|
|
|
$this->refreshToken = $refreshToken; |
101
|
|
|
} // if we were given a socialite user, use that. |
102
|
|
|
elseif ($user instanceof User) { |
103
|
|
|
$this->token = $user->token; |
104
|
|
|
$this->refreshToken = $user->refreshToken; |
105
|
|
|
} // if we were given a proper user object, use that. |
106
|
|
|
elseif ($user instanceof HasMonzoCredentials) { |
|
|
|
|
107
|
|
|
$this->user = $user; |
108
|
|
|
$this->token = $user->getMonzoAccessToken(); |
109
|
|
|
$this->refreshToken = $user->getMonzoRefreshToken(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Add the "before" pagination query param. |
117
|
|
|
* |
118
|
|
|
* @param string $id |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function before(string $id) |
122
|
|
|
{ |
123
|
|
|
$this->client->before($id); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Add the "since" pagination query param. |
130
|
|
|
* |
131
|
|
|
* @param string $id |
132
|
|
|
* @return $this |
133
|
|
|
*/ |
134
|
|
|
public function since(string $id) |
135
|
|
|
{ |
136
|
|
|
$this->client->since($id); |
137
|
|
|
|
138
|
|
|
return $this; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Select the max number of objects to return from the API. |
143
|
|
|
* |
144
|
|
|
* @param int $limit |
145
|
|
|
* @return \Amelia\Monzo\Monzo |
146
|
|
|
*/ |
147
|
|
|
public function take(int $limit) |
148
|
|
|
{ |
149
|
|
|
$this->client->take($limit > 100 ? 100 : $limit); |
150
|
|
|
|
151
|
|
|
return $this; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Expand given keys. |
156
|
|
|
* |
157
|
|
|
* @param string|array $params |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
public function expand($params) |
161
|
|
|
{ |
162
|
|
|
$this->client->expand(is_array($params) ? $params : func_get_args()); |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set the current access token. |
169
|
|
|
* |
170
|
|
|
* @param string $token |
171
|
|
|
* @param string|null $refreshToken |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public static function setToken(string $token, string $refreshToken = null) |
175
|
|
|
{ |
176
|
|
|
static::$defaultToken = $token; |
177
|
|
|
static::$defaultRefreshToken = $refreshToken; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Set credentials via a user object. |
182
|
|
|
* |
183
|
|
|
* @param mixed $user |
184
|
|
|
* @return void |
185
|
|
|
* @throws \TypeError |
186
|
|
|
*/ |
187
|
|
|
public static function setUser($user) |
188
|
|
|
{ |
189
|
|
|
// if we've been passed a socialite user, use that. |
190
|
|
|
if ($user instanceof User) { |
191
|
|
|
static::$defaultToken = $user->token; |
192
|
|
|
static::$defaultRefreshToken = $user->refreshToken; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// if we're using the monzo interface, we can just use that. |
196
|
|
|
elseif ($user instanceof HasMonzoCredentials) { |
197
|
|
|
static::$defaultUser = $user; |
198
|
|
|
static::$defaultToken = $user->getMonzoAccessToken(); |
199
|
|
|
static::$defaultRefreshToken = $user->getMonzoRefreshToken(); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
// if we didn't get either, throw a TypeError. |
203
|
|
|
else { |
204
|
|
|
throw new TypeError( |
205
|
|
|
static::class . '::' . __METHOD__ . |
206
|
|
|
' expects ' . User::class . ' or an object using ' |
207
|
|
|
. MonzoCredentials::class |
208
|
|
|
); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get the current access token. |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
* @throws \Amelia\Monzo\Exceptions\MonzoException if an access token is not set. |
217
|
|
|
*/ |
218
|
|
|
protected function getAccessToken() |
219
|
|
|
{ |
220
|
|
|
$token = $this->token ?? static::$defaultToken; |
221
|
|
|
|
222
|
|
|
if ($token === null) { |
|
|
|
|
223
|
|
|
throw new MonzoException( |
224
|
|
|
'An access token has not been set; ' . |
225
|
|
|
'have you given a user?' |
226
|
|
|
); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $token; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @return string |
234
|
|
|
* @throws \Amelia\Monzo\Exceptions\MonzoException if a refresh token is not set. |
235
|
|
|
*/ |
236
|
|
|
protected function getRefreshToken() |
237
|
|
|
{ |
238
|
|
|
$token = $this->refreshToken ?? static::$defaultRefreshToken; |
239
|
|
|
|
240
|
|
|
if ($token === null) { |
|
|
|
|
241
|
|
|
throw new MonzoException( |
242
|
|
|
'A refresh token has not been set; ' . |
243
|
|
|
'have you given a user, and are you using a confidential client?' |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $token; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* Check if we have a refresh token set. |
252
|
|
|
* |
253
|
|
|
* @return bool |
254
|
|
|
*/ |
255
|
|
|
protected function hasRefreshToken() |
256
|
|
|
{ |
257
|
|
|
return ($this->refreshToken ?? static::$defaultRefreshToken) !== null; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Check if we have a user present. |
262
|
|
|
* |
263
|
|
|
* @return bool |
264
|
|
|
*/ |
265
|
|
|
protected function hasUser() |
266
|
|
|
{ |
267
|
|
|
return $this->getUser() instanceof HasMonzoCredentials; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Get a user, if set. |
272
|
|
|
* |
273
|
|
|
* @return \Amelia\Monzo\Contracts\HasMonzoCredentials |
274
|
|
|
*/ |
275
|
|
|
protected function getUser() |
276
|
|
|
{ |
277
|
|
|
return $this->user ?? static::$defaultUser; |
278
|
|
|
} |
279
|
|
|
} |
280
|
|
|
|