<commit_before><?php

class LizardsAndPumpkins_MagentoConnector_Model_Export_CategoryCollector
{
    /**
     * @var Mage_Catalog_Model_Resource_Category_Collection
     */
    private $collection;

    /**
     * @var ArrayIterator
     */
    private $categoryIterator;

    /**
     * @var Mage_Core_Model_Store[]
     */
    private $storesToExportTemplate;

    /**
     * @var Mage_Core_Model_Store[]
     */
    private $storesToExportInCurrentLoop;

    /**
     * @var Mage_Core_Model_Store
     */
    private $store;

    /**
     * @var int[]
     */
    private $queuedCategoryIds;

    /**
     * @var Zend_Queue_Message_Iterator
     */
    private $messageIterator;

    /**
     * @return Mage_Catalog_Model_Category
     */
    public function getCategory()
    {
        if ($this->existsNextCategory()) {
            return $this->categoryIterator->current();
        }

        $this->prepareNextBunchOfCategories();

        $this->setNextStoreToExport();
        if (!$this->isCategoryLeftForExport()) {
            return null;
        }

        $this->createCollectionWithIdFilter();

        $this->categoryIterator = $this->collection->getIterator();
        if ($this->categoryIterator->current() === null) {
            return $this->getCategory();
        }
        return $this->categoryIterator->current();
    }

    /**
     * @return bool
     */
    private function existsNextCategory()
    {
        if ($this->categoryIterator) {
            $this->categoryIterator->next();
            return $this->categoryIterator->valid();
        }
        return false;
    }

    private function prepareNextBunchOfCategories()
    {
        if (empty($this->storesToExportInCurrentLoop)) {
            $this->storesToExportInCurrentLoop = $this->getStoresToExport();
            $this->queuedCategoryIds = $this->getQueuedCategoryIds();
        }
    }

    /**
     * @return Mage_Core_Model_Store[]
     */
    private function getStoresToExport()
    {
        if (!$this->storesToExportTemplate) {
            return Mage::app()->getStores();
        }
        return $this->storesToExportTemplate;
    }

    /**
     * @param Mage_Core_Model_Store[] $stores
     */
    public function setStoresToExport(array $stores)
    {
        $this->storesToExportTemplate = $stores;
    }

    /**
     * @param Mage_Core_Model_Store $store
     * @return Mage_Catalog_Model_Resource_Category_Collection
     */
    private function createCollection(Mage_Core_Model_Store $store)
    {
        /** @var $collection Mage_Catalog_Model_Resource_Category_Collection */
        $collection = Mage::getResourceModel('catalog/category_collection');
        $collection->setStore($store);
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('is_active', 1);
        return $collection;
    }

    /**
     * @return int[]<commit_msg>Issue #139: Root categories must not be exported<commit_after><?php

class LizardsAndPumpkins_MagentoConnector_Model_Export_CategoryCollector
{
    /**
     * @var Mage_Catalog_Model_Resource_Category_Collection
     */
    private $collection;

    /**
     * @var ArrayIterator
     */
    private $categoryIterator;

    /**
     * @var Mage_Core_Model_Store[]
     */
    private $storesToExportTemplate;

    /**
     * @var Mage_Core_Model_Store[]
     */
    private $storesToExportInCurrentLoop;

    /**
     * @var Mage_Core_Model_Store
     */
    private $store;

    /**
     * @var int[]
     */
    private $queuedCategoryIds;

    /**
     * @var Zend_Queue_Message_Iterator
     */
    private $messageIterator;

    /**
     * @return Mage_Catalog_Model_Category
     */
    public function getCategory()
    {
        if ($this->existsNextCategory()) {
            return $this->categoryIterator->current();
        }

        $this->prepareNextBunchOfCategories();

        $this->setNextStoreToExport();
        if (!$this->isCategoryLeftForExport()) {
            return null;
        }

        $this->createCollectionWithIdFilter();

        $this->categoryIterator = $this->collection->getIterator();
        if ($this->categoryIterator->current() === null) {
            return $this->getCategory();
        }
        return $this->categoryIterator->current();
    }

    /**
     * @return bool
     */
    private function existsNextCategory()
    {
        if ($this->categoryIterator) {
            $this->categoryIterator->next();
            return $this->categoryIterator->valid();
        }
        return false;
    }

    private function prepareNextBunchOfCategories()
    {
        if (empty($this->storesToExportInCurrentLoop)) {
            $this->storesToExportInCurrentLoop = $this->getStoresToExport();
            $this->queuedCategoryIds = $this->getQueuedCategoryIds();
        }
    }

    /**
     * @return Mage_Core_Model_Store[]
     */
    private function getStoresToExport()
    {
        if (!$this->storesToExportTemplate) {
            return Mage::app()->getStores();
        }
        return $this->storesToExportTemplate;
    }

    /**
     * @param Mage_Core_Model_Store[] $stores
     */
    public function setStoresToExport(array $stores)
    {
        $this->storesToExportTemplate = $stores;
    }

    /**
     * @param Mage_Core_Model_Store $store
     * @return Mage_Catalog_Model_Resource_Category_Collection
     */
    private function createCollection(Mage_Core_Model_Store $store)
    {
        /** @var $collection Mage_Catalog_Model_Resource_Category_Collection */
        $collection = Mage::getResourceModel('catalog/category_collection');
        $collection->setStore($store);
        $collection->addAttributeToSelect('*');
        $collection->addAttributeToFilter('is_active', 1);
        $collection->addAttributeToFilter('level', ['gt' => 1]);
        return $collection;
    }

    /**
     * @return int[]
{'fraction_non_alphanumeric': 0.1255521576622494, 'fraction_numerical': 0.0010193679918450561, 'mean_word_length': 2.4048582995951415, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '146111', 'n_tokens_mistral': 1709, 'n_tokens_neox': 1647, 'n_words': 247}