src/Calendar/Voter/ConnectCalendarVoter.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Harmonizely\Calendar\Voter;
  4. use Doctrine\Common\Collections\Collection;
  5. use Harmonizely\Model\CalendarAccountInterface;
  6. use Harmonizely\Model\User;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. final class ConnectCalendarVoter extends Voter
  10. {
  11.     public const CONNECT_CALENDAR 'connect_calendar';
  12.     public const MAX_CONNECTED_CALENDARS 4;
  13.     public const MAX_CONNECTED_CALENDARS_PREMIUM 9;
  14.     protected function supports($attribute$subject)
  15.     {
  16.         if ($attribute !== self::CONNECT_CALENDAR) {
  17.             return false;
  18.         }
  19.         if (false === $subject instanceof User) {
  20.             return false;
  21.         }
  22.         return true;
  23.     }
  24.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  25.     {
  26.         /** @var User $subject */
  27.         /** @var CalendarAccountInterface[]|Collection $calendarAccounts */
  28.         $calendarAccounts $subject->getCalendarAccounts();
  29.         if ($calendarAccounts->isEmpty()) {
  30.             return true;
  31.         }
  32.         if ($calendarAccounts->count() < self::MAX_CONNECTED_CALENDARS) {
  33.             return true;
  34.         }
  35.         if ($calendarAccounts->count() < self::MAX_CONNECTED_CALENDARS_PREMIUM) {
  36.             return $subject->hasPaidSubscription();
  37.         }
  38.         return false;
  39.     }
  40. }