vendor/friendsofsymfony/elastica-bundle/src/Persister/ObjectPersister.php line 141

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSElasticaBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace FOS\ElasticaBundle\Persister;
  11. use Elastica\Document;
  12. use Elastica\Exception\BulkException;
  13. use Elastica\Index;
  14. use FOS\ElasticaBundle\Transformer\ModelToElasticaTransformerInterface;
  15. use Psr\Log\LoggerInterface;
  16. /**
  17.  * Inserts, replaces and deletes single documents in an elastica type
  18.  * Accepts domain model objects and converts them to elastica documents.
  19.  *
  20.  * @author Thibault Duplessis <thibault.duplessis@gmail.com>
  21.  *
  22.  * @phpstan-type TOptions = array<string, mixed>
  23.  * @phpstan-import-type TFields from ModelToElasticaTransformerInterface
  24.  *
  25.  * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html for TOptions description
  26.  */
  27. class ObjectPersister implements ObjectPersisterInterface
  28. {
  29.     /**
  30.      * @var Index
  31.      */
  32.     protected $index;
  33.     /**
  34.      * @var ModelToElasticaTransformerInterface
  35.      */
  36.     protected $transformer;
  37.     /**
  38.      * @var class-string
  39.      */
  40.     protected $objectClass;
  41.     /**
  42.      * @var array
  43.      * @phpstan-var TFields
  44.      */
  45.     protected $fields;
  46.     /**
  47.      * @var ?LoggerInterface
  48.      */
  49.     protected $logger;
  50.     /**
  51.      * @var array
  52.      * @phpstan-var TOptions
  53.      */
  54.     private $options;
  55.     /**
  56.      * @param class-string $objectClass
  57.      * @phpstan-param TFields $fields
  58.      * @phpstan-param TOptions $options
  59.      */
  60.     public function __construct(Index $indexModelToElasticaTransformerInterface $transformerstring $objectClass, array $fields, array $options = [])
  61.     {
  62.         $this->index $index;
  63.         $this->transformer $transformer;
  64.         $this->objectClass $objectClass;
  65.         $this->fields $fields;
  66.         $this->options $options;
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public function handlesObject($object): bool
  72.     {
  73.         return $object instanceof $this->objectClass;
  74.     }
  75.     /**
  76.      * @return void
  77.      */
  78.     public function setLogger(LoggerInterface $logger)
  79.     {
  80.         $this->logger $logger;
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function insertOne($object)
  86.     {
  87.         $this->insertMany([$object]);
  88.     }
  89.     /**
  90.      * {@inheritdoc}
  91.      */
  92.     public function replaceOne($object)
  93.     {
  94.         $this->replaceMany([$object]);
  95.     }
  96.     /**
  97.      * {@inheritdoc}
  98.      */
  99.     public function deleteOne($object)
  100.     {
  101.         $this->deleteMany([$object]);
  102.     }
  103.     /**
  104.      * {@inheritdoc}
  105.      */
  106.     public function deleteById($id$routing false)
  107.     {
  108.         $this->deleteManyByIdentifiers([$id], $routing);
  109.     }
  110.     /**
  111.      * {@inheritdoc}
  112.      */
  113.     public function insertMany(array $objects)
  114.     {
  115.         $documents = [];
  116.         foreach ($objects as $object) {
  117.             $documents[] = $this->transformToElasticaDocument($object);
  118.         }
  119.         try {
  120.             $this->index->addDocuments($documents$this->options);
  121.         } catch (BulkException $e) {
  122.             $this->log($e);
  123.         }
  124.     }
  125.     /**
  126.      * {@inheritdoc}
  127.      */
  128.     public function replaceMany(array $objects)
  129.     {
  130.         $documents = [];
  131.         foreach ($objects as $object) {
  132.             $document $this->transformToElasticaDocument($object);
  133.             $document->setDocAsUpsert(true);
  134.             $documents[] = $document;
  135.         }
  136.         try {
  137.             $this->index->updateDocuments($documents$this->options);
  138.         } catch (BulkException $e) {
  139.             $this->log($e);
  140.         }
  141.     }
  142.     /**
  143.      * {@inheritdoc}
  144.      */
  145.     public function deleteMany(array $objects)
  146.     {
  147.         $documents = [];
  148.         foreach ($objects as $object) {
  149.             $documents[] = $this->transformToElasticaDocument($object);
  150.         }
  151.         try {
  152.             $this->index->deleteDocuments($documents);
  153.         } catch (BulkException $e) {
  154.             $this->log($e);
  155.         }
  156.     }
  157.     /**
  158.      * {@inheritdoc}
  159.      */
  160.     public function deleteManyByIdentifiers(array $identifiers$routing false)
  161.     {
  162.         try {
  163.             $this->index->getClient()->deleteIds($identifiers$this->index->getName(), $routing);
  164.         } catch (BulkException $e) {
  165.             $this->log($e);
  166.         }
  167.     }
  168.     /**
  169.      * Transforms an object to an elastica document.
  170.      */
  171.     public function transformToElasticaDocument(object $object): Document
  172.     {
  173.         return $this->transformer->transform($object$this->fields);
  174.     }
  175.     /**
  176.      * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw.
  177.      *
  178.      * @throws BulkException
  179.      *
  180.      * @return void
  181.      */
  182.     private function log(BulkException $e)
  183.     {
  184.         if (!$this->logger) {
  185.             throw $e;
  186.         }
  187.         $this->logger->error($e);
  188.     }
  189. }