vendor/ibexa/activity-log/src/bundle/Event/ContentEventsListener.php line 114

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\Bundle\ActivityLog\Event;
  8. use Ibexa\Contracts\ActivityLog\ActivityLogServiceInterface;
  9. use Ibexa\Contracts\AdminUi\Autosave\AutosaveServiceInterface;
  10. use Ibexa\Contracts\Core\Repository\Events\Content\CreateContentDraftEvent;
  11. use Ibexa\Contracts\Core\Repository\Events\Content\CreateContentEvent;
  12. use Ibexa\Contracts\Core\Repository\Events\Content\DeleteContentEvent;
  13. use Ibexa\Contracts\Core\Repository\Events\Content\DeleteTranslationEvent;
  14. use Ibexa\Contracts\Core\Repository\Events\Content\HideContentEvent;
  15. use Ibexa\Contracts\Core\Repository\Events\Content\PublishVersionEvent;
  16. use Ibexa\Contracts\Core\Repository\Events\Content\RevealContentEvent;
  17. use Ibexa\Contracts\Core\Repository\Events\Content\UpdateContentEvent;
  18. use Ibexa\Contracts\Core\Repository\Events\Trash\RecoverEvent;
  19. use Ibexa\Contracts\Core\Repository\Events\Trash\TrashEvent;
  20. use Ibexa\Contracts\Core\Repository\Values\Content\Content;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. final class ContentEventsListener implements EventSubscriberInterface
  23. {
  24.     private ActivityLogServiceInterface $activityLogService;
  25.     private ?AutosaveServiceInterface $autosaveService;
  26.     public function __construct(
  27.         ActivityLogServiceInterface $activityLogService,
  28.         ?AutosaveServiceInterface $autosaveService
  29.     ) {
  30.         $this->activityLogService $activityLogService;
  31.         $this->autosaveService $autosaveService;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             CreateContentDraftEvent::class => ['onCreateContentDraft'],
  37.             CreateContentEvent::class => ['onCreateContent'],
  38.             UpdateContentEvent::class => ['onUpdateContent'],
  39.             TrashEvent::class => ['onTrashContent'],
  40.             RecoverEvent::class => ['onRecoverContent'],
  41.             DeleteContentEvent::class => ['onDeleteContent'],
  42.             DeleteTranslationEvent::class => ['onDeleteContentTranslation'],
  43.             HideContentEvent::class => ['onHideContent'],
  44.             RevealContentEvent::class => ['onRevealContent'],
  45.             PublishVersionEvent::class => ['onPublishContent'],
  46.         ];
  47.     }
  48.     public function onCreateContentDraft(CreateContentDraftEvent $event): void
  49.     {
  50.         $content $event->getContentDraft();
  51.         $this->saveActivityLog(ActivityLogServiceInterface::ACTION_CREATE_DRAFT$event->getContentInfo()->getId(), $content->getName());
  52.     }
  53.     public function onCreateContent(CreateContentEvent $event): void
  54.     {
  55.         $content $event->getContent();
  56.         $this->saveActivityLog(ActivityLogServiceInterface::ACTION_CREATE$content->id$content->getName());
  57.     }
  58.     public function onUpdateContent(UpdateContentEvent $event): void
  59.     {
  60.         if ($this->autosaveService !== null && $this->autosaveService->isInProgress()) {
  61.             // Skip autosaved drafts
  62.             return;
  63.         }
  64.         $content $event->getContent();
  65.         $this->saveActivityLog(ActivityLogServiceInterface::ACTION_UPDATE$content->id$content->getName());
  66.     }
  67.     public function onPublishContent(PublishVersionEvent $event): void
  68.     {
  69.         $content $event->getContent();
  70.         $this->saveActivityLog(ActivityLogServiceInterface::ACTION_PUBLISH$content->id$content->getName());
  71.     }
  72.     public function onRecoverContent(RecoverEvent $event): void
  73.     {
  74.         $contentInfo $event->getTrashItem()->getContentInfo();
  75.         $this->saveActivityLog(ActivityLogServiceInterface::ACTION_RESTORE$contentInfo->getId(), $contentInfo->name);
  76.     }
  77.     public function onTrashContent(TrashEvent $event): void
  78.     {
  79.         $item $event->getTrashItem();
  80.         if ($item === null) {
  81.             return;
  82.         }
  83.         $contentInfo $item->getContentInfo();
  84.         $this->saveActivityLog(ActivityLogServiceInterface::ACTION_TRASH$contentInfo->getId(), $contentInfo->name);
  85.     }
  86.     public function onDeleteContent(DeleteContentEvent $event): void
  87.     {
  88.         $contentInfo $event->getContentInfo();
  89.         $this->saveActivityLog(ActivityLogServiceInterface::ACTION_DELETE$contentInfo->getId(), $contentInfo->name);
  90.     }
  91.     public function onDeleteContentTranslation(DeleteTranslationEvent $event): void
  92.     {
  93.         $contentInfo $event->getContentInfo();
  94.         $this->saveActivityLog(ActivityLogServiceInterface::ACTION_DELETE_TRANSLATION$contentInfo->getId(), $contentInfo->name);
  95.     }
  96.     public function onHideContent(HideContentEvent $event): void
  97.     {
  98.         $contentInfo $event->getContentInfo();
  99.         $this->saveActivityLog(ActivityLogServiceInterface::ACTION_HIDE$contentInfo->getId(), $contentInfo->name);
  100.     }
  101.     public function onRevealContent(RevealContentEvent $event): void
  102.     {
  103.         $contentInfo $event->getContentInfo();
  104.         $this->saveActivityLog(ActivityLogServiceInterface::ACTION_REVEAL$contentInfo->getId(), $contentInfo->name);
  105.     }
  106.     private function saveActivityLog(string $actionint $id, ?string $name): void
  107.     {
  108.         $activityLog $this->activityLogService->build(Content::class, (string)$id$action);
  109.         if ($name !== null) {
  110.             $activityLog->setObjectName($name);
  111.         }
  112.         $this->activityLogService->save($activityLog);
  113.     }
  114. }