src/Security/Voters/Olympiad/TrialTestVoter.php line 12
<?php
namespace App\Security\Voters\Olympiad;
use App\Service\Site\Config\Config;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class TrialTestVoter extends Voter
{
public const TRIAL_TEST_ENABLED = 'trial_test_enabled';
private Security $security;
private RequestStack $requestStack;
private Config $config;
public function __construct(Security $security, RequestStack $requestStack, Config $config)
{
$this->security = $security;
$this->requestStack = $requestStack;
$this->config = $config;
}
protected function supports(string $attribute, $subject): bool
{
// dump($attribute, $subject);
if ($attribute != self::TRIAL_TEST_ENABLED) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
switch ($attribute) {
case self::TRIAL_TEST_ENABLED:
return $this->isEnabled();
}
throw new \LogicException('This code should not be reached!');
}
private function isEnabled(): bool
{
if ($this->security->isGranted('ROLE_TEST_ANY_TIME')) {
return true;
}
//dump($this->trial_is_enabled);
$enabled=$this->config->value('trial-test', 'enable');
if (!$enabled) {
return false;
}
$enabledLocales = explode('|', $this->config->value('trial-test', 'locale'));
if (!in_array($this->requestStack->getMainRequest()->getLocale(), $enabledLocales)) {
return false;
}
$endTime = $this->config->value('trial-test', 'end');
$startTime = $this->config->value('trial-test', 'start');
// $endTime = new \DateTime('18.05.2022 06:00');
// $startTime = new \DateTime('17.05.2022 00:00');
$now = new \DateTime();
// dump($now);
if ($endTime && $now > $endTime) {
return false;
}
if ($startTime && $now < $startTime) {
return false;
}
return true;
}
}