Total Complexity | 4 |
Total Lines | 59 |
Duplicated Lines | 0 % |
Coverage | 26.67% |
Changes | 0 |
1 | <?php |
||
18 | class Wallet extends Model implements Customer, WalletFloat |
||
19 | { |
||
20 | |||
21 | use CanBePaidFloat; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $fillable = [ |
||
27 | 'holder_type', |
||
28 | 'holder_id', |
||
29 | 'name', |
||
30 | 'slug', |
||
31 | 'description', |
||
32 | 'balance', |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $casts = [ |
||
39 | 'balance' => 'int', |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * @return string |
||
44 | */ |
||
45 | 21 | public function getTable(): string |
|
46 | { |
||
47 | 21 | if (!$this->table) { |
|
48 | 21 | $this->table = \config('wallet.wallet.table'); |
|
49 | } |
||
50 | |||
51 | 21 | return parent::getTable(); |
|
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function calculateBalance(): bool |
||
58 | { |
||
59 | $this->getBalanceAttribute(); |
||
60 | $balance = $this->transactions() |
||
61 | ->where('wallet_id', $this->getKey()) |
||
62 | ->where('confirmed', true) |
||
63 | ->sum('amount'); |
||
64 | |||
65 | WalletProxy::set($this->getKey(), $balance); |
||
66 | $this->attributes['balance'] = $balance; |
||
67 | |||
68 | return $this->save(); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return BelongsToMany |
||
73 | */ |
||
74 | public function holder(): BelongsToMany |
||
77 | } |
||
78 | |||
79 | } |
||
80 |