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

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.     /**
  27.      * @var MetadataFactoryInterface
  28.      */
  29.     private $metadataFactory;
  30.     /**
  31.      * @var NodeManager
  32.      */
  33.     private $nodeManager;
  34.     /**
  35.      * @var EventDispatcherInterface
  36.      */
  37.     private $eventDispatcher;
  38.     public function __construct(
  39.         MetadataFactoryInterface $metadataFactory,
  40.         NodeManager $nodeManager,
  41.         EventDispatcherInterface $eventDispatcher
  42.     ) {
  43.         $this->metadataFactory $metadataFactory;
  44.         $this->nodeManager $nodeManager;
  45.         $this->eventDispatcher $eventDispatcher;
  46.     }
  47.     public static function getSubscribedEvents()
  48.     {
  49.         return [
  50.             Events::FIND => ['handleFind'500],
  51.             Events::CONFIGURE_OPTIONS => 'configureOptions',
  52.         ];
  53.     }
  54.     public function configureOptions(ConfigureOptionsEvent $event)
  55.     {
  56.         $options $event->getOptions();
  57.         $options->setDefaults([
  58.             'type' => null,
  59.         ]);
  60.     }
  61.     /**
  62.      * @throws DocumentManagerException
  63.      * @throws DocumentNotFoundException
  64.      */
  65.     public function handleFind(FindEvent $event)
  66.     {
  67.         $options $event->getOptions();
  68.         $aliasOrClass $options['type'];
  69.         $node $this->nodeManager->find($event->getId());
  70.         $hydrateEvent = new HydrateEvent($node$event->getLocale(), $options);
  71.         $this->eventDispatcher->dispatch($hydrateEventEvents::HYDRATE);
  72.         $document $hydrateEvent->getDocument();
  73.         if ($aliasOrClass) {
  74.             $this->checkAliasOrClass($aliasOrClass$document);
  75.         }
  76.         $event->setDocument($hydrateEvent->getDocument());
  77.     }
  78.     private function checkAliasOrClass($aliasOrClass$document)
  79.     {
  80.         if ($this->metadataFactory->hasAlias($aliasOrClass)) {
  81.             $class $this->metadataFactory->getMetadataForAlias($aliasOrClass)->getClass();
  82.         } elseif (!\class_exists($aliasOrClass)) {
  83.             throw new DocumentManagerException(\sprintf(
  84.                 'Unknown class specified and no alias exists for "%s", known aliases: "%s"',
  85.                 $aliasOrClass\implode('", "'$this->metadataFactory->getAliases())
  86.             ));
  87.         } else {
  88.             $class $aliasOrClass;
  89.         }
  90.         if (\get_class($document) !== $class) {
  91.             throw new DocumentNotFoundException(\sprintf(
  92.                 'Requested document of type "%s" but got document of type "%s"',
  93.                 $aliasOrClass,
  94.                 \get_class($document)
  95.             ));
  96.         }
  97.     }
  98. }