Passed
Pull Request — master (#31)
by Бабичев
03:17
created

HasWallets   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Test Coverage

Coverage 85%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 79
ccs 17
cts 20
cp 0.85
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A wallets() 0 3 1
A createWallet() 0 16 2
A getWallet() 0 20 5
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
9
/**
10
 * Trait HasWallets
11
 * To use a trait, you must add HasWallet trait.
12
 *
13
 * @package Bavix\Wallet\Traits
14
 *
15
 * @property-read Collection|WalletModel[] $wallets
16
 */
17
trait HasWallets
18
{
19
20
    /**
21
     * The variable is used for the cache, so as not to request wallets many times.
22
     * WalletProxy keeps the money wallets in the memory to avoid errors when you
23
     * purchase/transfer, etc.
24
     *
25
     * @var array
26
     */
27
    private $_wallets = [];
28
29
    /**
30
     * Get wallet by slug
31
     *
32
     *  $user->wallet->balance // 200
33
     *  or short recording $user->balance; // 200
34
     *
35
     *  $defaultSlug = config('wallet.wallet.default.slug');
36
     *  $user->getWallet($defaultSlug)->balance; // 200
37
     *
38
     *  $user->getWallet('usd')->balance; // 50
39
     *  $user->getWallet('rub')->balance; // 100
40
     *
41
     * @param string $slug
42
     * @return WalletModel|null
43
     */
44 1
    public function getWallet(string $slug): ?WalletModel
45
    {
46 1
        if (\array_key_exists($slug, $this->_wallets)) {
47 1
            return $this->_wallets[$slug];
48
        }
49
50 1
        if ($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

50
        if ($this->/** @scrutinizer ignore-call */ relationLoaded('wallets')) {
Loading history...
51
            $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

51
            /** @scrutinizer ignore-call */ 
52
            $wallets = $this->getRelation('wallets');
Loading history...
52
            foreach ($wallets as $wallet) {
53
                $this->_wallets[$wallet->slug] = $wallet;
54
            }
55
        }
56
57 1
        if (!\array_key_exists($slug, $this->_wallets)) {
58 1
            $this->_wallets[$slug] = $this->wallets()
59 1
                ->where('slug', $slug)
60 1
                ->first();
61
        }
62
63 1
        return $this->_wallets[$slug];
64
    }
65
66
    /**
67
     * method of obtaining all wallets
68
     *
69
     * @return MorphMany
70
     */
71 10
    public function wallets(): MorphMany
72
    {
73 10
        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

73
        return $this->/** @scrutinizer ignore-call */ morphMany(config('wallet.wallet.model'), 'holder');
Loading history...
74
    }
75
76
    /**
77
     * @param array $data
78
     * @return WalletModel
79
     */
80 10
    public function createWallet(array $data): WalletModel
81
    {
82
        /**
83
         * Create a default wallet
84
         */
85 10
        $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

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