<?php declare(strict_types=1);
namespace Swag\EnterpriseSearch\Action;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ActionLoadSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [ActionEvents::ACTION_LOADED_EVENT => 'setIsValid'];
}
public function setIsValid(EntityLoadedEvent $event): void
{
/** @var ActionEntity $entity */
foreach ($event->getEntities() as $entity) {
$entity->setIsValid($this->isValid($entity));
}
}
private function isValid(ActionEntity $action): bool
{
$now = new \DateTime();
$validFrom = $action->getValidFrom();
$validTo = $action->getValidTo();
if (!($validFrom instanceof \DateTimeInterface) && !($validTo instanceof \DateTimeInterface)) {
return true;
}
if (!($validTo instanceof \DateTimeInterface)) {
return $now >= $validFrom;
}
if (!($validFrom instanceof \DateTimeInterface)) {
return $now < $validTo;
}
return $now >= $validFrom && $now < $validTo;
}
}