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:
```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

View File

@ -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;
}
}