vendor/ibexa/elasticsearch/src/lib/DocumentMapper/EventSubscriber/UserContentDocumentMapper.php line 32

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 Ibexa\Elasticsearch\DocumentMapper\EventSubscriber;
  8. use Ibexa\Contracts\Core\Persistence\Content as SPIContent;
  9. use Ibexa\Contracts\Core\Search\Field;
  10. use Ibexa\Contracts\Core\Search\FieldType\IdentifierField;
  11. use Ibexa\Contracts\Elasticsearch\Mapping\Event\ContentIndexCreateEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. final class UserContentDocumentMapper implements EventSubscriberInterface
  14. {
  15.     /** @internal */
  16.     public const HASHING_ALGORITHM 'sha256';
  17.     public static function getSubscribedEvents(): array
  18.     {
  19.         return [
  20.             ContentIndexCreateEvent::class => [
  21.                 'onContentIndexCreate',
  22.                 -100,
  23.             ],
  24.         ];
  25.     }
  26.     public function onContentIndexCreate(ContentIndexCreateEvent $event): void
  27.     {
  28.         $content $event->getContent();
  29.         $field $this->getUserField($content);
  30.         if ($field === null) {
  31.             return;
  32.         }
  33.         $document $event->getDocument();
  34.         if (isset($field->value->externalData['login'])) {
  35.             $document->fields[] = new Field(
  36.                 'user_login',
  37.                 hash(self::HASHING_ALGORITHM$field->value->externalData['login']),
  38.                 new IdentifierField()
  39.             );
  40.         }
  41.         if (isset($field->value->externalData['email'])) {
  42.             $document->fields[] = new Field(
  43.                 'user_email',
  44.                 hash(self::HASHING_ALGORITHM$field->value->externalData['email']),
  45.                 new IdentifierField()
  46.             );
  47.         }
  48.     }
  49.     private function getUserField(SPIContent $content): ?SPIContent\Field
  50.     {
  51.         foreach ($content->fields as $field) {
  52.             if ($field->type === 'ezuser') {
  53.                 return $field;
  54.             }
  55.         }
  56.         return null;
  57.     }
  58. }