src/Lib/Payment/Voter/HasCoinShortageVoter.php line 17

Open in your IDE?
  1. <?php
  2. /*
  3.  * Author: Dominik Piekarski <code@dompie.de>
  4.  * Created at: 2020/08/25 16:29
  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\User\BalanceMemberService;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  14. class HasCoinShortageVoter extends Voter
  15. {
  16.     protected array $supportUsernames = [];
  17.     protected BalanceMemberService $service;
  18.     public function __construct(ParameterBagInterface $paramsBalanceMemberService $service)
  19.     {
  20.         $this->supportUsernames $params->get('support_usernames');
  21.         $this->service $service;
  22.     }
  23.     /**
  24.      * @param Account $subject
  25.      */
  26.     protected function supports(string $attribute$subject): bool
  27.     {
  28.         return Permission::ALLOWED_TO_BOOK === $attribute && $subject instanceof Account;
  29.     }
  30.     /**
  31.      * @param Account $subject
  32.      */
  33.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  34.     {
  35.         if (in_array(mb_strtolower($subject->getMember()->getUsername()), $this->supportUsernamestrue)) {
  36.             return true;
  37.         }
  38.         if ($this->service->getCoinsForMember($subject->getMember()) >= 2000) {
  39.             throw PaymentVoterException::noCoinShortage();
  40.         }
  41.         return true;
  42.     }
  43. }