vendor/sulu/sulu/src/Sulu/Bundle/PageBundle/Search/EventSubscriber/BlameTimestampSubscriber.php line 79

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\Bundle\PageBundle\Search\EventSubscriber;
  11. use Doctrine\ORM\EntityManager;
  12. use Massive\Bundle\SearchBundle\Search\Event\HitEvent;
  13. use Massive\Bundle\SearchBundle\Search\Event\PreIndexEvent;
  14. use Massive\Bundle\SearchBundle\Search\Factory;
  15. use Massive\Bundle\SearchBundle\Search\SearchEvents;
  16. use Sulu\Bundle\SearchBundle\Search\Document;
  17. use Sulu\Component\Persistence\Model\TimestampableInterface;
  18. use Sulu\Component\Persistence\Model\UserBlameInterface;
  19. use Sulu\Component\Security\Authentication\UserInterface;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. /**
  22.  * Add blame (creator, changor) and timestamp (created, changed) to
  23.  * the document before it is indexed.
  24.  *
  25.  * Works for objects implementing interfaces (UserBlameInterface and
  26.  * TimestampableInterface).
  27.  */
  28. class BlameTimestampSubscriber implements EventSubscriberInterface
  29. {
  30.     /**
  31.      * @var Factory
  32.      */
  33.     private $factory;
  34.     /**
  35.      * @var EntityManager
  36.      */
  37.     private $entityManager;
  38.     public function __construct(Factory $factoryEntityManager $entityManager)
  39.     {
  40.         $this->factory $factory;
  41.         $this->entityManager $entityManager;
  42.     }
  43.     public static function getSubscribedEvents()
  44.     {
  45.         return [
  46.             SearchEvents::PRE_INDEX => 'handleBlameTimestamp',
  47.             SearchEvents::HIT => 'handleBlameTimestampHitMapping',
  48.         ];
  49.     }
  50.     /**
  51.      * Map blame and timestamp information to the search document.
  52.      */
  53.     public function handleBlameTimestamp(PreIndexEvent $event)
  54.     {
  55.         $subject $event->getSubject();
  56.         $document $event->getDocument();
  57.         if ($subject instanceof UserBlameInterface) {
  58.             $this->mapCreatorAndChanger($document$subject->getCreator(), $subject->getChanger());
  59.         }
  60.         if ($subject instanceof TimestampableInterface) {
  61.             $this->mapTimestamp($document$subject->getCreated(), $subject->getChanged());
  62.         }
  63.     }
  64.     /**
  65.      * Map the changer and created from the field data to
  66.      * the search document (we don't include the field data in the search result API).
  67.      */
  68.     public function handleBlameTimestampHitMapping(HitEvent $event)
  69.     {
  70.         $document $event->getHit()->getDocument();
  71.         $this->doHandleBlameTimestampHitMapping($document);
  72.     }
  73.     private function doHandleBlameTimestampHitMapping(Document $document)
  74.     {
  75.         $document->setCreatorName($this->getFieldValue($document'creator'));
  76.         $document->setChangerName($this->getFieldValue($document'changer'));
  77.         $document->setCreatorId($this->getFieldValue($document'creator_id'));
  78.         $document->setChangerId($this->getFieldValue($document'changer_id'));
  79.         $document->setCreated($this->getFieldValue($document'created'));
  80.         $document->setChanged($this->getFieldValue($document'changed'));
  81.     }
  82.     /**
  83.      * Return the named field from the document or return null.
  84.      */
  85.     private function getFieldValue($document$fieldName)
  86.     {
  87.         if (false === $document->hasField($fieldName)) {
  88.             return;
  89.         }
  90.         return $document->getField($fieldName)->getValue();
  91.     }
  92.     /**
  93.      * Map timestamps to the search document.
  94.      *
  95.      * @param \DateTime $created
  96.      * @param \DateTime $changed
  97.      */
  98.     private function mapTimestamp(Document $document, \DateTime $created null, \DateTime $changed null)
  99.     {
  100.         $document->addField(
  101.             $this->factory->createField('created'$created $created->format('c') : null'string')
  102.         );
  103.         $document->addField(
  104.             $this->factory->createField('changed'$changed $changed->format('c') : null'string')
  105.         );
  106.     }
  107.     /**
  108.      * Map the creator and changer to the document.
  109.      *
  110.      * @param UserInterface $creator
  111.      * @param UserInterface $changer
  112.      */
  113.     private function mapCreatorAndChanger(Document $documentUserInterface $creator nullUserInterface $changer null)
  114.     {
  115.         $document->addField(
  116.             $this->factory->createField('changer'$changer $changer->getUsername() : null'string')
  117.         );
  118.         $document->addField(
  119.             $this->factory->createField('changer_id'$changer $changer->getId() : null'string')
  120.         );
  121.         $document->addField(
  122.             $this->factory->createField('creator'$creator $creator->getUsername() : null'string')
  123.         );
  124.         $document->addField(
  125.             $this->factory->createField('creator_id'$creator $creator->getId() : null'string')
  126.         );
  127.     }
  128. }