vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Event/TrashService.php line 27

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 eZ\Publish\Core\Event;
  8. use eZ\Publish\API\Repository\TrashService as TrashServiceInterface;
  9. use eZ\Publish\API\Repository\Values\Content\Location;
  10. use eZ\Publish\API\Repository\Values\Content\TrashItem;
  11. use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResult;
  12. use eZ\Publish\API\Repository\Values\Content\Trash\TrashItemDeleteResultList;
  13. use eZ\Publish\API\Repository\Events\Trash\BeforeDeleteTrashItemEvent;
  14. use eZ\Publish\API\Repository\Events\Trash\BeforeEmptyTrashEvent;
  15. use eZ\Publish\API\Repository\Events\Trash\BeforeRecoverEvent;
  16. use eZ\Publish\API\Repository\Events\Trash\BeforeTrashEvent;
  17. use eZ\Publish\API\Repository\Events\Trash\DeleteTrashItemEvent;
  18. use eZ\Publish\API\Repository\Events\Trash\EmptyTrashEvent;
  19. use eZ\Publish\API\Repository\Events\Trash\RecoverEvent;
  20. use eZ\Publish\API\Repository\Events\Trash\TrashEvent;
  21. use eZ\Publish\SPI\Repository\Decorator\TrashServiceDecorator;
  22. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  23. class TrashService extends TrashServiceDecorator
  24. {
  25.     /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
  26.     protected $eventDispatcher;
  27.     public function __construct(
  28.         TrashServiceInterface $innerService,
  29.         EventDispatcherInterface $eventDispatcher
  30.     ) {
  31.         parent::__construct($innerService);
  32.         $this->eventDispatcher $eventDispatcher;
  33.     }
  34.     public function trash(Location $location): ?TrashItem
  35.     {
  36.         $eventData = [$location];
  37.         $beforeEvent = new BeforeTrashEvent(...$eventData);
  38.         $this->eventDispatcher->dispatch($beforeEvent);
  39.         if ($beforeEvent->isPropagationStopped()) {
  40.             return $beforeEvent->getResult();
  41.         }
  42.         $trashItem $beforeEvent->isResultSet()
  43.             ? $beforeEvent->getResult()
  44.             : $this->innerService->trash($location);
  45.         $this->eventDispatcher->dispatch(
  46.             new TrashEvent($trashItem, ...$eventData)
  47.         );
  48.         return $trashItem;
  49.     }
  50.     public function recover(
  51.         TrashItem $trashItem,
  52.         ?Location $newParentLocation null
  53.     ): Location {
  54.         $eventData = [
  55.             $trashItem,
  56.             $newParentLocation,
  57.         ];
  58.         $beforeEvent = new BeforeRecoverEvent(...$eventData);
  59.         $this->eventDispatcher->dispatch($beforeEvent);
  60.         if ($beforeEvent->isPropagationStopped()) {
  61.             return $beforeEvent->getLocation();
  62.         }
  63.         $location $beforeEvent->hasLocation()
  64.             ? $beforeEvent->getLocation()
  65.             : $this->innerService->recover($trashItem$newParentLocation);
  66.         $this->eventDispatcher->dispatch(
  67.             new RecoverEvent($location, ...$eventData)
  68.         );
  69.         return $location;
  70.     }
  71.     public function emptyTrash(): TrashItemDeleteResultList
  72.     {
  73.         $beforeEvent = new BeforeEmptyTrashEvent();
  74.         $this->eventDispatcher->dispatch($beforeEvent);
  75.         if ($beforeEvent->isPropagationStopped()) {
  76.             return $beforeEvent->getResultList();
  77.         }
  78.         $resultList $beforeEvent->hasResultList()
  79.             ? $beforeEvent->getResultList()
  80.             : $this->innerService->emptyTrash();
  81.         $this->eventDispatcher->dispatch(
  82.             new EmptyTrashEvent($resultList)
  83.         );
  84.         return $resultList;
  85.     }
  86.     public function deleteTrashItem(TrashItem $trashItem): TrashItemDeleteResult
  87.     {
  88.         $eventData = [$trashItem];
  89.         $beforeEvent = new BeforeDeleteTrashItemEvent(...$eventData);
  90.         $this->eventDispatcher->dispatch($beforeEvent);
  91.         if ($beforeEvent->isPropagationStopped()) {
  92.             return $beforeEvent->getResult();
  93.         }
  94.         $result $beforeEvent->hasResult()
  95.             ? $beforeEvent->getResult()
  96.             : $this->innerService->deleteTrashItem($trashItem);
  97.         $this->eventDispatcher->dispatch(
  98.             new DeleteTrashItemEvent($result, ...$eventData)
  99.         );
  100.         return $result;
  101.     }
  102. }