src/FMT/Application/Voter/UserContactVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace FMT\Application\Voter;
  3. use FMT\Data\Entity\UserContact;
  4. use FOS\UserBundle\Model\UserInterface;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. /**
  8.  * Class UserContactVoter
  9.  * @package FMT\Application\Voter
  10.  */
  11. class UserContactVoter extends Voter
  12. {
  13.     const CAN_DELETE 'canDeleteContact';
  14.     const AVAILABLE_METHODS = [
  15.         self::CAN_DELETE,
  16.     ];
  17.     /**
  18.      * @param string $attribute
  19.      * @param mixed $subject
  20.      * @return bool
  21.      * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  22.      */
  23.     protected function supports($attribute$subject)
  24.     {
  25.         if (!in_array($attributeself::AVAILABLE_METHODS)) {
  26.             return false;
  27.         }
  28.         return true;
  29.     }
  30.     /**
  31.      * @param string $attribute
  32.      * @param mixed $subject
  33.      * @param TokenInterface $token
  34.      * @return bool
  35.      * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  36.      */
  37.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  38.     {
  39.         /** @var UserInterface $user */
  40.         $user $token->getUser();
  41.         if (!$subject instanceof UserContact || !$user instanceof UserInterface) {
  42.             return false;
  43.         }
  44.         if (method_exists($this$attribute)) {
  45.             return $this->$attribute($subject$user);
  46.         }
  47.         return false;
  48.     }
  49.     /**
  50.      * @param UserContact $contact
  51.      * @param UserInterface $user
  52.      * @return bool
  53.      */
  54.     public function canDeleteContact(UserContact $contactUserInterface $user)
  55.     {
  56.         return $contact->getStudent() === $user;
  57.     }
  58. }