|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Bavix\Wallet\Interfaces\Wallet; |
|
6
|
|
|
use Bavix\Wallet\Models\Transaction; |
|
7
|
|
|
use Bavix\Wallet\Models\Transfer; |
|
8
|
|
|
use Bavix\Wallet\Models\Wallet as WalletModel; |
|
9
|
|
|
use Bavix\Wallet\Objects\Bring; |
|
10
|
|
|
use Bavix\Wallet\Objects\Operation; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
12
|
|
|
use Ramsey\Uuid\Uuid; |
|
13
|
|
|
|
|
14
|
|
|
class CommonService |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param Wallet $self |
|
19
|
|
|
* @param Operation[] $transactions |
|
20
|
|
|
* @return Transaction[] |
|
21
|
|
|
*/ |
|
22
|
28 |
|
public function enforce(Wallet $self, array $transactions): array |
|
23
|
|
|
{ |
|
24
|
28 |
|
$objects = []; |
|
25
|
28 |
|
$amount = 0; |
|
26
|
|
|
|
|
27
|
28 |
|
foreach ($transactions as $transaction) { |
|
28
|
28 |
|
if ($transaction->isConfirmed()) { |
|
29
|
28 |
|
$amount += $transaction->getAmount(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
28 |
|
$objects[] = $transaction->create($self); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
28 |
|
$this->addBalance($self, $amount); |
|
36
|
28 |
|
return $objects; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* this method adds a new transfer to the transfer table |
|
42
|
|
|
* |
|
43
|
|
|
* @param Wallet $wallet |
|
44
|
|
|
* @param Transaction $withdraw |
|
45
|
|
|
* @param Transaction $deposit |
|
46
|
|
|
* @param string $status |
|
47
|
|
|
* @return Transfer |
|
48
|
|
|
* @throws |
|
49
|
|
|
*/ |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param Bring[] $brings |
|
53
|
|
|
* @return array |
|
54
|
|
|
* @throws |
|
55
|
|
|
*/ |
|
56
|
17 |
|
public function assemble(array $brings): array |
|
57
|
|
|
{ |
|
58
|
17 |
|
$objects = []; |
|
59
|
17 |
|
foreach ($brings as $bring) { |
|
60
|
17 |
|
$objects[] = $bring->create(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
17 |
|
return $objects; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param Wallet $wallet |
|
69
|
|
|
* @param int $amount |
|
70
|
|
|
* @return bool |
|
71
|
|
|
* @throws |
|
72
|
|
|
*/ |
|
73
|
28 |
|
public function addBalance(Wallet $wallet, int $amount): bool |
|
74
|
|
|
{ |
|
75
|
|
|
/** |
|
76
|
|
|
* @var ProxyService $proxy |
|
77
|
|
|
* @var \Bavix\Wallet\Models\Wallet $wallet |
|
78
|
|
|
*/ |
|
79
|
28 |
|
$proxy = \app(ProxyService::class); |
|
80
|
28 |
|
$balance = $wallet->balance; |
|
81
|
28 |
|
if ($proxy->has($wallet->getKey())) { |
|
|
|
|
|
|
82
|
28 |
|
$balance = $proxy->get($wallet->getKey()); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
28 |
|
$balance += $amount; |
|
86
|
28 |
|
if ($wallet->update(\compact('balance'))) { |
|
87
|
28 |
|
$proxy->set($wallet->getKey(), $balance); |
|
|
|
|
|
|
88
|
28 |
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|