Test Failed
Push — master ( 72daf6...986bed )
by Бабичев
03:30
created

HasWallets::wallets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\Wallet\Traits;
4
5
use Bavix\Wallet\Models\Wallet as WalletModel;
6
use Illuminate\Database\Eloquent\Relations\MorphMany;
7
use Illuminate\Support\Collection;
8
use function array_key_exists;
9
use function config;
10
11
/**
12
 * Trait HasWallets
13
 * To use a trait, you must add HasWallet trait.
14
 *
15
 * @package Bavix\Wallet\Traits
16
 *
17
 * @property-read Collection|WalletModel[] $wallets
18
 */
19
trait HasWallets
20
{
21
22
    /**
23
     * The variable is used for the cache, so as not to request wallets many times.
24
     * WalletProxy keeps the money wallets in the memory to avoid errors when you
25
     * purchase/transfer, etc.
26
     *
27
     * @var array
28
     */
29
    private $_wallets = [];
30
31
    /**
32
     * @var bool
33
     */
34
    private $_loadedWallets;
35
36
    /**
37
     * Get wallet by slug
38
     *
39
     *  $user->wallet->balance // 200
40
     *  or short recording $user->balance; // 200
41
     *
42
     *  $defaultSlug = config('wallet.wallet.default.slug');
43
     *  $user->getWallet($defaultSlug)->balance; // 200
44
     *
45
     *  $user->getWallet('usd')->balance; // 50
46
     *  $user->getWallet('rub')->balance; // 100
47
     *
48
     * @param string $slug
49
     * @return WalletModel|null
50
     */
51
    public function getWallet(string $slug): ?WalletModel
52
    {
53
        if (!$this->_loadedWallets && $this->relationLoaded('wallets')) {
0 ignored issues
show
Bug introduced by
It seems like relationLoaded() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

53
        if (!$this->_loadedWallets && $this->/** @scrutinizer ignore-call */ relationLoaded('wallets')) {
Loading history...
54
            $this->_loadedWallets = true;
55
            $wallets = $this->getRelation('wallets');
0 ignored issues
show
Bug introduced by
It seems like getRelation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

55
            /** @scrutinizer ignore-call */ 
56
            $wallets = $this->getRelation('wallets');
Loading history...
56
            foreach ($wallets as $wallet) {
57
                $this->_wallets[$wallet->slug] = $wallet;
58
            }
59
        }
60
61
        if (!array_key_exists($slug, $this->_wallets)) {
62
            $this->_wallets[$slug] = $this->wallets()
63
                ->where('slug', $slug)
64
                ->first();
65
        }
66
67
        return $this->_wallets[$slug];
68
    }
69
70
    /**
71
     * method of obtaining all wallets
72
     *
73
     * @return MorphMany
74
     */
75
    public function wallets(): MorphMany
76
    {
77
        return $this->morphMany(config('wallet.wallet.model'), 'holder');
0 ignored issues
show
Bug introduced by
It seems like morphMany() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        return $this->/** @scrutinizer ignore-call */ morphMany(config('wallet.wallet.model'), 'holder');
Loading history...
78
    }
79
80
    /**
81
     * @param array $data
82
     * @return WalletModel
83
     */
84
    public function createWallet(array $data): WalletModel
85
    {
86
        /**
87
         * Create a default wallet
88
         */
89
        $this->getBalanceAttribute();
0 ignored issues
show
Bug introduced by
It seems like getBalanceAttribute() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

89
        $this->/** @scrutinizer ignore-call */ 
90
               getBalanceAttribute();
Loading history...
90
91
        /**
92
         * @var WalletModel $wallet
93
         */
94
        $wallet = $this->wallets()->create($data);
95
        $this->_wallets[$wallet->slug] = $wallet;
96
97
        return $wallet;
98
    }
99
100
}
101