Completed
Pull Request — master (#39)
by Бабичев
06:05
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\CanPayFloat;
8
use Bavix\Wallet\Traits\HasGift;
9
use Bavix\Wallet\WalletProxy;
10
use Illuminate\Database\Eloquent\Model;
11
use Illuminate\Database\Eloquent\Relations\MorphTo;
12
use Illuminate\Support\Str;
13
14
/**
15
 * Class Wallet
16
 * @package Bavix\Wallet\Models
17
 * @property string $slug
18
 * @property int $balance
19
 * @property \Bavix\Wallet\Interfaces\Wallet $holder
20
 */
21
class Wallet extends Model implements Customer, WalletFloat
22
{
23
24
    use CanPayFloat;
0 ignored issues
show
introduced by
The trait Bavix\Wallet\Traits\CanPayFloat requires some properties which are not provided by Bavix\Wallet\Models\Wallet: $amount, $payable, $withdraw, $status, $deposit
Loading history...
25
    use HasGift;
26
27
    /**
28
     * @var array
29
     */
30
    protected $fillable = [
31
        'holder_type',
32
        'holder_id',
33
        'name',
34
        'slug',
35
        'description',
36
        'balance',
37
    ];
38
39
    /**
40
     * @var array
41
     */
42
    protected $casts = [
43
        'balance' => 'int',
44
    ];
45
46
    /**
47
     * @return string
48
     */
49 39
    public function getTable(): string
50
    {
51 39
        if (!$this->table) {
52 39
            $this->table = \config('wallet.wallet.table');
53
        }
54
55 39
        return parent::getTable();
56
    }
57
58
    /**
59
     * @param string $name
60
     * @return void
61
     */
62 37
    public function setNameAttribute(string $name): void
63
    {
64 37
        $this->attributes['name'] = $name;
65
66
        /**
67
         * Must be updated only if the model does not exist
68
         *  or the slug is empty
69
         */
70 37
        if (!$this->exists && !\array_key_exists('slug', $this->attributes)) {
71 37
            $this->attributes['slug'] = Str::slug($name);
72
        }
73 37
    }
74
75
    /**
76
     * @return bool
77
     * @deprecated
78
     * @see refreshBalance
79
     */
80 1
    public function calculateBalance(): bool
81
    {
82 1
        return $this->refreshBalance();
83
    }
84
85
    /**
86
     * @return bool
87
     */
88 1
    public function refreshBalance(): bool
89
    {
90 1
        $balance = $this->getAvailableBalance();
91 1
        WalletProxy::set($this->getKey(), $balance);
92 1
        $this->attributes['balance'] = $balance;
93
94 1
        return $this->save();
95
    }
96
97
    /**
98
     * @return int
99
     */
100 1
    public function getAvailableBalance(): int
101
    {
102 1
        return $this->transactions()
103 1
            ->where('wallet_id', $this->getKey())
104 1
            ->where('confirmed', true)
105 1
            ->sum('amount');
106
    }
107
108
    /**
109
     * @return MorphTo
110
     */
111 8
    public function holder(): MorphTo
112
    {
113 8
        return $this->morphTo();
114
    }
115
116
}
117