Skip to content

Commit 319c863

Browse files
committed
micro optimize
1 parent 54da5cd commit 319c863

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/Traits/HasWallets.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ trait HasWallets
2323
/**
2424
* The variable is used for the cache, so as not to request wallets many times. WalletProxy keeps the money wallets
2525
* in the memory to avoid errors when you purchase/transfer, etc.
26+
*
27+
* @var WalletModel[]
2628
*/
2729
private array $_wallets = [];
2830

29-
private bool $_loadedWallets = false;
30-
3131
/**
3232
* Get wallet by slug.
3333
*
@@ -59,16 +59,19 @@ public function getWallet(string $slug): ?WalletModel
5959
*/
6060
public function getWalletOrFail(string $slug): WalletModel
6161
{
62-
if (!$this->_loadedWallets && $this->relationLoaded('wallets')) {
63-
$this->_loadedWallets = true;
64-
$wallets = $this->getRelation('wallets');
65-
foreach ($wallets as $wallet) {
62+
if ($this->_wallets === [] && $this->relationLoaded('wallets')) {
63+
/** @var WalletModel $wallet */
64+
foreach ($this->getRelation('wallets') as $wallet) {
65+
$wallet->setRelation('holder', $this);
6666
$this->_wallets[$wallet->slug] = $wallet;
6767
}
6868
}
6969

7070
if (!array_key_exists($slug, $this->_wallets)) {
71-
$this->_wallets[$slug] = app(WalletServiceInterface::class)->getBySlug($this, $slug);
71+
$wallet = app(WalletServiceInterface::class)->getBySlug($this, $slug);
72+
$wallet->setRelation('holder', $this);
73+
74+
$this->_wallets[$slug] = $wallet;
7275
}
7376

7477
return $this->_wallets[$slug];
@@ -86,6 +89,7 @@ public function createWallet(array $data): WalletModel
8689
{
8790
$wallet = app(WalletServiceInterface::class)->create($this, $data);
8891
$this->_wallets[$wallet->slug] = $wallet;
92+
$wallet->setRelation('holder', $this);
8993

9094
return $wallet;
9195
}

0 commit comments

Comments
 (0)