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

Wallet::holder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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