vendor/sulu/sulu/src/Sulu/Component/DocumentManager/Subscriber/Phpcr/FindSubscriber.php line 64

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Sulu.
  4. *
  5. * (c) Sulu GmbH
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Sulu\Component\DocumentManager\Subscriber\Phpcr;
  11. use Sulu\Component\DocumentManager\Event\ConfigureOptionsEvent;
  12. use Sulu\Component\DocumentManager\Event\FindEvent;
  13. use Sulu\Component\DocumentManager\Event\HydrateEvent;
  14. use Sulu\Component\DocumentManager\Events;
  15. use Sulu\Component\DocumentManager\Exception\DocumentManagerException;
  16. use Sulu\Component\DocumentManager\Exception\DocumentNotFoundException;
  17. use Sulu\Component\DocumentManager\MetadataFactoryInterface;
  18. use Sulu\Component\DocumentManager\NodeManager;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. /**
  22. * This class is responsible for finding documents.
  23. */
  24. class FindSubscriber implements EventSubscriberInterface
  25. {
  26. public function __construct(
  27. private MetadataFactoryInterface $metadataFactory,
  28. private NodeManager $nodeManager,
  29. private EventDispatcherInterface $eventDispatcher,
  30. ) {
  31. }
  32. public static function getSubscribedEvents()
  33. {
  34. return [
  35. Events::FIND => ['handleFind', 500],
  36. Events::CONFIGURE_OPTIONS => 'configureOptions',
  37. ];
  38. }
  39. public function configureOptions(ConfigureOptionsEvent $event)
  40. {
  41. $options = $event->getOptions();
  42. $options->setDefaults([
  43. 'type' => null,
  44. ]);
  45. }
  46. /**
  47. * @throws DocumentManagerException
  48. * @throws DocumentNotFoundException
  49. */
  50. public function handleFind(FindEvent $event)
  51. {
  52. $options = $event->getOptions();
  53. $aliasOrClass = $options['type'];
  54. $node = $this->nodeManager->find($event->getId());
  55. $hydrateEvent = new HydrateEvent($node, $event->getLocale(), $options);
  56. $this->eventDispatcher->dispatch($hydrateEvent, Events::HYDRATE);
  57. $document = $hydrateEvent->getDocument();
  58. if ($aliasOrClass) {
  59. $this->checkAliasOrClass($aliasOrClass, $document);
  60. }
  61. $event->setDocument($hydrateEvent->getDocument());
  62. }
  63. private function checkAliasOrClass($aliasOrClass, $document)
  64. {
  65. if ($this->metadataFactory->hasAlias($aliasOrClass)) {
  66. $class = $this->metadataFactory->getMetadataForAlias($aliasOrClass)->getClass();
  67. } elseif (!\class_exists($aliasOrClass)) {
  68. throw new DocumentManagerException(\sprintf(
  69. 'Unknown class specified and no alias exists for "%s", known aliases: "%s"',
  70. $aliasOrClass, \implode('", "', $this->metadataFactory->getAliases())
  71. ));
  72. } else {
  73. $class = $aliasOrClass;
  74. }
  75. if (\get_class($document) !== $class) {
  76. throw new DocumentNotFoundException(\sprintf(
  77. 'Requested document of type "%s" but got document of type "%s"',
  78. $aliasOrClass,
  79. \get_class($document)
  80. ));
  81. }
  82. }
  83. }