|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Bavix\Wallet\Interfaces\Product; |
|
6
|
|
|
use Bavix\Wallet\Interfaces\Wallet; |
|
7
|
|
|
use Bavix\Wallet\Models\Transfer; |
|
8
|
|
|
use Bavix\Wallet\Objects\Bring; |
|
9
|
|
|
use Bavix\Wallet\Services\CommonService; |
|
10
|
|
|
use Bavix\Wallet\Services\WalletService; |
|
11
|
|
|
use Illuminate\Support\Facades\DB; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
use function app; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Trait HasGift |
|
17
|
|
|
* @package Bavix\Wallet\Traits |
|
18
|
|
|
* |
|
19
|
|
|
* This trait should be used with the trait HasWallet. |
|
20
|
|
|
*/ |
|
21
|
|
|
trait HasGift |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Give the goods safely. |
|
26
|
|
|
* |
|
27
|
|
|
* @param Wallet $to |
|
28
|
|
|
* @param Product $product |
|
29
|
|
|
* @param bool $force |
|
30
|
|
|
* @return Transfer|null |
|
31
|
|
|
*/ |
|
32
|
|
|
public function safeGift(Wallet $to, Product $product, bool $force = null): ?Transfer |
|
33
|
|
|
{ |
|
34
|
|
|
try { |
|
35
|
|
|
return $this->gift($to, $product, $force); |
|
36
|
|
|
} catch (Throwable $throwable) { |
|
37
|
|
|
return null; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* From this moment on, each user (wallet) can give |
|
43
|
|
|
* the goods to another user (wallet). |
|
44
|
|
|
* This functionality can be organized for gifts. |
|
45
|
|
|
* |
|
46
|
|
|
* @param Wallet $to |
|
47
|
|
|
* @param Product $product |
|
48
|
|
|
* @param bool $force |
|
49
|
|
|
* @return Transfer |
|
50
|
|
|
*/ |
|
51
|
|
|
public function gift(Wallet $to, Product $product, bool $force = null): Transfer |
|
52
|
|
|
{ |
|
53
|
|
|
/** |
|
54
|
|
|
* Who's giving? Let's call him Santa Claus |
|
55
|
|
|
* @var Wallet $santa |
|
56
|
|
|
*/ |
|
57
|
|
|
$santa = $this; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Unfortunately, |
|
61
|
|
|
* I think it is wrong to make the "assemble" method public. |
|
62
|
|
|
* That's why I address him like this! |
|
63
|
|
|
*/ |
|
64
|
|
|
return DB::transaction(static function() use ($santa, $to, $product, $force) { |
|
65
|
|
|
$amount = $product->getAmountProduct(); |
|
66
|
|
|
$meta = $product->getMetaProduct(); |
|
67
|
|
|
$fee = app(WalletService::class) |
|
68
|
|
|
->fee($product, $amount); |
|
69
|
|
|
|
|
70
|
|
|
$commonService = app(CommonService::class); |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Santa pays taxes |
|
74
|
|
|
*/ |
|
75
|
|
|
if (!$force) { |
|
|
|
|
|
|
76
|
|
|
$commonService->verifyWithdraw($santa, $amount); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$withdraw = $commonService->forceWithdraw($santa, $amount + $fee, $meta); |
|
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
$deposit = $commonService->deposit($product, $amount, $meta); |
|
82
|
|
|
|
|
83
|
|
|
$from = app(WalletService::class) |
|
84
|
|
|
->getWallet($to); |
|
85
|
|
|
|
|
86
|
|
|
$transfers = $commonService->assemble([ |
|
87
|
|
|
(new Bring()) |
|
88
|
|
|
->setStatus(Transfer::STATUS_GIFT) |
|
89
|
|
|
->setDeposit($deposit) |
|
90
|
|
|
->setWithdraw($withdraw) |
|
91
|
|
|
->setFrom($from) |
|
92
|
|
|
->setTo($product) |
|
93
|
|
|
]); |
|
94
|
|
|
|
|
95
|
|
|
return current($transfers); |
|
96
|
|
|
}); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* to give force) |
|
101
|
|
|
* |
|
102
|
|
|
* @param Wallet $to |
|
103
|
|
|
* @param Product $product |
|
104
|
|
|
* @return Transfer |
|
105
|
|
|
*/ |
|
106
|
|
|
public function forceGift(Wallet $to, Product $product): Transfer |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->gift($to, $product, true); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
If an expression can have both
false, andnullas possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.