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

Wallet   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 28.57%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 58
ccs 4
cts 14
cp 0.2857
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTable() 0 7 2
A holder() 0 3 1
A calculateBalance() 0 11 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 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 21
    public function getTable(): string
46
    {
47 21
        if (!$this->table) {
48 21
            $this->table = \config('wallet.wallet.table');
49
        }
50
51 21
        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