src/EventSubscriber/DocFileSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\DocFile;
  4. use App\Entity\DocFileBlock;
  5. use App\Handler\DocFileHandler;
  6. use App\Event\DocFilePreUpdateEvent;
  7. use App\Event\DocFilePrePersistEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class DocFileSubscriber implements EventSubscriberInterface
  10. {
  11.     private $intervalSortCode = [
  12.         'DAY' => 'D',
  13.         'MONTH' => 'M',
  14.         'YEAR' => 'Y'
  15.     ];
  16.     public function __construct(
  17.         private DocFileHandler $handler
  18.     ) {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             DocFilePrePersistEvent::NAME => 'preCreate',
  24.             DocFilePreUpdateEvent::NAME => 'preUpdate'
  25.         ];
  26.     }
  27.     public function preCreate(DocFilePrePersistEvent $event)
  28.     {
  29.         $object $event->getObject();
  30.         $parent $object->getParent();
  31.         if ($object->getDocFileType() !== DocFile::DOCFILE_TYPE_FOLDER && $object->getDocFileType() !== DocFile::DOCFILE_TYPE_URL) {
  32.             $object->addDocFileBlock(new DocFileBlock());
  33.         }
  34.         if ($parent) {
  35.             $path $this->handler->getPathFromParent($parent);
  36.             $object->setPath($path);
  37.         }
  38.         if ($object->getReminderDurationType() && $object->getReminderDuration()) {
  39.             $nr = new \DateTime();
  40.             $nr->add(new \DateInterval('P'.$object->getReminderDuration().$this->intervalSortCode[$object->getReminderDurationType()]));
  41.             $object->setNextReminder($nr);
  42.         }
  43.     }
  44.     public function preUpdate(DocFilePreUpdateEvent $event)
  45.     {
  46.         $object $event->getObject();
  47.         $originalEvent $event->getEvent();
  48.         $changeSet $originalEvent->getEntityChangeSet();
  49.         if (isset($changeSet['reminderDurationType']) || isset($changeSet['reminderDuration'])) {
  50.             $nr null;
  51.             if ($object->getReminderDurationType() && $object->getReminderDuration()) {
  52.                 $nr = new \DateTime();
  53.                 $nr->add(new \DateInterval('P'.$object->getReminderDuration().$this->intervalSortCode[$object->getReminderDurationType()]));
  54.             }
  55.             $object->setNextReminder($nr);
  56.         }
  57.     }
  58. }