vendor/store.shopware.com/swagenterprisesearchplatform/src/Action/ActionLoadSubscriber.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swag\EnterpriseSearch\Action;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. class ActionLoadSubscriber implements EventSubscriberInterface
  6. {
  7.     public static function getSubscribedEvents(): array
  8.     {
  9.         return [ActionEvents::ACTION_LOADED_EVENT => 'setIsValid'];
  10.     }
  11.     public function setIsValid(EntityLoadedEvent $event): void
  12.     {
  13.         /** @var ActionEntity $entity */
  14.         foreach ($event->getEntities() as $entity) {
  15.             $entity->setIsValid($this->isValid($entity));
  16.         }
  17.     }
  18.     private function isValid(ActionEntity $action): bool
  19.     {
  20.         $now = new \DateTime();
  21.         $validFrom $action->getValidFrom();
  22.         $validTo $action->getValidTo();
  23.         if (!($validFrom instanceof \DateTimeInterface) && !($validTo instanceof \DateTimeInterface)) {
  24.             return true;
  25.         }
  26.         if (!($validTo instanceof \DateTimeInterface)) {
  27.             return $now >= $validFrom;
  28.         }
  29.         if (!($validFrom instanceof \DateTimeInterface)) {
  30.             return $now $validTo;
  31.         }
  32.         return $now >= $validFrom && $now $validTo;
  33.     }
  34. }