<?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 = 3;
public const MAX_CONNECTED_CALENDARS_PREMIUM = 8;
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 (!$subject->hasCurrentSubscription()) {
return false;
}
$calendarCount = $calendarAccounts->count();
if ($calendarCount < self::MAX_CONNECTED_CALENDARS) {
return true;
}
if ($calendarCount < self::MAX_CONNECTED_CALENDARS_PREMIUM) {
return $subject->hasPaidSubscription();
}
return false;
}
}