<?php
declare(strict_types=1);
namespace Harmonizely\Calendar\Voter;
use Doctrine\Common\Collections\Collection;
use Harmonizely\Model\CalendarAccountInterface;
use Harmonizely\Model\User;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
final class ConnectCalendarVoter extends Voter
{
public const CONNECT_CALENDAR = 'connect_calendar';
public const MAX_CONNECTED_CALENDARS = 4;
public const MAX_CONNECTED_CALENDARS_PREMIUM = 9;
protected function supports($attribute, $subject)
{
if ($attribute !== self::CONNECT_CALENDAR) {
return false;
}
if (false === $subject instanceof User) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
/** @var User $subject */
/** @var CalendarAccountInterface[]|Collection $calendarAccounts */
$calendarAccounts = $subject->getCalendarAccounts();
if ($calendarAccounts->isEmpty()) {
return true;
}
if ($calendarAccounts->count() < self::MAX_CONNECTED_CALENDARS) {
return true;
}
if ($calendarAccounts->count() < self::MAX_CONNECTED_CALENDARS_PREMIUM) {
return $subject->hasPaidSubscription();
}
return false;
}
}