1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Services; |
4
|
|
|
|
5
|
|
|
use Bavix\Wallet\Exceptions\BalanceIsEmpty; |
6
|
|
|
use Bavix\Wallet\Exceptions\InsufficientFunds; |
7
|
|
|
use Bavix\Wallet\Interfaces\Wallet; |
8
|
|
|
use Bavix\Wallet\Models\Transaction; |
9
|
|
|
use Bavix\Wallet\Models\Wallet as WalletModel; |
10
|
|
|
use Bavix\Wallet\Objects\Bring; |
11
|
|
|
use Bavix\Wallet\Objects\Operation; |
12
|
|
|
use Bavix\Wallet\Traits\HasWallet; |
13
|
|
|
use Illuminate\Support\Facades\DB; |
14
|
|
|
use function app; |
15
|
|
|
use function compact; |
16
|
|
|
|
17
|
|
|
class CommonService |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Wallet $wallet |
22
|
|
|
* @param int $amount |
23
|
|
|
* @return void |
24
|
|
|
* @throws BalanceIsEmpty |
25
|
|
|
* @throws InsufficientFunds |
26
|
|
|
*/ |
27
|
34 |
|
public function verifyWithdraw(Wallet $wallet, int $amount): void |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var HasWallet $wallet |
31
|
|
|
*/ |
32
|
34 |
|
if ($amount && !$wallet->balance) { |
33
|
14 |
|
throw new BalanceIsEmpty(trans('wallet::errors.wallet_empty')); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
28 |
|
if (!$wallet->canWithdraw($amount)) { |
37
|
1 |
|
throw new InsufficientFunds(trans('wallet::errors.insufficient_funds')); |
|
|
|
|
38
|
|
|
} |
39
|
27 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Create Operation with DB::transaction |
43
|
|
|
* |
44
|
|
|
* @param Wallet $self |
45
|
|
|
* @param Operation[] $transactions |
46
|
|
|
* @return Transaction[] |
47
|
|
|
*/ |
48
|
30 |
|
public function enforce(Wallet $self, array $transactions): array |
49
|
|
|
{ |
50
|
|
|
return DB::transaction(function () use ($self, $transactions) { |
51
|
30 |
|
return $this->multiOperation($self, $transactions); |
52
|
30 |
|
}); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create Operation without DB::transaction |
57
|
|
|
* |
58
|
|
|
* @param Wallet $self |
59
|
|
|
* @param array $operations |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
30 |
|
public function multiOperation(Wallet $self, array $operations): array |
63
|
|
|
{ |
64
|
30 |
|
$amount = 0; |
65
|
30 |
|
$objects = []; |
66
|
30 |
|
foreach ($operations as $operation) { |
67
|
30 |
|
if ($operation->isConfirmed()) { |
68
|
30 |
|
$amount += $operation->getAmount(); |
69
|
|
|
} |
70
|
|
|
|
71
|
30 |
|
$objects[] = $operation |
72
|
30 |
|
->setWallet($self) |
73
|
30 |
|
->create(); |
74
|
|
|
} |
75
|
|
|
|
76
|
30 |
|
$this->addBalance($self, $amount); |
77
|
30 |
|
return $objects; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create Bring with DB::transaction |
82
|
|
|
* |
83
|
|
|
* @param Bring[] $brings |
84
|
|
|
* @return array |
85
|
|
|
* @throws |
86
|
|
|
*/ |
87
|
18 |
|
public function assemble(array $brings): array |
88
|
|
|
{ |
89
|
|
|
return DB::transaction(function () use ($brings) { |
90
|
18 |
|
return $this->multiBrings($brings); |
91
|
18 |
|
}); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Create Bring without DB::transaction |
96
|
|
|
* |
97
|
|
|
* @param array $brings |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
18 |
|
public function multiBrings(array $brings): array |
101
|
|
|
{ |
102
|
18 |
|
$objects = []; |
103
|
18 |
|
foreach ($brings as $bring) { |
104
|
18 |
|
$objects[] = $bring->create(); |
105
|
|
|
} |
106
|
|
|
|
107
|
18 |
|
return $objects; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param Wallet $wallet |
112
|
|
|
* @param int $amount |
113
|
|
|
* @return bool |
114
|
|
|
* @throws |
115
|
|
|
*/ |
116
|
30 |
|
public function addBalance(Wallet $wallet, int $amount): bool |
117
|
|
|
{ |
118
|
|
|
/** |
119
|
|
|
* @var ProxyService $proxy |
120
|
|
|
* @var WalletModel $wallet |
121
|
|
|
*/ |
122
|
30 |
|
$proxy = app(ProxyService::class); |
123
|
30 |
|
$balance = $wallet->balance + $amount; |
124
|
30 |
|
if ($proxy->has($wallet->getKey())) { |
|
|
|
|
125
|
30 |
|
$balance = $proxy->get($wallet->getKey()) + $amount; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
30 |
|
$result = $wallet->update(compact('balance')); |
129
|
30 |
|
$proxy->set($wallet->getKey(), $balance); |
|
|
|
|
130
|
|
|
|
131
|
30 |
|
return $result; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|