<?php
/*
* Author: Dominik Piekarski <code@dompie.de>
* Created at: 2020/08/25 16:36
*/
declare(strict_types=1);
namespace App\Lib\Payment\Voter;
use App\Dictionary\Permission;
use App\Entity\Account;
use App\Service\Payment\PaymentService;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class HasPaidAllBillsVoter extends Voter
{
protected PaymentService $payments;
public function __construct(PaymentService $payments)
{
$this->payments = $payments;
}
protected function supports(string $attribute, $subject): bool
{
return Permission::ALLOWED_TO_BOOK === $attribute && $subject instanceof Account;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$result = 0.0 === $this->payments->getOpenPaymentAmountByAccount($subject);
if (!$result) {
throw PaymentVoterException::hasUnpaidBills();
}
return true;
}
}