refactor: refactor CashTransaction model logic into helper methods and update best practice documentation for Eloquent attributes

This commit is contained in:
Yoga Pangestu 2026-06-22 11:39:40 +07:00
parent 016e8d95d9
commit 1ffb913fe8
2 changed files with 41 additions and 36 deletions

View File

@ -45,22 +45,32 @@ ### **Prinsip _single responsibility_**
Contoh buruk: Contoh buruk:
```php ```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 Attribute::make(
return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name; get: function () {
} else { if (auth()->user() && auth()->user()->hasRole('client') && auth()->user()->isVerified()) {
return $this->first_name[0] . '. ' . $this->last_name; return 'Mr. ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name;
} } else {
return $this->first_name[0] . '. ' . $this->last_name;
}
}
);
} }
``` ```
Contoh terbaik: Contoh terbaik:
```php ```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 public function isVerifiedClient(): bool

View File

@ -90,13 +90,9 @@ public function createdByName(): Attribute
public function isIncoming(): Attribute public function isIncoming(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: function () { get: fn () => $this->reference_type === EmployeeAdvance::class
if ($this->reference_type === EmployeeAdvance::class && $this->reference instanceof EmployeeAdvance) { ? $this->isEmployeeAdvanceRepayment()
return $this->reference->repayment_cash_transaction_id === $this->id; : $this->type === CashTransactionType::DEPOSIT,
}
return $this->type === CashTransactionType::DEPOSIT;
},
); );
} }
@ -119,13 +115,11 @@ public function referenceLabel(): Attribute
return Attribute::make( return Attribute::make(
get: function () { get: function () {
if ($this->reference_type === null) { 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) { if ($this->reference_type === EmployeeAdvance::class) {
return $this->reference->repayment_cash_transaction_id === $this->id return $this->getEmployeeAdvanceLabel();
? 'Pelunasan Kasbon'
: 'Pencairan Kasbon';
} }
return self::labelForReferenceType($this->reference_type); 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 public static function mediaModuleName(): string
{ {
return 'cash'; 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 public static function isIncomingTransaction(self $transaction): bool
{ {
if ($transaction->reference_type === EmployeeAdvance::class && $transaction->reference instanceof EmployeeAdvance) { return (bool) $transaction->is_incoming;
return $transaction->reference->repayment_cash_transaction_id === $transaction->id;
}
return self::isIncomingReference($transaction->reference_type);
} }
} }