vendor/ibexa/workflow/src/lib/Event/Subscriber/DeleteWorkflowSubscriber.php line 69

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\Workflow\Event\Subscriber;
  8. use Ibexa\Contracts\Core\Repository\Events\Content\DeleteContentEvent;
  9. use Ibexa\Contracts\Core\Repository\Events\Content\DeleteVersionEvent;
  10. use Ibexa\Contracts\Core\Repository\Events\Trash\DeleteTrashItemEvent;
  11. use Ibexa\Contracts\Core\Repository\Events\Trash\EmptyTrashEvent;
  12. use Ibexa\Contracts\Workflow\Persistence\Handler\HandlerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class DeleteWorkflowSubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var \Ibexa\Contracts\Workflow\Persistence\Handler\HandlerInterface */
  17.     private $workflowHandler;
  18.     /**
  19.      * @param \Ibexa\Contracts\Workflow\Persistence\Handler\HandlerInterface $workflowHandler
  20.      */
  21.     public function __construct(HandlerInterface $workflowHandler)
  22.     {
  23.         $this->workflowHandler $workflowHandler;
  24.     }
  25.     /**
  26.      * {@inheritdoc}
  27.      */
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             DeleteVersionEvent::class => 'onDeleteVersion',
  32.             DeleteContentEvent::class => 'onDeleteContent',
  33.             DeleteTrashItemEvent::class => 'onDeleteTrashItem',
  34.             EmptyTrashEvent::class => 'onEmptyTrash',
  35.         ];
  36.     }
  37.     public function onDeleteVersion(DeleteVersionEvent $event): void
  38.     {
  39.         $versionInfo $event->getVersionInfo();
  40.         $this->doDeleteWorkflowMetadata($versionInfo->contentInfo->id$versionInfo->versionNo);
  41.     }
  42.     public function onDeleteContent(DeleteContentEvent $event): void
  43.     {
  44.         $contentInfo $event->getContentInfo();
  45.         $this->doDeleteWorkflowMetadata($contentInfo->id);
  46.     }
  47.     public function onDeleteTrashItem(DeleteTrashItemEvent $event): void
  48.     {
  49.         $trashItemDeleteResult $event->getResult();
  50.         if (!$trashItemDeleteResult->contentRemoved) {
  51.             return;
  52.         }
  53.         $this->doDeleteWorkflowMetadata($trashItemDeleteResult->contentId);
  54.     }
  55.     public function onEmptyTrash(EmptyTrashEvent $event): void
  56.     {
  57.         $resultList $event->getResultList();
  58.         foreach ($resultList as $trashItemDeleteResult) {
  59.             if (!$trashItemDeleteResult->contentRemoved) {
  60.                 continue;
  61.             }
  62.             $this->doDeleteWorkflowMetadata($trashItemDeleteResult->contentId);
  63.         }
  64.     }
  65.     private function doDeleteWorkflowMetadata(
  66.         int $contentId,
  67.         ?int $versionNo null
  68.     ): void {
  69.         $persistenceWorkflowList $this->workflowHandler->loadWorkflowMetadataByContent($contentId$versionNo);
  70.         foreach ($persistenceWorkflowList as $workflowMetadata) {
  71.             $this->workflowHandler->deleteWorkflowMetadata($workflowMetadata->id);
  72.         }
  73.     }
  74. }
  75. class_alias(DeleteWorkflowSubscriber::class, 'EzSystems\EzPlatformWorkflow\Event\Subscriber\DeleteWorkflowSubscriber');