Completed
Pull Request — master (#39)
by Бабичев
06:05
created

HasWallets::createWallet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 16
ccs 6
cts 6
cp 1
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
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
     * @var bool
31
     */
32
    private $_loadedWallets = false;
33
34
    /**
35
     * Get wallet by slug
36
     *
37
     *  $user->wallet->balance // 200
38
     *  or short recording $user->balance; // 200
39
     *
40
     *  $defaultSlug = config('wallet.wallet.default.slug');
41
     *  $user->getWallet($defaultSlug)->balance; // 200
42
     *
43
     *  $user->getWallet('usd')->balance; // 50
44
     *  $user->getWallet('rub')->balance; // 100
45
     *
46
     * @param string $slug
47
     * @return WalletModel|null
48
     */
49 2
    public function getWallet(string $slug): ?WalletModel
50
    {
51 2
        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

51
        if (!$this->_loadedWallets && $this->/** @scrutinizer ignore-call */ relationLoaded('wallets')) {
Loading history...
52 1
            $this->_loadedWallets = true;
53 1
            $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

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

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

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