Completed
Push — master ( 1d66b1...2c0009 )
by Бабичев
07:00 queued 12s
created

Wallet::holder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Wallet\Models;
4
5
use Bavix\Wallet\Interfaces\Confirmable;
6
use Bavix\Wallet\Interfaces\Customer;
7
use Bavix\Wallet\Interfaces\Exchangeable;
8
use Bavix\Wallet\Interfaces\WalletFloat;
9
use Bavix\Wallet\Services\WalletService;
10
use Bavix\Wallet\Traits\CanConfirm;
11
use Bavix\Wallet\Traits\CanExchange;
12
use Bavix\Wallet\Traits\CanPayFloat;
13
use Bavix\Wallet\Traits\HasGift;
14
use Illuminate\Database\Eloquent\Model;
15
use Illuminate\Database\Eloquent\Relations\MorphTo;
16
use Illuminate\Support\Str;
17
use function app;
18
use function array_key_exists;
19
use function config;
20
21
/**
22
 * Class Wallet
23
 * @package Bavix\Wallet\Models
24
 * @property string $slug
25
 * @property int $balance
26
 * @property int $decimal_places
27
 * @property \Bavix\Wallet\Interfaces\Wallet $holder
28
 * @property-read string $currency
29
 */
30
class Wallet extends Model implements Customer, WalletFloat, Confirmable, Exchangeable
31
{
32
33
    use CanConfirm;
0 ignored issues
show
introduced by
The trait Bavix\Wallet\Traits\CanConfirm requires some properties which are not provided by Bavix\Wallet\Models\Wallet: $confirmed, $amount, $type, $wallet_id
Loading history...
34
    use CanExchange;
35
    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, $withdraw, $status, $deposit
Loading history...
36
    use HasGift;
37
38
    /**
39
     * @var array
40
     */
41
    protected $fillable = [
42
        'holder_type',
43
        'holder_id',
44
        'name',
45
        'slug',
46
        'description',
47
        'balance',
48
        'decimal_places',
49
    ];
50
51
    /**
52
     * @var array
53
     */
54
    protected $casts = [
55
        'balance' => 'int',
56
        'decimal_places' => 'int',
57
    ];
58
59
    /**
60
     * @return string
61
     */
62 61
    public function getTable(): string
63
    {
64 61
        if (!$this->table) {
65 61
            $this->table = config('wallet.wallet.table');
66
        }
67
68 61
        return parent::getTable();
69
    }
70
71
    /**
72
     * @param string $name
73
     * @return void
74
     */
75 59
    public function setNameAttribute(string $name): void
76
    {
77 59
        $this->attributes['name'] = $name;
78
79
        /**
80
         * Must be updated only if the model does not exist
81
         *  or the slug is empty
82
         */
83 59
        if (!$this->exists && !array_key_exists('slug', $this->attributes)) {
84 59
            $this->attributes['slug'] = Str::slug($name);
85
        }
86 59
    }
87
88
    /**
89
     * @return bool
90
     */
91 8
    public function refreshBalance(): bool
92
    {
93 8
        return app(WalletService::class)->refresh($this);
94
    }
95
96
    /**
97
     * @return int
98
     */
99 8
    public function getAvailableBalance(): int
100
    {
101 8
        return $this->transactions()
102 8
            ->where('wallet_id', $this->getKey())
103 8
            ->where('confirmed', true)
104 8
            ->sum('amount');
105
    }
106
107
    /**
108
     * @return MorphTo
109
     */
110 44
    public function holder(): MorphTo
111
    {
112 44
        return $this->morphTo();
113
    }
114
115
    /**
116
     * @return string
117
     */
118 3
    public function getCurrencyAttribute(): string
119
    {
120 3
        $currencies = config('wallet.currencies', []);
121 3
        return $currencies[$this->slug] ?? Str::upper($this->slug);
122
    }
123
124
}
125