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

Open in your IDE?
  1. <?php
  2. namespace FMT\Application\Voter;
  3. use FMT\Data\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. /**
  7.  * Class DonorVoter
  8.  * @package FMT\Application\Voter
  9.  * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  10.  */
  11. class DonorVoter extends Voter
  12. {
  13.     const CAN_SHARE_FB 'canShareDonorFB';
  14.     const CAN_SHARE_TW 'canShareDonorTW';
  15.     public static $permissionArray = [
  16.         self::CAN_SHARE_FB,
  17.         self::CAN_SHARE_TW,
  18.     ];
  19.     /**
  20.      * @param string $attribute
  21.      * @param mixed $subject
  22.      * @return bool
  23.      */
  24.     protected function supports($attribute$subject)
  25.     {
  26.         if (!in_array($attributeself::$permissionArray)) {
  27.             return false;
  28.         }
  29.         return true;
  30.     }
  31.     /**
  32.      * @param string $attribute
  33.      * @param mixed $subject
  34.      * @param TokenInterface $token
  35.      * @return bool
  36.      */
  37.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  38.     {
  39.         if (!$subject instanceof User || !$subject->isDonor()) {
  40.             return false;
  41.         }
  42.         if (method_exists($this$attribute)) {
  43.             return $this->$attribute($subject);
  44.         }
  45.         return false;
  46.     }
  47.     /**
  48.      * @param User $owner
  49.      * @return bool
  50.      */
  51.     protected function canShareDonorFB(User $owner)
  52.     {
  53.         $profile $owner->getProfile();
  54.         return $profile->isVisibleForAll() && $profile->isFacebook();
  55.     }
  56.     /**
  57.      * @param User $owner
  58.      * @return bool
  59.      */
  60.     protected function canShareDonorTW(User $owner)
  61.     {
  62.         $profile $owner->getProfile();
  63.         return $profile->isVisibleForAll() && $profile->isTwitter();
  64.     }
  65. }