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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the FOSElasticaBundle package.
  4.  *
  5.  * (c) FriendsOfSymfony <http://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\Type;
  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. class ObjectPersister implements ObjectPersisterInterface
  23. {
  24.     protected $type;
  25.     protected $transformer;
  26.     protected $objectClass;
  27.     protected $fields;
  28.     protected $logger;
  29.     private $options;
  30.     /**
  31.      * @param Type                                $type
  32.      * @param ModelToElasticaTransformerInterface $transformer
  33.      * @param string                              $objectClass
  34.      * @param array                               $fields
  35.      */
  36.     public function __construct(Type $typeModelToElasticaTransformerInterface $transformer$objectClass, array $fields, array $options = [])
  37.     {
  38.         $this->type $type;
  39.         $this->transformer $transformer;
  40.         $this->objectClass $objectClass;
  41.         $this->fields $fields;
  42.         $this->options $options;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function handlesObject($object)
  48.     {
  49.         return $object instanceof $this->objectClass;
  50.     }
  51.     /**
  52.      * @param LoggerInterface $logger
  53.      */
  54.     public function setLogger(LoggerInterface $logger)
  55.     {
  56.         $this->logger $logger;
  57.     }
  58.     /**
  59.      * {@inheritdoc}
  60.      */
  61.     public function insertOne($object)
  62.     {
  63.         $this->insertMany([$object]);
  64.     }
  65.     /**
  66.      * {@inheritdoc}
  67.      */
  68.     public function replaceOne($object)
  69.     {
  70.         $this->replaceMany([$object]);
  71.     }
  72.     /**
  73.      * {@inheritdoc}
  74.      */
  75.     public function deleteOne($object)
  76.     {
  77.         $this->deleteMany([$object]);
  78.     }
  79.     /**
  80.      * {@inheritdoc}
  81.      */
  82.     public function deleteById($id$routing false)
  83.     {
  84.         $this->deleteManyByIdentifiers([$id], $routing);
  85.     }
  86.     /**
  87.      * {@inheritdoc}
  88.      */
  89.     public function insertMany(array $objects)
  90.     {
  91.         $documents = [];
  92.         foreach ($objects as $object) {
  93.             $documents[] = $this->transformToElasticaDocument($object);
  94.         }
  95.         try {
  96.             $this->type->addDocuments($documents$this->options);
  97.         } catch (BulkException $e) {
  98.             $this->log($e);
  99.         }
  100.     }
  101.     /**
  102.      * {@inheritdoc}
  103.      */
  104.     public function replaceMany(array $objects)
  105.     {
  106.         $documents = [];
  107.         foreach ($objects as $object) {
  108.             $document $this->transformToElasticaDocument($object);
  109.             $document->setDocAsUpsert(true);
  110.             $documents[] = $document;
  111.         }
  112.         try {
  113.             $this->type->updateDocuments($documents$this->options);
  114.         } catch (BulkException $e) {
  115.             $this->log($e);
  116.         }
  117.     }
  118.     /**
  119.      * {@inheritdoc}
  120.      */
  121.     public function deleteMany(array $objects)
  122.     {
  123.         $documents = [];
  124.         foreach ($objects as $object) {
  125.             $documents[] = $this->transformToElasticaDocument($object);
  126.         }
  127.         try {
  128.             $this->type->deleteDocuments($documents);
  129.         } catch (BulkException $e) {
  130.             $this->log($e);
  131.         }
  132.     }
  133.     /**
  134.      * {@inheritdoc}
  135.      */
  136.     public function deleteManyByIdentifiers(array $identifiers$routing false)
  137.     {
  138.         try {
  139.             $this->type->deleteIds($identifiers$routing);
  140.         } catch (BulkException $e) {
  141.             $this->log($e);
  142.         }
  143.     }
  144.     /**
  145.      * Transforms an object to an elastica document.
  146.      *
  147.      * @param object $object
  148.      *
  149.      * @return Document the elastica document
  150.      */
  151.     public function transformToElasticaDocument($object)
  152.     {
  153.         return $this->transformer->transform($object$this->fields);
  154.     }
  155.     /**
  156.      * Log exception if logger defined for persister belonging to the current listener, otherwise re-throw.
  157.      *
  158.      * @param BulkException $e
  159.      *
  160.      * @throws BulkException
  161.      */
  162.     private function log(BulkException $e)
  163.     {
  164.         if (!$this->logger) {
  165.             throw $e;
  166.         }
  167.         $this->logger->error($e);
  168.     }
  169. }