src/Voter/InvoiceBatchVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Voter;
  3. use App\Entity\InvoiceBatch;
  4. use App\Model\InvoiceBatchFactory;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class InvoiceBatchVoter extends Voter
  8. {
  9.     /**
  10.      * @var InvoiceBatchFactory
  11.      */
  12.     private $invoiceBatchFactory;
  13.     /**
  14.      * InvoiceBatchVoter constructor.
  15.      */
  16.     public function __construct(InvoiceBatchFactory $invoiceBatchFactory)
  17.     {
  18.         $this->invoiceBatchFactory $invoiceBatchFactory;
  19.     }
  20.     public const EDIT 'edit';
  21.     /**
  22.      * @param string $attribute
  23.      * @param mixed $subject
  24.      */
  25.     protected function supports($attribute$subject): bool
  26.     {
  27.         // if the attribute isn't one we support, return false
  28.         if (! in_array($attribute, [
  29.             self::EDIT
  30.         ])) {
  31.             return false;
  32.         }
  33.         if (! $subject instanceof InvoiceBatch) {
  34.             return false;
  35.         }
  36.         return true;
  37.     }
  38.     /**
  39.      * @param string $attribute
  40.      * @param mixed $subject
  41.      */
  42.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  43.     {
  44.         $user $token->getUser();
  45.         if (!$user) {
  46.             return false;  // User not logged in
  47.         }
  48.         $invoiceBatch $this->invoiceBatchFactory->createModel($subject);
  49.         if ($invoiceBatch->isProcessed()) {
  50.             return false;
  51.         }
  52.         return true;
  53.     }
  54. }