vendor/ibexa/scheduler/src/lib/Event/Subscriber/RepositoryEventSubscriber.php line 83

Open in your IDE?
  1. <?php
  2. /**
  3.  * @copyright Copyright (C) Ibexa AS. All rights reserved.
  4.  * @license For full copyright and license information view LICENSE file distributed with this source code.
  5.  */
  6. declare(strict_types=1);
  7. namespace Ibexa\Scheduler\Event\Subscriber;
  8. use Ibexa\Contracts\Core\Repository\Events\Content\BeforeDeleteContentEvent;
  9. use Ibexa\Contracts\Core\Repository\Events\Content\BeforeDeleteVersionEvent;
  10. use Ibexa\Contracts\Core\Repository\Events\Content\UpdateContentEvent;
  11. use Ibexa\Contracts\Core\Repository\Events\Trash\TrashEvent;
  12. use Ibexa\Contracts\Core\Repository\NotificationService;
  13. use Ibexa\Contracts\Core\Repository\Values\Notification\CreateStruct;
  14. use Ibexa\Contracts\Scheduler\Repository\DateBasedPublishServiceInterface;
  15. use Ibexa\Core\Base\Exceptions\NotFoundException;
  16. use Ibexa\Scheduler\Persistence\HandlerInterface;
  17. use Ibexa\Scheduler\Repository\DateBasedPublisherService;
  18. use Ibexa\Scheduler\ValueObject\NotificationFactory;
  19. use Ibexa\Scheduler\ValueObject\ScheduledEntry as SPIScheduledEntry;
  20. use JMS\TranslationBundle\Annotation\Desc;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. class RepositoryEventSubscriber implements EventSubscriberInterface
  24. {
  25.     /** @var \Ibexa\Scheduler\Persistence\HandlerInterface */
  26.     private $persistenceHandler;
  27.     /** @var \Symfony\Contracts\Translation\TranslatorInterface */
  28.     private $translator;
  29.     /** @var \Ibexa\Scheduler\ValueObject\NotificationFactory */
  30.     private $notificationFactory;
  31.     /** @var \Ibexa\Contracts\Core\Repository\NotificationService */
  32.     private $notificationService;
  33.     /** @var \Ibexa\Contracts\Scheduler\Repository\DateBasedPublishServiceInterface */
  34.     private $dateBasedPublisherService;
  35.     public function __construct(
  36.         HandlerInterface $persistenceHandler,
  37.         TranslatorInterface $translator,
  38.         NotificationFactory $notificationFactory,
  39.         NotificationService $notificationService,
  40.         DateBasedPublishServiceInterface $dateBasedPublisherService
  41.     ) {
  42.         $this->persistenceHandler $persistenceHandler;
  43.         $this->translator $translator;
  44.         $this->notificationFactory $notificationFactory;
  45.         $this->notificationService $notificationService;
  46.         $this->dateBasedPublisherService $dateBasedPublisherService;
  47.     }
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [
  51.             BeforeDeleteContentEvent::class => 'onBeforeDeleteContent',
  52.             BeforeDeleteVersionEvent::class => 'onBeforeDeleteVersion',
  53.             UpdateContentEvent::class => 'onUpdateContent',
  54.             TrashEvent::class => 'onTrash',
  55.         ];
  56.     }
  57.     public function onBeforeDeleteContent(BeforeDeleteContentEvent $event): void
  58.     {
  59.         $contentInfo $event->getContentInfo();
  60.         $contentId $contentInfo->id;
  61.         $spiScheduledEntries $this->persistenceHandler
  62.             ->getAllTypesEntries($contentIdnull0null);
  63.         $this->persistenceHandler->deleteAllTypesEntries($contentIdnull);
  64.         foreach ($spiScheduledEntries as $spiScheduledEntry) {
  65.             $this->createNotificationForDelete($spiScheduledEntry);
  66.         }
  67.     }
  68.     public function onBeforeDeleteVersion(BeforeDeleteVersionEvent $event): void
  69.     {
  70.         $versionInfo $event->getVersionInfo();
  71.         $versionId $versionInfo->id;
  72.         try {
  73.             $scheduledVersion $this->dateBasedPublisherService->getScheduledPublish($versionId);
  74.         } catch (NotFoundException $e) {
  75.             return;
  76.         }
  77.         $this->persistenceHandler->deleteVersionEntry(
  78.             $versionId,
  79.             DateBasedPublisherService::ACTION_TYPE
  80.         );
  81.         $contentId $scheduledVersion->content->id;
  82.         $versionNo $scheduledVersion->versionInfo->versionNo;
  83.         $notification = new CreateStruct();
  84.         $notification->ownerId $scheduledVersion->user->id;
  85.         $notification->type 'DateBasedPublisher:' NotificationFactory::TYPE_UNSCHEDULED;
  86.         $notification->data = [
  87.             'message' => $this->translator->trans(/** @Desc("Version deleted") */
  88.                 'version.deleted',
  89.                 [],
  90.                 'ibexa_scheduler'
  91.             ),
  92.             'contentName' => sprintf(
  93.                 '[id: %d, version: %d]',
  94.                 $contentId,
  95.                 $versionNo
  96.             ),
  97.             'contentId' => $contentId,
  98.             'versionNumber' => $versionNo,
  99.             'isAvailable' => false,
  100.             'link' => false,
  101.         ];
  102.         $this->notificationService->createNotification($notification);
  103.     }
  104.     public function onUpdateContent(UpdateContentEvent $event): void
  105.     {
  106.         $versionInfo $event->getContent()->getVersionInfo();
  107.         $isVersionScheduled $this->dateBasedPublisherService->isScheduledPublish($versionInfo->id);
  108.         if (!$isVersionScheduled) {
  109.             return;
  110.         }
  111.         $scheduledVersion $this->dateBasedPublisherService->getScheduledPublish($versionInfo->id);
  112.         $this->dateBasedPublisherService->unschedulePublish($versionInfo->id);
  113.         $createStruct $this->notificationFactory->getNotificationCreateStruct($scheduledVersionNotificationFactory::TYPE_UNSCHEDULED);
  114.         $createStruct->data['message'] = $this->translator->trans(/** @Desc("Content updated") */
  115.             'content.updated',
  116.             [],
  117.             'ibexa_scheduler'
  118.         );
  119.         $this->notificationService->createNotification($createStruct);
  120.     }
  121.     public function onTrash(TrashEvent $event): void
  122.     {
  123.         /** @var \Ibexa\Core\Repository\Values\Content\TrashItem|null $trashItem */
  124.         $trashItem $event->getTrashItem();
  125.         if ($trashItem === null) {
  126.             return;
  127.         }
  128.         $removedLocationContentIdMap $trashItem->getRemovedLocationContentIdMap();
  129.         foreach ($removedLocationContentIdMap as $contentId) {
  130.             $spiScheduledEntries $this->persistenceHandler
  131.                 ->getAllTypesEntries($contentIdnull0null);
  132.             $this->persistenceHandler->deleteAllTypesEntries($contentIdnull);
  133.             foreach ($spiScheduledEntries as $spiScheduledEntry) {
  134.                 $this->createNotificationForTrash($spiScheduledEntry);
  135.             }
  136.         }
  137.     }
  138.     private function createNotificationForTrash(SPIScheduledEntry $scheduledVersion): void
  139.     {
  140.         $message $this->translator->trans(
  141.             /** @Desc("Content moved to Trash") */
  142.             'content.trashed',
  143.             [],
  144.             'ibexa_scheduler'
  145.         );
  146.         $notification $this->notificationFactory->getNotificationCreateStructBySPIEntry(
  147.             $scheduledVersion,
  148.             NotificationFactory::TYPE_UNSCHEDULED,
  149.             true,
  150.             $message
  151.         );
  152.         $this->notificationService->createNotification($notification);
  153.     }
  154.     private function createNotificationForDelete(SPIScheduledEntry $scheduledEntry): void
  155.     {
  156.         $message $this->translator->trans(
  157.             /** @Desc("Content deleted") */
  158.             'content.deleted',
  159.             [],
  160.             'ibexa_scheduler'
  161.         );
  162.         $notification $this->notificationFactory->getNotificationCreateStructBySPIEntry(
  163.             $scheduledEntry,
  164.             NotificationFactory::TYPE_UNSCHEDULED,
  165.             false,
  166.             $message
  167.         );
  168.         $notification->data['contentName'] = sprintf(
  169.             '[id: %d, version: %d]',
  170.             $scheduledEntry->contentId,
  171.             $scheduledEntry->versionNumber ?? '-'
  172.         );
  173.         $this->notificationService->createNotification($notification);
  174.     }
  175. }
  176. class_alias(RepositoryEventSubscriber::class, 'EzSystems\DateBasedPublisher\Core\Event\Subscriber\RepositoryEventSubscriber');