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

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\EntityManagerInterface;
  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. public function __construct(private Factory $factory, private EntityManagerInterface $entityManager)
  31. {
  32. }
  33. public static function getSubscribedEvents()
  34. {
  35. return [
  36. SearchEvents::PRE_INDEX => 'handleBlameTimestamp',
  37. SearchEvents::HIT => 'handleBlameTimestampHitMapping',
  38. ];
  39. }
  40. /**
  41. * Map blame and timestamp information to the search document.
  42. */
  43. public function handleBlameTimestamp(PreIndexEvent $event)
  44. {
  45. $subject = $event->getSubject();
  46. $document = $event->getDocument();
  47. if ($subject instanceof UserBlameInterface) {
  48. $this->mapCreatorAndChanger($document, $subject->getCreator(), $subject->getChanger());
  49. }
  50. if ($subject instanceof TimestampableInterface) {
  51. $this->mapTimestamp($document, $subject->getCreated(), $subject->getChanged());
  52. }
  53. }
  54. /**
  55. * Map the changer and created from the field data to
  56. * the search document (we don't include the field data in the search result API).
  57. */
  58. public function handleBlameTimestampHitMapping(HitEvent $event)
  59. {
  60. $document = $event->getHit()->getDocument();
  61. $this->doHandleBlameTimestampHitMapping($document);
  62. }
  63. private function doHandleBlameTimestampHitMapping(Document $document)
  64. {
  65. $document->setCreatorName($this->getFieldValue($document, 'creator'));
  66. $document->setChangerName($this->getFieldValue($document, 'changer'));
  67. $document->setCreatorId($this->getFieldValue($document, 'creator_id'));
  68. $document->setChangerId($this->getFieldValue($document, 'changer_id'));
  69. $document->setCreated($this->getFieldValue($document, 'created'));
  70. $document->setChanged($this->getFieldValue($document, 'changed'));
  71. }
  72. /**
  73. * Return the named field from the document or return null.
  74. */
  75. private function getFieldValue($document, $fieldName)
  76. {
  77. if (false === $document->hasField($fieldName)) {
  78. return;
  79. }
  80. return $document->getField($fieldName)->getValue();
  81. }
  82. /**
  83. * Map timestamps to the search document.
  84. */
  85. private function mapTimestamp(Document $document, ?\DateTime $created = null, ?\DateTime $changed = null)
  86. {
  87. $document->addField(
  88. $this->factory->createField('created', $created ? $created->format('c') : null, 'string')
  89. );
  90. $document->addField(
  91. $this->factory->createField('changed', $changed ? $changed->format('c') : null, 'string')
  92. );
  93. }
  94. /**
  95. * Map the creator and changer to the document.
  96. */
  97. private function mapCreatorAndChanger(Document $document, ?UserInterface $creator = null, ?UserInterface $changer = null)
  98. {
  99. $document->addField(
  100. $this->factory->createField('changer', $changer ? $changer->getUserIdentifier() : null, 'string')
  101. );
  102. $document->addField(
  103. $this->factory->createField('changer_id', $changer ? $changer->getId() : null, 'string')
  104. );
  105. $document->addField(
  106. $this->factory->createField('creator', $creator ? $creator->getUserIdentifier() : null, 'string')
  107. );
  108. $document->addField(
  109. $this->factory->createField('creator_id', $creator ? $creator->getId() : null, 'string')
  110. );
  111. }
  112. }