Completed
Pull Request — master (#14)
by Бабичев
02:43
created

Wallet::getTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Wallet\Models;
4
5
use Bavix\Wallet\Interfaces\Customer;
6
use Bavix\Wallet\Interfaces\WalletFloat;
7
use Bavix\Wallet\Traits\CanBePaidFloat;
8
use Bavix\Wallet\WalletProxy;
9
use Illuminate\Database\Eloquent\Model;
10
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
11
12
/**
13
 * Class Wallet
14
 * @package Bavix\Wallet\Models
15
 * @property int $balance
16
 * @property \Bavix\Wallet\Interfaces\Wallet $holder
17
 */
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
    public function getTable(): string
46
    {
47
        if (!$this->table) {
48
            $this->table = \config('wallet.wallet.table');
49
        }
50
51
        return parent::getTable();
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function calculateBalance(): bool
58
    {
59
        $balance = $this->transactions()
60
            ->where('wallet_id', $this->getKey())
61
            ->where('confirmed', true)
62
            ->sum('amount');
63
64
        WalletProxy::set($this->getKey(), $balance);
65
        $this->attributes['balance'] = $balance;
66
67
        return $this->save();
68
    }
69
70
    /**
71
     * @return BelongsToMany
72
     */
73
    public function holder(): BelongsToMany
74
    {
75
        return $this->belongsToMany('holder');
76
    }
77
78
}
79