<commit_before><?php

use Brera\MagentoConnector\XmlBuilder\ProductMerge;

class Brera_MagentoConnector_Model_Export_Exporter
{

    const PAGE_SIZE = 100;
    /**
     * @var Mage_Core_Model_Session
     */
    private $coreSession;

    /**
     * @var Brera_MagentoConnector_Model_Export_ProductCollector
     */
    private $productCollector;

    public function __construct()
    {
        $this->productCollector = Mage::getModel('brera_magentoconnector/export_productCollector');
        $this->coreSession = Mage::getSingleton('core/session');
    }

    public function exportAllProducts()
    {
        $collectorCollection = array($this->productCollector, 'getAllProductsCollection');
        $this->exportProductsWith($collectorCollection);
    }

    public function exportProductsInQueue()
    {
        $collectorCollection = array($this->productCollector, 'getAllQueuedProductUpdates');
        $this->exportProductsWith($collectorCollection);
        $updates = $this->productCollector->getQueuedProductUpdates();
        $this->cleanupQueue(
            $updates['ids'],
            $updates['skus'],
            Brera_MagentoConnector_Model_Product_Queue_Item::ACTION_STOCK_UPDATE
        );
    }

    /**
     * @param int[] $ids
     * @param string[] $skus
     * @param string $action
     */
    private function cleanupQueue(array $ids, array $skus, $action)
    {
        Mage::getResourceModel('brera_magentoconnector/product_queue_item')
            ->cleanupQueue($ids, $skus, $action);
    }

    /**
     * @param callable $collectorCollection
     */
    private function createAndUploadCatalogXml(callable $collectorCollection)
    {
        $xmlMerge = new ProductMerge();
        /** @var Brera_MagentoConnector_Model_XmlUploader $uploader */
        $uploader = Mage::getModel('brera_magentoconnector/productXmlUploader');

        foreach (Mage::app()->getStores() as $store) {
            $this->exportProductsFromStore($collectorCollection, $store, $xmlMerge, $uploader);
        }

        $uploader->writePartialString($xmlMerge->finish());
    }

    private function triggerCatalogUpdateApi()
    {
        $apiUrl = Mage::getStoreConfig('brera/magentoconnector/api_url');
        $remoteFileLocation = Mage::getStoreConfig('brera/magentoconnector/remote_catalog_xml_location');
        $api = new \Brera\MagentoConnector\Api\Api($apiUrl);
        $api->triggerProductImport($remoteFileLocation);
    }

    /**
     * @param callable $collectorCollection
     */
    private function exportProductsWith(callable $collectorCollection)
    {
        $this->createAndUploadCatalogXml($collectorCollection);

        try {
            $this->triggerCatalogUpdateApi();
            $this->coreSession->addSuccess('Export was successfull.');
        } catch (Exception $e) {
            $this->coreSession->addError('Export failed: ' . $e->getMessage());
        }
    }

    /**
     * @param callable $collectorCollection
     * @param Mage_Core_Model_Store $store
     * @param ProductMerge $xmlMerge
     * @param Brera_MagentoConnector_Model_XmlUploader $uploader
     */
    private function exportProductsFromStore(callable $collectorCollection, $store, $xmlMerge, $uploader)
    {
        /** @var Mage_Catalog_Model_Resource_Product_Collection $productCollection */
        $productCollection = $collectorCollection($store);
        $productCollection->setPageSize(self::PAGE_SIZE);
        $pages = $productCollection->getLastPageNumber();
        $currentPage = 1;

        do {
            $productCollection->setCurPage($currentPage);
            $this->productCollector->addAdditionalData($productCollection, $store);

            $xmlBuilderAndUploader = new Brera_MagentoConnector_Model_Export_ProductXmlBuilderAndUploader(
                $productCollection, $store, $xmlMerge, $uploader
            );

            $xmlBuilderAndUploader->process();

            $currentPage++;
            //clear collection and free memory
            $productCollection->clear();
        } while ($currentPage <= $pages);
    }
}
<commit_msg>Issue #41: Fixes #41<commit_after><?php

use Brera\MagentoConnector\XmlBuilder\ProductMerge;

class Brera_MagentoConnector_Model_Export_Exporter
{

