src/Security/Voters/Olympiad/DirectionVoter.php line 9
<?php
namespace App\Security\Voters\Olympiad;
use App\Entity\Olympiad\Online\Direction;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class DirectionVoter extends Voter
{
public const DIRECTION_REGISTRATION_OPEN = 'direction_registration_open';
public const DIRECTION_WORKS_SHOW = 'direction_works_show';
public const DIRECTION_APPEAL_SHOW = 'direction_appeal_show';
/**
* Determines if the attribute and subject are supported by this voter.
*
* @param string $attribute An attribute
* @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
*
* @return bool True if the attribute and subject are supported, false otherwise
*/
protected function supports($attribute, $subject): bool
{
if (!in_array($attribute, [self::DIRECTION_REGISTRATION_OPEN, self::DIRECTION_WORKS_SHOW, self::DIRECTION_APPEAL_SHOW])) {
return false;
}
// only vote on Post objects inside this voter
if (!$subject instanceof Direction) {
return false;
}
return true;
}
/**
* Perform a single access check operation on a given attribute, subject and token.
* It is safe to assume that $attribute and $subject already passed the "supports()" method check.
*
* @param string $attribute
* @param Direction $subject
* @param TokenInterface $token
*
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::DIRECTION_REGISTRATION_OPEN:
return $this->isRegistrationOpen($subject, $token);
case self::DIRECTION_WORKS_SHOW:
return $this->isWorksShow($subject, $token);
case self::DIRECTION_APPEAL_SHOW:
return $this->isAppealShow($subject, $token);
}
throw new \LogicException('This code should not be reached!');
}
private function isRegistrationOpen(Direction $subject, TokenInterface $token): bool
{
$max_date = $subject->getCategory()->getRegistrationOpen()->getTo();
$min_date = $subject->getCategory()->getRegistrationOpen()->getFrom();
$now = new \DateTime();
if (!empty($max_date) && $max_date < $now) {
return false;
}
if (!empty($min_date) && $min_date > $now) {
return false;
}
// dump($max_date, $min_date, $now);
return true;
}
private function isWorksShow(Direction $subject, TokenInterface $token): bool
{
$max_date = $subject->getWorkShow()->getTo();
$min_date = $subject->getWorkShow()->getFrom();
$now = new \DateTime();
if (!empty($min_date) && $min_date > $now) {
return false;
}
if (!empty($max_date) && $max_date < $now) {
return false;
}
if (empty($min_date)) {
return false;
}
// dump($max_date, $min_date, $now);
return true;
}
private function isAppealShow(Direction $subject, TokenInterface $token): bool
{
$max_date = $subject->getAppealShow()->getTo();
$min_date = $subject->getAppealShow()->getFrom();
$now = new \DateTime();
if (!empty($min_date) && $min_date > $now) {
return false;
}
if (!empty($max_date) && $max_date < $now) {
return false;
}
if (empty($min_date)) {
return false;
}
// dump($max_date, $min_date, $now);
return true;
}
}