From 1ffb913fe8ac4765495fbdad6a326b6ef4aa9d95 Mon Sep 17 00:00:00 2001 From: Yoga Pangestu Date: Mon, 22 Jun 2026 11:39:40 +0700 Subject: [PATCH] refactor: refactor CashTransaction model logic into helper methods and update best practice documentation for Eloquent attributes --- LARAVEL_BEST_PRACTICE.md | 26 +++++++++++------ app/Models/CashTransaction.php | 51 +++++++++++++++------------------- 2 files changed, 41 insertions(+), 36 deletions(-) diff --git a/LARAVEL_BEST_PRACTICE.md b/LARAVEL_BEST_PRACTICE.md index 03fd37e..0c05ddb 100644 --- a/LARAVEL_BEST_PRACTICE.md +++ b/LARAVEL_BEST_PRACTICE.md @@ -45,22 +45,32 @@ ### **Prinsip _single responsibility_** Contoh buruk: ```php -public function getFullNameAttribute(): string +use Illuminate\Database\Eloquent\Casts\Attribute; + +public function fullName(): Attribute { - if (auth()->user() && auth()->user()->hasRole('client') && auth()->user()->isVerified()) { - return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name; - } else { - return $this->first_name[0] . '. ' . $this->last_name; - } + return Attribute::make( + get: function () { + if (auth()->user() && auth()->user()->hasRole('client') && auth()->user()->isVerified()) { + return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name; + } else { + return $this->first_name[0] . '. ' . $this->last_name; + } + } + ); } ``` Contoh terbaik: ```php -public function getFullNameAttribute(): string +use Illuminate\Database\Eloquent\Casts\Attribute; + +public function fullName(): Attribute { - return $this->isVerifiedClient() ? $this->getFullNameLong() : $this->getFullNameShort(); + return Attribute::make( + get: fn () => $this->isVerifiedClient() ? $this->getFullNameLong() : $this->getFullNameShort() + ); } public function isVerifiedClient(): bool diff --git a/app/Models/CashTransaction.php b/app/Models/CashTransaction.php index 10ec5e5..27d4aa0 100644 --- a/app/Models/CashTransaction.php +++ b/app/Models/CashTransaction.php @@ -90,13 +90,9 @@ public function createdByName(): Attribute public function isIncoming(): Attribute { return Attribute::make( - get: function () { - if ($this->reference_type === EmployeeAdvance::class && $this->reference instanceof EmployeeAdvance) { - return $this->reference->repayment_cash_transaction_id === $this->id; - } - - return $this->type === CashTransactionType::DEPOSIT; - }, + get: fn () => $this->reference_type === EmployeeAdvance::class + ? $this->isEmployeeAdvanceRepayment() + : $this->type === CashTransactionType::DEPOSIT, ); } @@ -119,13 +115,11 @@ public function referenceLabel(): Attribute return Attribute::make( get: function () { if ($this->reference_type === null) { - return $this->type === CashTransactionType::WITHDRAWAL ? 'Tarik Kas' : 'Setor Kas'; + return $this->getManualTransactionLabel(); } - if ($this->reference_type === EmployeeAdvance::class && $this->reference instanceof EmployeeAdvance) { - return $this->reference->repayment_cash_transaction_id === $this->id - ? 'Pelunasan Kasbon' - : 'Pencairan Kasbon'; + if ($this->reference_type === EmployeeAdvance::class) { + return $this->getEmployeeAdvanceLabel(); } return self::labelForReferenceType($this->reference_type); @@ -133,6 +127,22 @@ public function referenceLabel(): Attribute ); } + public function isEmployeeAdvanceRepayment(): bool + { + return $this->reference instanceof EmployeeAdvance + && $this->reference->repayment_cash_transaction_id === $this->id; + } + + public function getManualTransactionLabel(): string + { + return $this->type === CashTransactionType::WITHDRAWAL ? 'Tarik Kas' : 'Setor Kas'; + } + + public function getEmployeeAdvanceLabel(): string + { + return $this->isEmployeeAdvanceRepayment() ? 'Pelunasan Kasbon' : 'Pencairan Kasbon'; + } + public static function mediaModuleName(): string { return 'cash'; @@ -158,23 +168,8 @@ public static function labelForReferenceType(?string $referenceType): string }; } - public static function isIncomingReference(?string $referenceType): bool - { - if ($referenceType === null) { - return true; - } - - return match ($referenceType) { - default => false, - }; - } - public static function isIncomingTransaction(self $transaction): bool { - if ($transaction->reference_type === EmployeeAdvance::class && $transaction->reference instanceof EmployeeAdvance) { - return $transaction->reference->repayment_cash_transaction_id === $transaction->id; - } - - return self::isIncomingReference($transaction->reference_type); + return (bool) $transaction->is_incoming; } }