vendor/ezsystems/ezplatform-kernel/eZ/Publish/Core/Event/LocationService.php line 35

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\LocationService as LocationServiceInterface;
  9. use eZ\Publish\API\Repository\Values\Content\ContentInfo;
  10. use eZ\Publish\API\Repository\Values\Content\Location;
  11. use eZ\Publish\API\Repository\Values\Content\LocationCreateStruct;
  12. use eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct;
  13. use eZ\Publish\API\Repository\Events\Location\BeforeCopySubtreeEvent;
  14. use eZ\Publish\API\Repository\Events\Location\BeforeCreateLocationEvent;
  15. use eZ\Publish\API\Repository\Events\Location\BeforeDeleteLocationEvent;
  16. use eZ\Publish\API\Repository\Events\Location\BeforeHideLocationEvent;
  17. use eZ\Publish\API\Repository\Events\Location\BeforeMoveSubtreeEvent;
  18. use eZ\Publish\API\Repository\Events\Location\BeforeSwapLocationEvent;
  19. use eZ\Publish\API\Repository\Events\Location\BeforeUnhideLocationEvent;
  20. use eZ\Publish\API\Repository\Events\Location\BeforeUpdateLocationEvent;
  21. use eZ\Publish\API\Repository\Events\Location\CopySubtreeEvent;
  22. use eZ\Publish\API\Repository\Events\Location\CreateLocationEvent;
  23. use eZ\Publish\API\Repository\Events\Location\DeleteLocationEvent;
  24. use eZ\Publish\API\Repository\Events\Location\HideLocationEvent;
  25. use eZ\Publish\API\Repository\Events\Location\MoveSubtreeEvent;
  26. use eZ\Publish\API\Repository\Events\Location\SwapLocationEvent;
  27. use eZ\Publish\API\Repository\Events\Location\UnhideLocationEvent;
  28. use eZ\Publish\API\Repository\Events\Location\UpdateLocationEvent;
  29. use eZ\Publish\SPI\Repository\Decorator\LocationServiceDecorator;
  30. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  31. class LocationService extends LocationServiceDecorator
  32. {
  33.     /** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface */
  34.     protected $eventDispatcher;
  35.     public function __construct(
  36.         LocationServiceInterface $innerService,
  37.         EventDispatcherInterface $eventDispatcher
  38.     ) {
  39.         parent::__construct($innerService);
  40.         $this->eventDispatcher $eventDispatcher;
  41.     }
  42.     public function copySubtree(
  43.         Location $subtree,
  44.         Location $targetParentLocation
  45.     ): Location {
  46.         $eventData = [
  47.             $subtree,
  48.             $targetParentLocation,
  49.         ];
  50.         $beforeEvent = new BeforeCopySubtreeEvent(...$eventData);
  51.         $this->eventDispatcher->dispatch($beforeEvent);
  52.         if ($beforeEvent->isPropagationStopped()) {
  53.             return $beforeEvent->getLocation();
  54.         }
  55.         $location $beforeEvent->hasLocation()
  56.             ? $beforeEvent->getLocation()
  57.             : $this->innerService->copySubtree($subtree$targetParentLocation);
  58.         $this->eventDispatcher->dispatch(
  59.             new CopySubtreeEvent($location, ...$eventData)
  60.         );
  61.         return $location;
  62.     }
  63.     public function createLocation(
  64.         ContentInfo $contentInfo,
  65.         LocationCreateStruct $locationCreateStruct
  66.     ): Location {
  67.         $eventData = [
  68.             $contentInfo,
  69.             $locationCreateStruct,
  70.         ];
  71.         $beforeEvent = new BeforeCreateLocationEvent(...$eventData);
  72.         $this->eventDispatcher->dispatch($beforeEvent);
  73.         if ($beforeEvent->isPropagationStopped()) {
  74.             return $beforeEvent->getLocation();
  75.         }
  76.         $location $beforeEvent->hasLocation()
  77.             ? $beforeEvent->getLocation()
  78.             : $this->innerService->createLocation($contentInfo$locationCreateStruct);
  79.         $this->eventDispatcher->dispatch(
  80.             new CreateLocationEvent($location, ...$eventData)
  81.         );
  82.         return $location;
  83.     }
  84.     public function updateLocation(
  85.         Location $location,
  86.         LocationUpdateStruct $locationUpdateStruct
  87.     ): Location {
  88.         $eventData = [
  89.             $location,
  90.             $locationUpdateStruct,
  91.         ];
  92.         $beforeEvent = new BeforeUpdateLocationEvent(...$eventData);
  93.         $this->eventDispatcher->dispatch($beforeEvent);
  94.         if ($beforeEvent->isPropagationStopped()) {
  95.             return $beforeEvent->getUpdatedLocation();
  96.         }
  97.         $updatedLocation $beforeEvent->hasUpdatedLocation()
  98.             ? $beforeEvent->getUpdatedLocation()
  99.             : $this->innerService->updateLocation($location$locationUpdateStruct);
  100.         $this->eventDispatcher->dispatch(
  101.             new UpdateLocationEvent($updatedLocation, ...$eventData)
  102.         );
  103.         return $updatedLocation;
  104.     }
  105.     public function swapLocation(
  106.         Location $location1,
  107.         Location $location2
  108.     ): void {
  109.         $eventData = [
  110.             $location1,
  111.             $location2,
  112.         ];
  113.         $beforeEvent = new BeforeSwapLocationEvent(...$eventData);
  114.         $this->eventDispatcher->dispatch($beforeEvent);
  115.         if ($beforeEvent->isPropagationStopped()) {
  116.             return;
  117.         }
  118.         $this->innerService->swapLocation($location1$location2);
  119.         $this->eventDispatcher->dispatch(
  120.             new SwapLocationEvent(...$eventData)
  121.         );
  122.     }
  123.     public function hideLocation(Location $location): Location
  124.     {
  125.         $eventData = [$location];
  126.         $beforeEvent = new BeforeHideLocationEvent(...$eventData);
  127.         $this->eventDispatcher->dispatch($beforeEvent);
  128.         if ($beforeEvent->isPropagationStopped()) {
  129.             return $beforeEvent->getHiddenLocation();
  130.         }
  131.         $hiddenLocation $beforeEvent->hasHiddenLocation()
  132.             ? $beforeEvent->getHiddenLocation()
  133.             : $this->innerService->hideLocation($location);
  134.         $this->eventDispatcher->dispatch(
  135.             new HideLocationEvent($hiddenLocation, ...$eventData)
  136.         );
  137.         return $hiddenLocation;
  138.     }
  139.     public function unhideLocation(Location $location): Location
  140.     {
  141.         $eventData = [$location];
  142.         $beforeEvent = new BeforeUnhideLocationEvent(...$eventData);
  143.         $this->eventDispatcher->dispatch($beforeEvent);
  144.         if ($beforeEvent->isPropagationStopped()) {
  145.             return $beforeEvent->getRevealedLocation();
  146.         }
  147.         $revealedLocation $beforeEvent->hasRevealedLocation()
  148.             ? $beforeEvent->getRevealedLocation()
  149.             : $this->innerService->unhideLocation($location);
  150.         $this->eventDispatcher->dispatch(
  151.             new UnhideLocationEvent($revealedLocation, ...$eventData)
  152.         );
  153.         return $revealedLocation;
  154.     }
  155.     public function moveSubtree(
  156.         Location $location,
  157.         Location $newParentLocation
  158.     ): void {
  159.         $eventData = [
  160.             $location,
  161.             $newParentLocation,
  162.         ];
  163.         $beforeEvent = new BeforeMoveSubtreeEvent(...$eventData);
  164.         $this->eventDispatcher->dispatch($beforeEvent);
  165.         if ($beforeEvent->isPropagationStopped()) {
  166.             return;
  167.         }
  168.         $this->innerService->moveSubtree($location$newParentLocation);
  169.         $this->eventDispatcher->dispatch(
  170.             new MoveSubtreeEvent(...$eventData)
  171.         );
  172.     }
  173.     public function deleteLocation(Location $location): void
  174.     {
  175.         $eventData = [$location];
  176.         $beforeEvent = new BeforeDeleteLocationEvent(...$eventData);
  177.         $this->eventDispatcher->dispatch($beforeEvent);
  178.         if ($beforeEvent->isPropagationStopped()) {
  179.             return;
  180.         }
  181.         $this->innerService->deleteLocation($location);
  182.         $this->eventDispatcher->dispatch(
  183.             new DeleteLocationEvent(...$eventData)
  184.         );
  185.     }
  186. }