-
Notifications
You must be signed in to change notification settings - Fork 2
/
YnabClient.php
347 lines (295 loc) · 9.5 KB
/
YnabClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
<?php
namespace Petrica\Ynab;
use Petrica\Ynab\Entity\YnabBudget;
use Petrica\Ynab\Entity\YnabDiff;
use Petrica\Ynab\Entity\YnabItem;
use Petrica\Ynab\Repository\YnabBudgetRepository;
use Petrica\Ynab\Entity\YnabDevice;
use Petrica\Ynab\Repository\YnabDeviceRepository;
use Petrica\Ynab\Entity\YnabTransaction;
use Petrica\Ynab\Entity\YnabVersion;
use Petrica\Ynab\IO\YnabIOInterface;
use Petrica\Ynab\Repository\YnabBudgetFullRepository;
use Petrica\Ynab\Repository\YnabDiffRepository;
use Petrica\Ynab\Utils\YnabVersionUtils;
require_once 'class.uuid.php';
/**
* Class YnabClient
*
* Push/Pull changes from a YNAB4 database
*
* @package Petrica\Ynab
*/
class YnabClient
{
/**
* @var string
*/
protected $budgetFilepath;
/**
* @var string
*/
protected $deviceGUID;
/**
* @var string
*/
protected $deviceShortId;
/**
* @var YnabIOInterface
*/
protected $io;
/**
* @var YnabBudget
*/
protected $budget;
/**
* @var YnabDevice[]
*/
protected $devices;
/**
* @var bool If device was found and full sync is required
*/
protected $isFullSyncRequired = false;
/**
* @var YnabTransaction[]
*/
protected $transactions;
/**
* If the client is fully initialized
*
* @var bool
*/
protected $isInitialized = false;
/**
* YnabClient constructor.
*
* @param $budgetFilepath string File path to YANB4 budget file .ynab4
* @param null $deviceGUID string device GUID to push/pull changes to/from
*/
public function __construct($budgetFilepath, $io, $deviceGUID = null)
{
$this->io = $io;
$this->budgetFilepath = $budgetFilepath;
$this->deviceGUID = $deviceGUID;
$this->transactions = [];
}
/**
* Read budget definition file and create new device if necessary
*/
public function initialize()
{
if ($this->isInitialized) {
return;
}
/**
* Load budget file
*/
$repo = new YnabBudgetRepository($this->budgetFilepath, $this->io);
$this->budget = $repo->read();
/**
* Load devices definition files
*/
$repo = new YnabDeviceRepository($this->budget, $this->io);
$this->devices = $repo->read();
/**
* Identify device short ID
*/
foreach ($this->devices as $device)
{
if ($device->getDeviceGUID() == $this->deviceGUID) {
$this->deviceShortId = $device->getShortDeviceId();
break;
}
}
/**
* Cold not identify device short id, create a new device file
*/
if (null === $this->deviceShortId) {
$this->createDevice();
}
$this->isInitialized = true;
}
/**
* Pull changes from other devices
*/
public function pull()
{
if (!$this->isInitialized) {
$this->initialize();
}
if ($this->isFullSyncRequired) {
$this->sync();
}
$repo = new YnabDiffRepository($this->devices, $this->getDevice()->getKnowledge(), $this->io);
$budgetFull = $repo->read();
$this->getDevice()->setKnowledge($budgetFull->getKnowledge());
$this->transactions = array_merge($this->transactions, $budgetFull->getTransactions());
return true;
}
/**
* Push changes for current device
*/
public function push()
{
if (!$this->isInitialized) {
$this->initialize();
}
if ($this->isFullSyncRequired) {
throw new \RuntimeException('Full sync is required before pushing changes.');
}
$diff = new YnabDiff();
$diff->setStartVersion($this->getDevice()->getKnowledge());
$diff->setPublishTime(date('D M j H:i:s \G\M\TO Y'));
$diff->setDeviceGUID($this->getDevice()->getDeviceGUID());
$diff->setShortDeviceId($this->getDevice()->getShortDeviceId());
$utils = new YnabVersionUtils();
$knowledge = $this->getDevice()->getKnowledge();
if (!isset($knowledge[$this->getDevice()->getShortDeviceId()])) {
throw new \RuntimeException(sprintf('Could not find knowledge version for device %d', $this->getDevice()->getShortDeviceId()));
}
$items = [];
foreach ($this->transactions as $transaction) {
if (null == $transaction->getEntityVersion()) {
/**
* Create new version
*/
$increment = $knowledge[$this->getDevice()->getShortDeviceId()]->getIncrement() + 1;
$version = new YnabVersion($this->getDevice()->getShortDeviceId(), $increment);
$transaction->setEntityVersion($version);
$knowledge[$this->getDevice()->getShortDeviceId()] = $version;
}
else {
/**
* Skip this transaction
*/
if ($utils->isNewKnowledge($transaction->getEntityVersion(), $this->getDevice()->getKnowledge())) {
$knowledge = $utils->mergeKnowledge($knowledge, [ $transaction->getEntityVersion() ]);
}
else {
continue;
}
}
$items[] = $transaction;
}
$diff->setItems($items);
$diff->setEndVersion($knowledge);
$this->getDevice()->setKnowledge($knowledge);
// Mark other devices as not having full knowledge
if (count($items)) {
foreach ($this->devices as $device) {
if ($device->getDeviceGUID() != $this->getDevice()->getDeviceGUID()) {
$device->setHasFullKnowledge(false);
}
}
}
$repo = new YnabDiffRepository($this->devices, $this->getDevice()->getKnowledge(), $this->io);
return $repo->write($diff);
}
/**
* Commit device related information to YNAB4 database
*/
public function commit()
{
$repo = new YnabDeviceRepository($this->budget, $this->io);
$success = true;
foreach ($this->devices as $device) {
if (!$repo->write($device)) {
$success = false;
}
}
return $success;
}
/**
* Read full knowledge from a device
*/
public function sync()
{
// find device that has the full database
$device = null;
foreach ($this->devices as $dev) {
if ($dev->getHasFullKnowledge()) {
$device = $dev;
break;
}
}
if (null === $device) {
throw new \RuntimeException('Could not find any device that has the full knowledge');
}
/**
* Read content of the full budget file
*/
$repo = new YnabBudgetFullRepository($device, $this->io);
$budgetFull = $repo->read();
$this->transactions = array_merge($this->transactions, $budgetFull->getTransactions());
/**
* Merge full budget history knowledge with device knowledge
*/
$versionUtils = new YnabVersionUtils();
$this->getDevice()->setKnowledge(
$versionUtils->mergeKnowledge($device->getKnowledgeInFullBudgetFile(), $this->getDevice()->getKnowledge())
);
return true;
}
/**
* @return YnabTransaction[]
*/
public function getTransactions()
{
return $this->transactions;
}
/**
* @param YnabTransaction[] $transactions
*/
public function setTransactions($transactions)
{
$this->transactions = $transactions;
}
/**
* Create new device definition
*/
protected function createDevice()
{
$all = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$this->deviceShortId = null;
$devices = array_keys($this->devices);
for ($i = 0; $i < strlen($all); $i++) {
if (!in_array($all[$i], $devices)) {
$this->deviceShortId = $all[$i];
break;
}
}
if (null === $this->deviceShortId) {
throw new \RuntimeException('Could not find a available device short id');
}
$version = new YnabVersion($this->deviceShortId, 0);
$this->deviceGUID = strtoupper(\UUID::generate(\UUID::UUID_TIME, \UUID::FMT_STRING, "ABCDEF"));
$device = new YnabDevice();
$device->setYNABVersion('MTools');
$device->setDeviceGUID($this->deviceGUID);
$device->setDeviceType('Web');
$device->setFormatVersion('1.2');
$device->setFriendlyName('MTools');
$device->setHasFullKnowledge(false);
$device->setHighestDataVersionImported('4.2');
$device->setKnowledge([
$version->getShortId() => $version
]);
$device->setKnowledgeInFullBudgetFile(null);
$device->setLastDataVersionFullyKnown('4.2');
$device->setShortDeviceId($this->deviceShortId);
$device->setDataFullPath($this->budgetFilepath . '/' . $this->deviceGUID);
$this->devices[$this->deviceShortId] = $device;
$this->isFullSyncRequired = true;
}
/**
* Get current device
* @return YnabDevice
*/
public function getDevice()
{
if (!isset($this->devices[$this->deviceShortId])) {
throw new \RuntimeException(sprintf('Could not find device %d in the available device list', $this->deviceShortId));
}
return $this->devices[$this->deviceShortId];
}
}