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

Wallet::getTable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
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 Illuminate\Database\Eloquent\Model;
9
10
/**
11
 * Class Wallet
12
 * @package Bavix\Wallet\Models
13
 * @property int $balance
14
 */
15
class Wallet extends Model implements Customer, WalletFloat
16
{
17
18
    use CanBePaidFloat;
19
20
    /**
21
     * @var array
22
     */
23
    protected $fillable = [
24
        'holder_type',
25
        'holder_id',
26
        'name',
27
        'slug',
28
        'description',
29
        'balance',
30
    ];
31
32
    /**
33
     * @var array
34
     */
35
    protected $casts = [
36
        'balance' => 'int',
37
    ];
38
39
    /**
40
     * @return string
41
     */
42 21
    public function getTable(): string
43
    {
44 21
        if (!$this->table) {
45 21
            $this->table = \config('wallet.wallet.table');
46
        }
47
48 21
        return parent::getTable();
49
    }
50
51
    /**
52
     * @return bool
53
     */
54
    public function calculateBalance(): bool
55
    {
56
        $this->attributes['balance'] = $this->transactions()
57
            ->where('wallet_id', $this->getKey())
58
            ->where('confirmed', true)
59
            ->sum('amount');
60
61
        return $this->save();
62
    }
63
64
}
65