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

Wallet   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 55.56%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 68
ccs 10
cts 18
cp 0.5556
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A holder() 0 3 1
A setNameAttribute() 0 4 1
A getTable() 0 7 2
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\MorphTo;
11
use Illuminate\Support\Str;
12
13
/**
14
 * Class Wallet
15
 * @package Bavix\Wallet\Models
16
 * @property string $slug
17
 * @property int $balance
18
 * @property \Bavix\Wallet\Interfaces\Wallet $holder
19
 */
20
class Wallet extends Model implements Customer, WalletFloat
21
{
22
23
    use CanBePaidFloat;
0 ignored issues
show
introduced by
The trait Bavix\Wallet\Traits\CanBePaidFloat requires some properties which are not provided by Bavix\Wallet\Models\Wallet: $amount, $deposit
Loading history...
24
25
    /**
26
     * @var array
27
     */
28
    protected $fillable = [
29
        'holder_type',
30
        'holder_id',
31
        'name',
32
        'slug',
33
        'description',
34
        'balance',
35
    ];
36
37
    /**
38
     * @var array
39
     */
40
    protected $casts = [
41
        'balance' => 'int',
42
    ];
43
44
    /**
45
     * @return string
46
     */
47 30
    public function getTable(): string
48
    {
49 30
        if (!$this->table) {
50 30
            $this->table = \config('wallet.wallet.table');
51
        }
52
53 30
        return parent::getTable();
54
    }
55
56
    /**
57
     * @param string $name
58
     * @return void
59
     */
60 28
    public function setNameAttribute(string $name): void
61
    {
62 28
        $this->attributes['name'] = $name;
63 28
        $this->attributes['slug'] = Str::slug($name);
64 28
    }
65
66
    /**
67
     * @return bool
68
     */
69
    public function calculateBalance(): bool
70
    {
71
        $balance = $this->transactions()
72
            ->where('wallet_id', $this->getKey())
73
            ->where('confirmed', true)
74
            ->sum('amount');
75
76
        WalletProxy::set($this->getKey(), $balance);
77
        $this->attributes['balance'] = $balance;
78
79
        return $this->save();
80
    }
81
82
    /**
83
     * @return MorphTo
84
     */
85 5
    public function holder(): MorphTo
86
    {
87 5
        return $this->morphTo();
88
    }
89
90
}
91