src/Offer/EventSubscriber/SetOfferDraftMetaSubscriber.php line 48

  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * Copyright (c) 2021 TECLA Consulting Group oü.
  5.  * All rights reserved.
  6.  *
  7.  * This unpublished material is proprietary to TECLA Consulting Group oü.
  8.  * All rights reserved. The methods and
  9.  * techniques described herein are considered trade secrets
  10.  * and/or confidential. Reproduction or distribution, in whole
  11.  * or in part, is forbidden except by express written permission
  12.  * of TECLA Consulting Group oü.
  13.  *
  14.  * @author    Matúš Sýkorjak <matus@tecla.no>
  15.  * @copyright 2021 TECLA Consulting Group oü
  16.  */
  17. namespace App\Offer\EventSubscriber;
  18. use App\Contact\Security\ContactContextInterface;
  19. use App\Offer\Event\OfferDraft\CreateEvent;
  20. use App\Offer\Event\OfferDraft\DeleteEvent;
  21. use App\Offer\Event\OfferDraft\UpdateEvent;
  22. use DateTimeImmutable;
  23. use JetBrains\PhpStorm\ArrayShape;
  24. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  25. final class SetOfferDraftMetaSubscriber implements EventSubscriberInterface
  26. {
  27.     private ContactContextInterface $context;
  28.     public function __construct(ContactContextInterface $context)
  29.     {
  30.         $this->context $context;
  31.     }
  32.     #[ArrayShape([CreateEvent::class => 'array'UpdateEvent::class => 'array'DeleteEvent::class => 'array'])]
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             CreateEvent::class => ['onCreate', -1024],
  37.             UpdateEvent::class => ['onUpdate', -1024],
  38.             DeleteEvent::class => ['onDelete', -1024],
  39.         ];
  40.     }
  41.     public function onCreate(CreateEvent $event): void
  42.     {
  43.         $nowDatetime = new DateTimeImmutable();
  44.         $offerDraft $event->getOfferDraft();
  45.         $offerDraft->setCreatedBy($this->context->getBase());
  46.         $offerDraft->setCreatedAt($nowDatetime);
  47.         $offerDraft->setUpdatedBy($this->context->getBase());
  48.         $offerDraft->setUpdatedAt($nowDatetime);
  49.     }
  50.     public function onUpdate(UpdateEvent $event): void
  51.     {
  52.         $nowDatetime = new DateTimeImmutable();
  53.         $offerDraft $event->getOfferDraft();
  54.         $offerDraft->setUpdatedBy($this->context->getBase());
  55.         $offerDraft->setUpdatedAt($nowDatetime);
  56.     }
  57.     public function onDelete(DeleteEvent $event): void
  58.     {
  59.         $offerDraft $event->getOfferDraft();
  60.         if (null !== $offerDraft->getDeletedAt()) {
  61.             return;
  62.         }
  63.         $offerDraft->setDeletedBy($this->context->getBase());
  64.         $offerDraft->setDeletedAt(new DateTimeImmutable());
  65.     }
  66. }