1 | <?php |
||||||
2 | |||||||
3 | namespace Bavix\Wallet\Traits; |
||||||
4 | |||||||
5 | use function array_key_exists; |
||||||
6 | use Bavix\Wallet\Models\Wallet as WalletModel; |
||||||
7 | use function config; |
||||||
8 | use Illuminate\Database\Eloquent\Relations\MorphMany; |
||||||
9 | use Illuminate\Support\Collection; |
||||||
10 | |||||||
11 | /** |
||||||
12 | * Trait HasWallets |
||||||
13 | * To use a trait, you must add HasWallet trait. |
||||||
14 | * |
||||||
15 | * |
||||||
16 | * @property-read Collection|WalletModel[] $wallets |
||||||
17 | */ |
||||||
18 | trait HasWallets |
||||||
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; |
||||||
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 | 6 | public function getWallet(string $slug): ?WalletModel |
|||||
50 | { |
||||||
51 | 6 | if (! $this->_loadedWallets && $this->relationLoaded('wallets')) { |
|||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||||
52 | 1 | $this->_loadedWallets = true; |
|||||
53 | 1 | $wallets = $this->getRelation('wallets'); |
|||||
0 ignored issues
–
show
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
![]() |
|||||||
54 | 1 | foreach ($wallets as $wallet) { |
|||||
55 | 1 | $this->_wallets[$wallet->slug] = $wallet; |
|||||
56 | } |
||||||
57 | } |
||||||
58 | |||||||
59 | 6 | if (! array_key_exists($slug, $this->_wallets)) { |
|||||
60 | 5 | $this->_wallets[$slug] = $this->wallets() |
|||||
61 | 5 | ->where('slug', $slug) |
|||||
62 | 5 | ->first(); |
|||||
63 | } |
||||||
64 | |||||||
65 | 6 | return $this->_wallets[$slug]; |
|||||
66 | } |
||||||
67 | |||||||
68 | /** |
||||||
69 | * method of obtaining all wallets. |
||||||
70 | * |
||||||
71 | * @return MorphMany |
||||||
72 | */ |
||||||
73 | 22 | public function wallets(): MorphMany |
|||||
74 | { |
||||||
75 | 22 | return $this->morphMany(config('wallet.wallet.model', WalletModel::class), 'holder'); |
|||||
0 ignored issues
–
show
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
![]() |
|||||||
76 | } |
||||||
77 | |||||||
78 | /** |
||||||
79 | * @param array $data |
||||||
80 | * @return WalletModel |
||||||
81 | */ |
||||||
82 | 22 | public function createWallet(array $data): WalletModel |
|||||
83 | { |
||||||
84 | /** |
||||||
85 | * @var WalletModel $wallet |
||||||
86 | */ |
||||||
87 | 22 | $wallet = $this->wallets()->create($data); |
|||||
88 | 22 | $this->_wallets[$wallet->slug] = $wallet; |
|||||
89 | |||||||
90 | 22 | return $wallet; |
|||||
91 | } |
||||||
92 | |||||||
93 | /** |
||||||
94 | * The method checks the existence of the wallet. |
||||||
95 | * |
||||||
96 | * @param string $slug |
||||||
97 | * @return bool |
||||||
98 | */ |
||||||
99 | 2 | public function hasWallet(string $slug): bool |
|||||
100 | { |
||||||
101 | 2 | return (bool) $this->getWallet($slug); |
|||||
102 | } |
||||||
103 | } |
||||||
104 |