From ffd26b91a6032919bfcc7dc782e0ec5be45ded7e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 24 Jan 2025 16:27:51 +1100 Subject: [PATCH] Coalesce duplicate cache calls into a single one per method --- app/Livewire/Flow2/InvoiceSummary.php | 35 ++++++++++++++++--------- app/Livewire/Flow2/PaymentMethod.php | 9 ++++--- app/Livewire/Flow2/ProcessPayment.php | 14 +++++----- app/Livewire/Flow2/RequiredFields.php | 12 +++++---- app/Livewire/Flow2/Terms.php | 7 ++--- app/Livewire/Flow2/UnderOverPayment.php | 15 ++++++----- 6 files changed, 55 insertions(+), 37 deletions(-) diff --git a/app/Livewire/Flow2/InvoiceSummary.php b/app/Livewire/Flow2/InvoiceSummary.php index aff4299d0f..cd0eeb3239 100644 --- a/app/Livewire/Flow2/InvoiceSummary.php +++ b/app/Livewire/Flow2/InvoiceSummary.php @@ -31,40 +31,49 @@ class InvoiceSummary extends Component public function mount() { - $contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user(); - $this->invoices = $this->getContext()['payable_invoices']; - $this->amount = Number::formatMoney($this->getContext()['amount'], $contact->client); - $this->gateway_fee = isset($this->getContext()['gateway_fee']) ? Number::formatMoney($this->getContext()['gateway_fee'], $contact->client) : false; + $_context = $this->getContext(); + + $contact = $_context['contact'] ?? auth()->guard('contact')->user(); + $this->invoices = $_context['payable_invoices']; + $this->amount = Number::formatMoney($_context['amount'], $contact->client); + $this->gateway_fee = isset($_context['gateway_fee']) ? Number::formatMoney($_context['gateway_fee'], $contact->client) : false; } #[On(self::CONTEXT_UPDATE)] public function onContextUpdate(): void { + + $_context = $this->getContext(); + // refactor logic for updating the price for eg if it changes with under/over pay - $contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user(); - $this->invoices = $this->getContext()['payable_invoices']; - $this->amount = Number::formatMoney($this->getContext()['amount'], $contact->client); - $this->gateway_fee = isset($this->getContext()['gateway_fee']) ? Number::formatMoney($this->getContext()['gateway_fee'], $contact->client) : false; + $contact = $_context['contact'] ?? auth()->guard('contact')->user(); + $this->invoices = $_context['payable_invoices']; + $this->amount = Number::formatMoney($_context['amount'], $contact->client); + $this->gateway_fee = isset($_context['gateway_fee']) ? Number::formatMoney($_context['gateway_fee'], $contact->client) : false; } #[On('payment-view-rendered')] public function handlePaymentViewRendered() { + + $_context = $this->getContext(); - $contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user(); - $this->amount = Number::formatMoney($this->getContext()['amount'], $contact->client); - $this->gateway_fee = isset($this->getContext()['gateway_fee']) ? Number::formatMoney($this->getContext()['gateway_fee'], $contact->client) : false; + $contact = $_context['contact'] ?? auth()->guard('contact')->user(); + $this->amount = Number::formatMoney($_context['amount'], $contact->client); + $this->gateway_fee = isset($_context['gateway_fee']) ? Number::formatMoney($_context['gateway_fee'], $contact->client) : false; } public function downloadDocument($invoice_hashed_id) { - $invitation_id = $this->getContext()['invitation_id']; + $_context = $this->getContext(); - $db = $this->getContext()['db']; + $invitation_id = $_context['invitation_id']; + + $db = $_context['db']; $invite = \App\Models\InvoiceInvitation::on($db)->withTrashed()->find($invitation_id); diff --git a/app/Livewire/Flow2/PaymentMethod.php b/app/Livewire/Flow2/PaymentMethod.php index 092d96a242..a89f1c2b9d 100644 --- a/app/Livewire/Flow2/PaymentMethod.php +++ b/app/Livewire/Flow2/PaymentMethod.php @@ -56,12 +56,13 @@ class PaymentMethod extends Component public function mount() { - $this->variables = $this->getContext()['variables']; - $this->amount = array_sum(array_column($this->getContext()['payable_invoices'], 'amount')); + $_context = $this->getContext(); + $this->variables = $_context['variables']; + $this->amount = array_sum(array_column($_context['payable_invoices'], 'amount')); - MultiDB::setDb($this->getContext()['db']); + MultiDB::setDb($_context['db']); - $contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user(); + $contact = $_context['contact'] ?? auth()->guard('contact')->user(); $this->methods = $contact->client->service()->getPaymentMethods($this->amount); diff --git a/app/Livewire/Flow2/ProcessPayment.php b/app/Livewire/Flow2/ProcessPayment.php index b5d907d875..4e05027129 100644 --- a/app/Livewire/Flow2/ProcessPayment.php +++ b/app/Livewire/Flow2/ProcessPayment.php @@ -37,12 +37,14 @@ class ProcessPayment extends Component $invitation = InvoiceInvitation::find($this->getContext()['invitation_id']); + $_context = $this->getContext(); + $data = [ - 'company_gateway_id' => $this->getContext()['company_gateway_id'], - 'payment_method_id' => $this->getContext()['gateway_type_id'], - 'payable_invoices' => $this->getContext()['payable_invoices'], - 'signature' => isset($this->getContext()['signature']) ? $this->getContext()['signature'] : false, - 'signature_ip' => isset($this->getContext()['signature_ip']) ? $this->getContext()['signature_ip'] : false, + 'company_gateway_id' => $_context['company_gateway_id'], + 'payment_method_id' => $_context['gateway_type_id'], + 'payable_invoices' => $_context['payable_invoices'], + 'signature' => isset($_context['signature']) ? $_context['signature'] : false, + 'signature_ip' => isset($_context['signature_ip']) ? $_context['signature_ip'] : false, 'pre_payment' => false, 'frequency_id' => false, 'remaining_cycles' => false, @@ -52,7 +54,7 @@ class ProcessPayment extends Component $responder_data = (new LivewireInstantPayment($data))->run(); - $company_gateway = CompanyGateway::find($this->getContext()['company_gateway_id']); + $company_gateway = CompanyGateway::find($_context['company_gateway_id']); if (!$responder_data['success']) { throw new PaymentFailed($responder_data['error'], 400); diff --git a/app/Livewire/Flow2/RequiredFields.php b/app/Livewire/Flow2/RequiredFields.php index c8d108becf..d3cd39c5bc 100644 --- a/app/Livewire/Flow2/RequiredFields.php +++ b/app/Livewire/Flow2/RequiredFields.php @@ -56,17 +56,19 @@ class RequiredFields extends Component public function mount(): void { + $_context = $this->getContext(); + MultiDB::setDB( - $this->getContext()['db'], + $_context['db'], ); - $this->fields = $this->getContext()['fields']; + $this->fields = $_context['fields']; $contact = auth()->guard('contact')->user(); $this->company_gateway = CompanyGateway::withTrashed() ->with('company') - ->find($this->getContext()['company_gateway_id']); + ->find($_context['company_gateway_id']); $this->client_name = $contact->client->name; $this->contact_first_name = $contact->first_name; @@ -89,8 +91,8 @@ class RequiredFields extends Component $this->client_custom_value4 = $contact->client->custom_value4; $rff = new RFFService( - fields: $this->getContext()['fields'], - database: $this->getContext()['db'], + fields: $_context['fields'], + database: $_context['db'], company_gateway_id: (string)$this->company_gateway->id, ); diff --git a/app/Livewire/Flow2/Terms.php b/app/Livewire/Flow2/Terms.php index 66df651d39..1845c7cbef 100644 --- a/app/Livewire/Flow2/Terms.php +++ b/app/Livewire/Flow2/Terms.php @@ -30,10 +30,11 @@ class Terms extends Component #[Computed()] public function invoice() { - - $invitation_id = $this->getContext()['invitation_id']; + $_context = $this->getContext(); - $db = $this->getContext()['db']; + $invitation_id = $_context['invitation_id']; + + $db = $_context['db']; $invite = \App\Models\InvoiceInvitation::on($db)->withTrashed()->find($invitation_id); diff --git a/app/Livewire/Flow2/UnderOverPayment.php b/app/Livewire/Flow2/UnderOverPayment.php index 7b49a84394..fa5dbb1c04 100644 --- a/app/Livewire/Flow2/UnderOverPayment.php +++ b/app/Livewire/Flow2/UnderOverPayment.php @@ -32,20 +32,23 @@ class UnderOverPayment extends Component public function mount() { - $contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user(); + + $_context = $this->getContext(); - $this->invoice_amount = array_sum(array_column($this->getContext()['payable_invoices'], 'amount')); + $contact = $_context['contact'] ?? auth()->guard('contact')->user(); + + $this->invoice_amount = array_sum(array_column($_context['payable_invoices'], 'amount')); $this->currency = $contact->client->currency(); - $this->payableInvoices = $this->getContext()['payable_invoices']; + $this->payableInvoices = $_context['payable_invoices']; } public function checkValue(array $payableInvoices) { $this->errors = ''; + $_context = $this->getContext(); + $settings = $_context['settings']; - $settings = $this->getContext()['settings']; - - $contact = $this->getContext()['contact'] ?? auth()->guard('contact')->user(); + $contact = $_context['contact'] ?? auth()->guard('contact')->user(); foreach ($payableInvoices as $key => $invoice) { $payableInvoices[$key]['amount'] = Number::parseFloat($invoice['formatted_amount']);