<?php
namespace App\EventSubscriber;
use App\Entity\Account\Customer;
use App\Entity\User;
use App\Service\CustomerBanService;
use Carbon\CarbonImmutable;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Security;
class CommentBanSubscriber implements EventSubscriberInterface
{
public function __construct(
private Security $security,
private CustomerBanService $customerBanService
) {}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'removeCommentBan',
];
}
public function removeCommentBan(): void
{
/** @var User $user */
$user = $this->security->getUser();
if($user && $user instanceof Customer && $user->getCommentBan() && $user->getCommentBan()->getEndsAt() <= CarbonImmutable::now()) {
$this->customerBanService->unbanCommentBan($user);
}
}
}