src/FlexApp/EventSubscriber/BanIpSubscriber.php line 45

Open in your IDE?
  1. <?php
  2. namespace FlexApp\EventSubscriber;
  3. use DateInterval;
  4. use DateTime;
  5. use FlexApp\Entity\BannedIp;
  6. use FlexApp\Events\CommentEvent;
  7. use FlexApp\Events\Style43\UserUsedStopWordsEvent;
  8. use FlexApp\Repository\BannedIpRepository;
  9. use FlexApp\Service\EntitySaver;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class BanIpSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var BannedIpRepository
  15.      */
  16.     private $bannedIpRepository;
  17.     /**
  18.      * @var \FlexApp\Service\EntitySaver
  19.      */
  20.     private $entitySaver;
  21.     /**
  22.      * BanIpSubscriber constructor.
  23.      *
  24.      * @param \FlexApp\Repository\BannedIpRepository $bannedIpRepository
  25.      * @param EntitySaver $entitySaver
  26.      */
  27.     public function __construct(BannedIpRepository $bannedIpRepositoryEntitySaver $entitySaver)
  28.     {
  29.         $this->bannedIpRepository $bannedIpRepository;
  30.         $this->entitySaver $entitySaver;
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             UserUsedStopWordsEvent::class => [
  36.                 ['banUserIps'0],
  37.             ],
  38.         ];
  39.     }
  40.     public function banUserIps(CommentEvent $commentEvent)
  41.     {
  42.         $comment $commentEvent->getComment();
  43.         $ips $comment->getIps();
  44.         foreach ($ips as $ipString) {
  45.             $currentDateTime = new DateTime();
  46.             $dateTimeTill = clone $currentDateTime;
  47.             $dateTimeTill->add(new DateInterval('P1M'));
  48.             $bannedIpEntity $this->bannedIpRepository->findOneBy(['ip' => $ipString]);
  49.             if (!$bannedIpEntity) {
  50.                 $bannedIpEntity = new BannedIp();
  51.                 $bannedIpEntity->setIp($ipString);
  52.             }
  53.             $bannedIpEntity->setBannedTill($dateTimeTill);
  54.             $this->entitySaver->save($bannedIpEntity);
  55.         }
  56.     }
  57. }