ldc-boutique-cnc/app/Policies/TransactionPolicy.php
2023-05-23 13:42:43 +02:00

52 lines
1.6 KiB
PHP

<?php
namespace App\Policies;
use App\Enums\Order\TransactionStatus;
use App\Models\Customer\Customer;
use App\Models\Payment\Transaction;
use App\Models\User;
use Illuminate\Auth\Access\Response;
class TransactionPolicy extends Policy
{
/**
* Can a user download transaction invoice
*
* ! Only owner customer or a Bo user
* ! + a validated transaction.
* ! + a !needs_validation transaction.
*
* @param \App\Models\Customer\Customer|\App\Models\User $user
* @param \App\Models\Payment\Transaction|null $transaction
*
* @return \Illuminate\Auth\Access\Response
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
*/
public function download(
$user,
Transaction $transaction = null
): Response {
if (!$transaction) {
return $this->deny(\trans('No transaction provided'));
}
if (!$transaction->status->equals(TransactionStatus::validated())) {
return $this->deny(trans('Transaction is not validated'));
}
if ($transaction->needs_validation) {
return $this->deny(\trans('Transaction needs to be validated first'));
}
if ($user instanceof User) {
return $this->allow();
}
if (
$user instanceof Customer and
$transaction->order->customer and
!\is_null($user->hashcode) and
$transaction->order->customer->hashcode === $user->hashcode
) {
return $this->allow();
}
return $this->deny(\trans('Could not satisfy any conditions'));
}
}