<commit_before><?php

namespace Algolia\AlgoliaSearch\Model\Indexer;

use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
use Algolia\AlgoliaSearch\Helper\Data;
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
use Algolia\AlgoliaSearch\Model\Queue;
use Magento;
use Magento\Framework\Message\ManagerInterface;
use Magento\Store\Model\StoreManagerInterface;

class Product implements Magento\Framework\Indexer\ActionInterface, Magento\Framework\Mview\ActionInterface
{
    private $storeManager;
    private $productHelper;
    private $algoliaHelper;
    private $fullAction;
    private $configHelper;
    private $queue;
    private $messageManager;

    public function __construct(StoreManagerInterface $storeManager,
                                ProductHelper $productHelper,
                                Data $helper,
                                AlgoliaHelper $algoliaHelper,
                                ConfigHelper $configHelper,
                                Queue $queue,
                                ManagerInterface $messageManager)
    {
        $this->fullAction = $helper;
        $this->storeManager = $storeManager;
        $this->productHelper = $productHelper;
        $this->algoliaHelper = $algoliaHelper;
        $this->configHelper = $configHelper;
        $this->queue = $queue;
        $this->messageManager = $messageManager;
    }

    public function execute($productIds)
    {
        if (!$this->configHelper->getApplicationID() || !$this->configHelper->getAPIKey() || !$this->configHelper->getSearchOnlyAPIKey()) {
            $errorMessage = 'Algolia reindexing failed: You need to configure your Algolia credentials in Stores > Configuration > Algolia Search.';

            if (php_sapi_name() === 'cli') {
                throw new \Exception($errorMessage);
            }

            $this->messageManager->addErrorMessage($errorMessage);

            return;
        }

        $storeIds = array_keys($this->storeManager->getStores());

        foreach ($storeIds as $storeId) {
            if (is_array($productIds) && count($productIds) > 0) {
                $this->queue->addToQueue($this->fullAction, 'rebuildStoreProductIndex', ['store_id' => $storeId, 'product_ids' => $productIds], count($productIds));

                return;
            }

            $useTmpIndex = $this->configHelper->isQueueActive($storeId);

            $collection = $this->productHelper->getProductCollectionQuery($storeId, $productIds, $useTmpIndex);
            $size = $collection->getSize();

            if (!empty($productIds)) {
                $size = max(count($productIds), $size);
            }

            $productsPerPage = $this->configHelper->getNumberOfElementByPage();
            $pages = ceil($size / $productsPerPage);

            $this->queue->addToQueue($this->fullAction, 'saveConfigurationToAlgolia', [
                'store_id' => $storeId,
                'useTmpIndex' => $useTmpIndex,
            ]);

            for ($i = 1; $i <= $pages; $i++) {
                $data = [
                    'store_id' => $storeId,
                    'product_ids' => $productIds,
                    'page' => $i,
                    'page_size' => $productsPerPage,
                    'useTmpIndex' => $useTmpIndex,
                ];

                $this->queue->addToQueue($this->fullAction, 'rebuildProductIndex', $data, $productsPerPage);<commit_msg>Fix interating over stores while indexing products<commit_after><?php

namespace Algolia\AlgoliaSearch\Model\Indexer;

use Algolia\AlgoliaSearch\Helper\AlgoliaHelper;
use Algolia\AlgoliaSearch\Helper\ConfigHelper;
use Algolia\AlgoliaSearch\Helper\Data;
use Algolia\AlgoliaSearch\Helper\Entity\ProductHelper;
use Algolia\AlgoliaSearch\Model\Queue;
use Magento;
use Magento\Framework\Message\ManagerInterface;
use Magento\Store\Model\StoreManagerInterface;

class Product implements Magento\Framework\Indexer\ActionInterface, Magento\Framework\Mview\ActionInterface
{
    private $storeManager;
    private $productHelper;
    private $algoliaHelper;
    private $fullAction;
    private $configHelper;
    private $queue;
    private $messageManager;

    public function __construct(StoreManagerInterface $storeManager,
                                ProductHelper $productHelper,
                                Data $helper,
                                AlgoliaHelper $algoliaHelper,
                                ConfigHelper $configHelper,
                                Queue $queue,
                                ManagerInterface $messageManager)
    {
        $this->fullAction = $helper;
        $this->storeManager = $storeManager;
        $this->productHelper = $productHelper;
        $this->algoliaHelper = $algoliaHelper;
        $this->configHelper = $configHelper;
        $this->queue = $queue;
        $this->messageManager = $messageManager;
    }

    public function execute($productIds)
    {
        if (!$this->configHelper->getApplicationID() || !$this->configHelper->getAPIKey() || !$this->configHelper->getSearchOnlyAPIKey()) {
            $errorMessage = 'Algolia reindexing failed: You need to configure your Algolia credentials in Stores > Configuration > Algolia Search.';

            if (php_sapi_name() === 'cli') {
                throw new \Exception($errorMessage);
            }

            $this->messageManager->addErrorMessage($errorMessage);

            return;
        }

        $storeIds = array_keys($this->storeManager->getStores());

        foreach ($storeIds as $storeId) {
            if (is_array($productIds) && count($productIds) > 0) {
                $this->queue->addToQueue($this->fullAction, 'rebuildStoreProductIndex', ['store_id' => $storeId, 'product_ids' => $productIds], count($productIds));
                continue;
            }

            $useTmpIndex = $this->configHelper->isQueueActive($storeId);

            $collection = $this->productHelper->getProductCollectionQuery($storeId, $productIds, $useTmpIndex);
            $size = $collection->getSize();

            if (!empty($productIds)) {
                $size = max(count($productIds), $size);
            }

            $productsPerPage = $this->configHelper->getNumberOfElementByPage();
            $pages = ceil($size / $productsPerPage);

            $this->queue->addToQueue($this->fullAction, 'saveConfigurationToAlgolia', [
                'store_id' => $storeId,
                'useTmpIndex' => $useTmpIndex,
            ]);

            for ($i = 1; $i <= $pages; $i++) {
                $data = [
                    'store_id' => $storeId,
                    'product_ids' => $productIds,
                    'page' => $i,
                    'page_size' => $productsPerPage,
                    'useTmpIndex' => $useTmpIndex,
                ];

                $this->queue->addToQueue($this->fullAction, 'rebuildProductIndex', $data, $productsPerPage);
{'fraction_non_alphanumeric': 0.12041284403669725, 'fraction_numerical': 0.0005733944954128441, 'mean_word_length': 2.2360853432282, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 93, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2233371', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1841, 'n_words': 329}