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

Wallet   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 55
ccs 4
cts 12
cp 0.3333
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A holder() 0 3 1
A getTable() 0 7 2
A calculateBalance() 0 8 1
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