<?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';
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;
}
$calendarCount = $calendarAccounts->count();
if ($calendarCount < $subject->getSubscriptionCalendarsLimit()) {
return true;
}
return false;
}
}