1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bavix\Wallet\Objects; |
4
|
|
|
|
5
|
|
|
use Bavix\Wallet\Interfaces\Wallet; |
6
|
|
|
use Bavix\Wallet\Models\Transaction; |
7
|
|
|
use Bavix\Wallet\Models\Transfer; |
8
|
|
|
use Ramsey\Uuid\Uuid; |
9
|
|
|
use function abs; |
10
|
|
|
|
11
|
|
|
class Bring |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $status; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Wallet |
21
|
|
|
*/ |
22
|
|
|
protected $from; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Wallet |
26
|
|
|
*/ |
27
|
|
|
protected $to; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Transaction |
31
|
|
|
*/ |
32
|
|
|
protected $deposit; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Transaction |
36
|
|
|
*/ |
37
|
|
|
protected $withdraw; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $uuid; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Bring constructor. |
46
|
|
|
* @throws |
47
|
|
|
*/ |
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
$this->uuid = Uuid::uuid4()->toString(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getStatus(): string |
57
|
|
|
{ |
58
|
|
|
return $this->status; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $status |
63
|
|
|
* @return static |
64
|
|
|
*/ |
65
|
|
|
public function setStatus(string $status): self |
66
|
|
|
{ |
67
|
|
|
$this->status = $status; |
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return Wallet |
73
|
|
|
*/ |
74
|
|
|
public function getFrom(): Wallet |
75
|
|
|
{ |
76
|
|
|
return $this->from; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param Wallet $from |
81
|
|
|
* @return static |
82
|
|
|
*/ |
83
|
|
|
public function setFrom(Wallet $from): self |
84
|
|
|
{ |
85
|
|
|
$this->from = $from; |
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Wallet |
91
|
|
|
*/ |
92
|
|
|
public function getTo(): Wallet |
93
|
|
|
{ |
94
|
|
|
return $this->to; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param Wallet $to |
99
|
|
|
* @return static |
100
|
|
|
*/ |
101
|
|
|
public function setTo(Wallet $to): self |
102
|
|
|
{ |
103
|
|
|
$this->to = $to; |
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return Transaction |
109
|
|
|
*/ |
110
|
|
|
public function getDeposit(): Transaction |
111
|
|
|
{ |
112
|
|
|
return $this->deposit; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param Transaction $deposit |
117
|
|
|
* @return static |
118
|
|
|
*/ |
119
|
|
|
public function setDeposit(Transaction $deposit): self |
120
|
|
|
{ |
121
|
|
|
$this->deposit = $deposit; |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return Transaction |
127
|
|
|
*/ |
128
|
|
|
public function getWithdraw(): Transaction |
129
|
|
|
{ |
130
|
|
|
return $this->withdraw; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Transaction $withdraw |
135
|
|
|
* @return static |
136
|
|
|
*/ |
137
|
|
|
public function setWithdraw(Transaction $withdraw): self |
138
|
|
|
{ |
139
|
|
|
$this->withdraw = $withdraw; |
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
|
|
public function getUuid(): string |
147
|
|
|
{ |
148
|
|
|
return $this->uuid; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @return Transfer |
153
|
|
|
* @throws |
154
|
|
|
*/ |
155
|
|
|
public function create(): Transfer |
156
|
|
|
{ |
157
|
|
|
return app(Transfer::class) |
158
|
|
|
->create($this->toArray()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return array |
163
|
|
|
* @throws |
164
|
|
|
*/ |
165
|
|
|
public function toArray(): array |
166
|
|
|
{ |
167
|
|
|
return [ |
168
|
|
|
'status' => $this->getStatus(), |
169
|
|
|
'deposit_id' => $this->getDeposit()->getKey(), |
170
|
|
|
'withdraw_id' => $this->getWithdraw()->getKey(), |
171
|
|
|
'from_type' => $this->getFrom()->getMorphClass(), |
|
|
|
|
172
|
|
|
'from_id' => $this->getFrom()->getKey(), |
|
|
|
|
173
|
|
|
'to_type' => $this->getTo()->getMorphClass(), |
174
|
|
|
'to_id' => $this->getTo()->getKey(), |
175
|
|
|
'fee' => abs($this->getWithdraw()->amount) - abs($this->getDeposit()->amount), |
176
|
|
|
'uuid' => $this->getUuid(), |
177
|
|
|
]; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
} |
181
|
|
|
|