    const PAGE_SIZE = 100;
    /**
     * @var Mage_Core_Model_Session
     */
    private $coreSession;

    /**
     * @var Brera_MagentoConnector_Model_Export_ProductCollector
     */
    private $productCollector;

    public function __construct()
    {
        $this->productCollector = Mage::getModel('brera_magentoconnector/export_productCollector');
        $this->coreSession = Mage::getSingleton('core/session');
    }

    public function exportAllProducts()
    {
        Mage::app()->setCurrentStore(Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID));
        $collectorCollection = array($this->productCollector, 'getAllProductsCollection');
        $this->exportProductsWith($collectorCollection);
    }

    public function exportProductsInQueue()
    {
        Mage::app()->setCurrentStore(Mage::app()->getStore(Mage_Core_Model_App::ADMIN_STORE_ID));
        $collectorCollection = array($this->productCollector, 'getAllQueuedProductUpdates');
        $this->exportProductsWith($collectorCollection);
        $updates = $this->productCollector->getQueuedProductUpdates();
        $this->cleanupQueue(
            $updates['ids'],
            $updates['skus'],
            Brera_MagentoConnector_Model_Product_Queue_Item::ACTION_STOCK_UPDATE
        );
    }

    /**
     * @param int[]    $ids
     * @param string[] $skus
     * @param string   $action
     */
    private function cleanupQueue(array $ids, array $skus, $action)
    {
        Mage::getResourceModel('brera_magentoconnector/product_queue_item')
            ->cleanupQueue($ids, $skus, $action);
    }

    /**
     * @param callable $collectorCollection
     */
    private function createAndUploadCatalogXml(callable $collectorCollection)
    {
        $xmlMerge = new ProductMerge();
        /** @var Brera_MagentoConnector_Model_XmlUploader $uploader */
        $uploader = Mage::getModel('brera_magentoconnector/productXmlUploader');

        foreach (Mage::app()->getStores() as $store) {
            $this->exportProductsFromStore($collectorCollection, $store, $xmlMerge, $uploader);
        }

        $uploader->writePartialString($xmlMerge->finish());
    }

    private function triggerCatalogUpdateApi()
    {
        $apiUrl = Mage::getStoreConfig('brera/magentoconnector/api_url');
        $remoteFileLocation = Mage::getStoreConfig('brera/magentoconnector/remote_catalog_xml_location');
        $api = new \Brera\MagentoConnector\Api\Api($apiUrl);
        $api->triggerProductImport($remoteFileLocation);
    }

    /**
     * @param callable $collectorCollection
     */
    private function exportProductsWith(callable $collectorCollection)
    {
        $this->createAndUploadCatalogXml($collectorCollection);

        try {
            $this->triggerCatalogUpdateApi();
            $this->coreSession->addSuccess('Export was successfull.');
        } catch (Exception $e) {
            $this->coreSession->addError('Export failed: ' . $e->getMessage());
        }
    }

    /**
     * @param callable                                 $collectorCollection
     * @param Mage_Core_Model_Store                    $store
     * @param ProductMerge                             $xmlMerge
     * @param Brera_MagentoConnector_Model_XmlUploader $uploader
     */
    private function exportProductsFromStore(callable $collectorCollection, $store, $xmlMerge, $uploader)
    {
        /** @var Mage_Catalog_Model_Resource_Product_Collection $productCollection */
        $productCollection = $collectorCollection($store);
        $productCollection->setPageSize(self::PAGE_SIZE);
        $pages = $productCollection->getLastPageNumber();
        $currentPage = 1;

        do {
            $productCollection->setCurPage($currentPage);
            $this->productCollector->addAdditionalData($productCollection, $store);

            $xmlBuilderAndUploader = new Brera_MagentoConnector_Model_Export_ProductXmlBuilderAndUploader(
                $productCollection, $store, $xmlMerge, $uploader
            );

            $xmlBuilderAndUploader->process();

            $currentPage++;
            //clear collection and free memory
            $productCollection->clear();
        } while ($currentPage <= $pages);
    }
}

{'fraction_non_alphanumeric': 0.11618012055312611, 'fraction_numerical': 0.0014182720718591182, 'mean_word_length': 3.0218631178707223, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3960250', 'n_tokens_mistral': 2387, 'n_tokens_neox': 2356, 'n_words': 341}