src/Lib/Payment/Voter/HasPaidAllBillsVoter.php line 16

Open in your IDE?
  1. <?php
  2. /*
  3.  * Author: Dominik Piekarski <code@dompie.de>
  4.  * Created at: 2020/08/25 16:36
  5.  */
  6. declare(strict_types=1);
  7. namespace App\Lib\Payment\Voter;
  8. use App\Dictionary\Permission;
  9. use App\Entity\Account;
  10. use App\Service\Payment\PaymentService;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. class HasPaidAllBillsVoter extends Voter
  14. {
  15.     protected PaymentService $payments;
  16.     public function __construct(PaymentService $payments)
  17.     {
  18.         $this->payments $payments;
  19.     }
  20.     protected function supports(string $attribute$subject): bool
  21.     {
  22.         return Permission::ALLOWED_TO_BOOK === $attribute && $subject instanceof Account;
  23.     }
  24.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  25.     {
  26.         $result 0.0 === $this->payments->getOpenPaymentAmountByAccount($subject);
  27.         if (!$result) {
  28.             throw PaymentVoterException::hasUnpaidBills();
  29.         }
  30.         return true;
  31.     }
  32. }