vendor/friendsofsymfony/elastica-bundle/Provider/AbstractProvider.php line 8

Open in your IDE?
  1. <?php
  2. namespace FOS\ElasticaBundle\Provider;
  3. use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
  4. use Symfony\Component\OptionsResolver\OptionsResolver;
  5. @trigger_error(sprintf('The %s class is deprecated since version 4.1 and will be removed in 5.0.'AbstractProvider::class), E_USER_DEPRECATED);
  6. /**
  7.  * @deprecated since 4.1 will be removed in 5.x. Use PagerProvider instead
  8.  * 
  9.  * AbstractProvider.
  10.  */
  11. abstract class AbstractProvider implements ProviderInterface
  12. {
  13.     /**
  14.      * @var array
  15.      */
  16.     protected $baseOptions;
  17.     /**
  18.      * @var string
  19.      */
  20.     protected $objectClass;
  21.     /**
  22.      * @var ObjectPersisterInterface
  23.      */
  24.     protected $objectPersister;
  25.     /**
  26.      * @var OptionsResolver
  27.      */
  28.     protected $resolver;
  29.     /**
  30.      * @var IndexableInterface
  31.      */
  32.     private $indexable;
  33.     /**
  34.      * Constructor.
  35.      *
  36.      * @param ObjectPersisterInterface $objectPersister
  37.      * @param IndexableInterface       $indexable
  38.      * @param string                   $objectClass
  39.      * @param array                    $baseOptions
  40.      */
  41.     public function __construct(
  42.         ObjectPersisterInterface $objectPersister,
  43.         IndexableInterface $indexable,
  44.         $objectClass,
  45.         array $baseOptions = array()
  46.     ) {
  47.         $this->baseOptions $baseOptions;
  48.         $this->indexable $indexable;
  49.         $this->objectClass $objectClass;
  50.         $this->objectPersister $objectPersister;
  51.         $this->resolver = new OptionsResolver();
  52.         $this->configureOptions();
  53.     }
  54.     /**
  55.      * {@inheritDoc}
  56.      */
  57.     public function populate(\Closure $loggerClosure null, array $options = array())
  58.     {
  59.         $options $this->resolveOptions($options);
  60.         $logger = !$options['debug_logging'] ?
  61.             $this->disableLogging() :
  62.             null;
  63.         $this->doPopulate($options$loggerClosure);
  64.         if (null !== $logger) {
  65.             $this->enableLogging($logger);
  66.         }
  67.     }
  68.     /**
  69.      * Disables logging and returns the logger that was previously set.
  70.      *
  71.      * @return mixed
  72.      */
  73.     abstract protected function disableLogging();
  74.     /**
  75.      * Perform actual population.
  76.      *
  77.      * @param array $options
  78.      * @param \Closure $loggerClosure
  79.      */
  80.     abstract protected function doPopulate($options, \Closure $loggerClosure null);
  81.     /**
  82.      * Reenables the logger with the previously returned logger from disableLogging();.
  83.      *
  84.      * @param mixed $logger
  85.      *
  86.      * @return mixed
  87.      */
  88.     abstract protected function enableLogging($logger);
  89.     /**
  90.      * Configures the option resolver.
  91.      */
  92.     protected function configureOptions()
  93.     {
  94.         $this->resolver->setDefaults(array(
  95.             'reset' => true,
  96.             'batch_size' => 100,
  97.             'skip_indexable_check' => false,
  98.         ));
  99.         $this->resolver->setRequired(array(
  100.             'indexName',
  101.             'typeName',
  102.         ));
  103.     }
  104.     /**
  105.      * Filters objects away if they are not indexable.
  106.      *
  107.      * @param array $options
  108.      * @param array $objects
  109.      * @return array
  110.      */
  111.     protected function filterObjects(array $options, array $objects)
  112.     {
  113.         if ($options['skip_indexable_check']) {
  114.             return $objects;
  115.         }
  116.         $index $options['indexName'];
  117.         $type $options['typeName'];
  118.         $return = array();
  119.         foreach ($objects as $object) {
  120.             if (!$this->indexable->isObjectIndexable($index$type$object)) {
  121.                 continue;
  122.             }
  123.             $return[] = $object;
  124.         }
  125.         return $return;
  126.     }
  127.     /**
  128.      * Merges the base options provided by the class with options passed to the populate
  129.      * method and runs them through the resolver.
  130.      *
  131.      * @param array $options
  132.      *
  133.      * @return array
  134.      */
  135.     protected function resolveOptions(array $options)
  136.     {
  137.         return $this->resolver->resolve(array_merge($this->baseOptions$options));
  138.     }
  139. }