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