Skip to content
Snippets Groups Projects
BaseIndexProvider.php 1.78 KiB
Newer Older
<?php

namespace IQDEV\ElasticSearch\Indexer;

use IQDEV\ElasticSearch\Configuration;
use IQDEV\ElasticSearch\Document\ProductDocument;
use IQDEV\ElasticSearch\Facet\FacetCategory;
use IQDEV\ElasticSearch\Facet\FacetKeyword;

class BaseIndexProvider implements IndexProvider
{
    private array $products;
Pavel's avatar
Pavel committed
    private ?int $size = null;
    private ?int $limit = null;
    private Configuration $configuration;

    public function __construct($products, $configuration)
    {
        $this->configuration = $configuration;
        $this->products = $products;
    }

    public function get(): \Generator
    {
        foreach ($this->products as $product) {
Pavel's avatar
Pavel committed
            $document = new ProductDocument(new FacetCategory($product['category']));
            $document->setAdditionData($product['data'] ?? []);
            foreach ($product['properties'] as $type => $values) {
                foreach ($values as $key => $prop) {
                    if ($type === 'number') {
Nikita Chernykh's avatar
Nikita Chernykh committed
                        $document->getNumberFacets()->add(new FacetKeyword($key, $prop));
                    } else {
                        $document->getKeywordFacets()->add(new FacetKeyword($key, $prop));
                    }
                }
            }
Pavel's avatar
Pavel committed
            $document->setSearchContent($product['name']);
Pavel's avatar
Pavel committed
            yield new AddIndex(
Pavel's avatar
Pavel committed

    public function setBatchSize(int $size): void
    {
        $this->size = $size;
    }

    public function getBatchSize(): ?int
    {
        return $this->size;
    }

    public function setLimit(int $limit): void
    {
        $this->limit = $limit;
    }

    public function getLimit(): ?int
    {
        return $this->limit;
    }
}