src/Security/Voters/Olympiad/TrialTestVoter.php line 12

  1. <?php
  2. namespace App\Security\Voters\Olympiad;
  3. use App\Service\Site\Config\Config;
  4. use Symfony\Component\HttpFoundation\RequestStack;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. class TrialTestVoter extends Voter
  9. {
  10.     public const TRIAL_TEST_ENABLED 'trial_test_enabled';
  11.     private Security $security;
  12.     private RequestStack $requestStack;
  13.     private Config $config;
  14.     public function __construct(Security $securityRequestStack $requestStackConfig $config)
  15.     {
  16.         $this->security $security;
  17.         $this->requestStack $requestStack;
  18.         $this->config $config;
  19.     }
  20.     protected function supports(string $attribute$subject): bool
  21.     {
  22. //        dump($attribute, $subject);
  23.         if ($attribute != self::TRIAL_TEST_ENABLED) {
  24.             return false;
  25.         }
  26.         return true;
  27.     }
  28.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  29.     {
  30.         switch ($attribute) {
  31.             case self::TRIAL_TEST_ENABLED:
  32.                 return $this->isEnabled();
  33.         }
  34.         throw new \LogicException('This code should not be reached!');
  35.     }
  36.     private function isEnabled(): bool
  37.     {
  38.         if ($this->security->isGranted('ROLE_TEST_ANY_TIME')) {
  39.             return true;
  40.         }
  41.         //dump($this->trial_is_enabled);
  42.         $enabled=$this->config->value('trial-test''enable');
  43.         if (!$enabled) {
  44.             return false;
  45.         }
  46.         $enabledLocales explode('|'$this->config->value('trial-test''locale'));
  47.         if (!in_array($this->requestStack->getMainRequest()->getLocale(), $enabledLocales)) {
  48.             return false;
  49.         }
  50.         $endTime =   $this->config->value('trial-test''end');
  51.         $startTime =   $this->config->value('trial-test''start');
  52. //        $endTime = new \DateTime('18.05.2022 06:00');
  53. //        $startTime = new \DateTime('17.05.2022 00:00');
  54.         $now = new \DateTime();
  55. //        dump($now);
  56.         if ($endTime && $now $endTime) {
  57.             return false;
  58.         }
  59.         if ($startTime && $now $startTime) {
  60.             return false;
  61.         }
  62.         return true;
  63.     }
  64. }