Andrew Wallo 6 mēnešus atpakaļ
vecāks
revīzija
58357001a8
1 mainītis faili ar 41 papildinājumiem un 15 dzēšanām
  1. 41
    15
      app/Casts/TransactionAmountCast.php

+ 41
- 15
app/Casts/TransactionAmountCast.php Parādīt failu

@@ -11,12 +11,42 @@ use UnexpectedValueException;
11 11
 
12 12
 class TransactionAmountCast implements CastsAttributes
13 13
 {
14
-    private array $currencyCache = [];
14
+    /**
15
+     * Static cache to persist across instances
16
+     */
17
+    private static array $currencyCache = [];
18
+
19
+    /**
20
+     * Eagerly load all required bank accounts at once if needed
21
+     */
22
+    private function loadMissingBankAccounts(array $ids): void
23
+    {
24
+        $missingIds = array_filter($ids, static fn ($id) => ! isset(self::$currencyCache[$id]) && $id !== null);
25
+
26
+        if (empty($missingIds)) {
27
+            return;
28
+        }
29
+
30
+        /** @var BankAccount[] $accounts */
31
+        $accounts = BankAccount::with('account')
32
+            ->whereIn('id', $missingIds)
33
+            ->get();
34
+
35
+        foreach ($accounts as $account) {
36
+            self::$currencyCache[$account->id] = $account->account->currency_code ?? CurrencyAccessor::getDefaultCurrency();
37
+        }
38
+    }
15 39
 
16 40
     public function get(Model $model, string $key, mixed $value, array $attributes): string
17 41
     {
18 42
         // Attempt to retrieve the currency code from the related bankAccount->account model
19
-        $currencyCode = $this->getCurrencyCodeFromBankAccountId($attributes['bank_account_id'] ?? null);
43
+        $bankAccountId = $attributes['bank_account_id'] ?? null;
44
+
45
+        if ($bankAccountId !== null && ! isset(self::$currencyCache[$bankAccountId])) {
46
+            $this->loadMissingBankAccounts([$bankAccountId]);
47
+        }
48
+
49
+        $currencyCode = $this->getCurrencyCodeFromBankAccountId($bankAccountId);
20 50
 
21 51
         if ($value !== null) {
22 52
             return CurrencyConverter::prepareForMutator($value, $currencyCode);
@@ -30,7 +60,13 @@ class TransactionAmountCast implements CastsAttributes
30 60
      */
31 61
     public function set(Model $model, string $key, mixed $value, array $attributes): int
32 62
     {
33
-        $currencyCode = $this->getCurrencyCodeFromBankAccountId($attributes['bank_account_id'] ?? null);
63
+        $bankAccountId = $attributes['bank_account_id'] ?? null;
64
+
65
+        if ($bankAccountId !== null && ! isset(self::$currencyCache[$bankAccountId])) {
66
+            $this->loadMissingBankAccounts([$bankAccountId]);
67
+        }
68
+
69
+        $currencyCode = $this->getCurrencyCodeFromBankAccountId($bankAccountId);
34 70
 
35 71
         if (is_numeric($value)) {
36 72
             $value = (string) $value;
@@ -42,8 +78,7 @@ class TransactionAmountCast implements CastsAttributes
42 78
     }
43 79
 
44 80
     /**
45
-     * Using this is necessary because the relationship is not always loaded into memory when the cast is called
46
-     * Instead of using: $model->bankAccount->account->currency_code directly, find the bank account and get the currency code
81
+     * Get currency code from the cache or use default
47 82
      */
48 83
     private function getCurrencyCodeFromBankAccountId(?int $bankAccountId): string
49 84
     {
@@ -51,15 +86,6 @@ class TransactionAmountCast implements CastsAttributes
51 86
             return CurrencyAccessor::getDefaultCurrency();
52 87
         }
53 88
 
54
-        if (isset($this->currencyCache[$bankAccountId])) {
55
-            return $this->currencyCache[$bankAccountId];
56
-        }
57
-
58
-        $bankAccount = BankAccount::find($bankAccountId);
59
-
60
-        $currencyCode = $bankAccount?->account?->currency_code ?? CurrencyAccessor::getDefaultCurrency();
61
-        $this->currencyCache[$bankAccountId] = $currencyCode;
62
-
63
-        return $currencyCode;
89
+        return self::$currencyCache[$bankAccountId] ?? CurrencyAccessor::getDefaultCurrency();
64 90
     }
65 91
 }

Notiek ielāde…
Atcelt
Saglabāt