jose-c
9/5/2018 - 1:13 PM

Modify category data setup script

<?php

namespace Macri\SetupTheme\Setup;

use Macri\SetupTheme\Model\ConstantsSetup;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Store\Model\GroupFactory;
use Magento\Store\Model\ResourceModel\Group;
use Magento\Framework\Event\ManagerInterface;
use Magento\Catalog\Model\CategoryFactory;
use Magento\Catalog\Model\CategoryRepository;
use Magento\Store\Model\Store;
class UpgradeData implements \Magento\Framework\Setup\UpgradeDataInterface
{

    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;
    /**
     * @var \Magento\Store\Model\WebsiteFactory
     */
    private $websiteFactory;
    /**
     * @var \Magento\Store\Model\WebsiteRepository
     */
    private $websiteRepository;
    /**
     * @var \Magento\Store\Model\StoreFactory
     */
    private $storeFactory;
    /**
     * @var \Magento\Store\Model\ResourceModel\Website
     */
    private $websiteResourceModel;
    /**
     * @var \Magento\Store\Model\ResourceModel\Store
     */
    private $storeResourceModel;
    /**
     * @var \Magento\Store\Model\StoreRepository
     */
    private $storeRepository;
    /**
     * @var \Magento\Config\Model\ResourceModel\Config
     */
    private $resourceConfig;
    /**
     * @var \Macri\SetupTheme\Model\Page
     */
    private $pageInstall;
    /**
     * @var \Macri\SetupTheme\Model\Block
     */
    private $blockInstall;
    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;
    /**
     * @var \Magento\Theme\Model\ThemeFactory
     */
    private $themeFactory;
    /**
     * @var \Magento\Theme\Model\ResourceModel\Theme
     */
    private $themeResourceModel;
    /**
     * @var \Magento\Framework\App\State
     */
    private $state;
    /**
     * @var \Macri\SetupTheme\Model\Website
     */
    private $websiteInstall;
    /**
     * @var \Macri\SetupTheme\Model\Store
     */
    private $storeGroupInstall;
    /**
     * @var \Macri\SetupTheme\Model\Storeview
     */
    private $storeviewInstall;
    /**
     * @var \Magento\Indexer\Model\IndexerFactory
     */
    private $indexerFactory;
    /**
     * @var \Magento\Indexer\Model\Indexer\CollectionFactory
     */
    private $indexerCollectionFactory;
    /**
     * @var \Macri\SetupTheme\Model\DesignConfig
     */
    private $designConfigInstall;
    /**
     * @var \Magento\Framework\Registry
     */
    private $registry;
    /**
     * @var GroupFactory
     */
    private $groupFactory;
    /**
     * @var Group
     */
    private $groupResourceModel;
    /**
     * @var ManagerInterface
     */
    private $eventManager;
    /**
     * @var Magento\Cms\Model\BlockRepository
     */
    private $staticBlockRepository;
    /**
     * @var CategoryFactory
     */
    private $categoryFactory;
    /**
     * @var CategoryRepository
     */
    private $categoryRepository;
    /**
     * @var \Macri\SetupTheme\Model\ConstantsSetup
     */
    private $constantsSetup;
    /**
     * @var \Magento\Store\Model\StoreManager
     */
    private $storeManager;

    /**
     * UpgradeData constructor.
     * @param EavSetupFactory $eavSetupFactory
     * @param \Magento\Store\Model\WebsiteFactory $websiteFactory
     * @param \Magento\Store\Model\WebsiteRepository $websiteRepository
     * @param \Magento\Store\Model\StoreFactory $storeFactory
     * @param \Magento\Store\Model\ResourceModel\Website $websiteResourceModel
     * @param \Magento\Store\Model\ResourceModel\Store $storeResourceModel
     * @param \Magento\Store\Model\StoreRepository $storeRepository
     * @param \Magento\Config\Model\ResourceModel\Config $resourceConfig
     * @param \Macri\SetupTheme\Model\Page $pageInstall
     * @param \Macri\SetupTheme\Model\Block $blockInstall
     * @param \Psr\Log\LoggerInterface $logger
     * @param \Magento\Theme\Model\ThemeFactory $themeFactory
     * @param \Magento\Theme\Model\ResourceModel\Theme $themeResourceModel
     * @param \Magento\Framework\App\State $state
     * @param \Macri\SetupTheme\Model\Website $websiteInstall
     * @param \Macri\SetupTheme\Model\Store $storeGroupInstall
     * @param \Macri\SetupTheme\Model\Storeview $storeviewInstall
     * @param \Magento\Indexer\Model\IndexerFactory $indexerFactory
     * @param \Magento\Indexer\Model\Indexer\CollectionFactory $indexerCollectionFactory
     * @param \Macri\SetupTheme\Model\DesignConfig $designConfigInstall
     * @param \Magento\Framework\Registry $registry
     * @param GroupFactory $groupFactory
     * @param Group $groupResourceModel
     */
    public function __construct(
        EavSetupFactory $eavSetupFactory,
        \Magento\Store\Model\WebsiteFactory $websiteFactory,
        \Magento\Store\Model\WebsiteRepository $websiteRepository,
        \Magento\Store\Model\StoreFactory $storeFactory,
        \Magento\Store\Model\ResourceModel\Website $websiteResourceModel,
        \Magento\Store\Model\ResourceModel\Store $storeResourceModel,
        \Magento\Store\Model\StoreRepository $storeRepository,
        \Magento\Config\Model\ResourceModel\Config $resourceConfig,
        \Macri\SetupTheme\Model\Page $pageInstall,
        \Macri\SetupTheme\Model\Block $blockInstall,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Theme\Model\ThemeFactory $themeFactory,
        \Magento\Theme\Model\ResourceModel\Theme $themeResourceModel,
        \Magento\Framework\App\State $state,
        \Macri\SetupTheme\Model\Website $websiteInstall,
        \Macri\SetupTheme\Model\Store $storeGroupInstall,
        \Macri\SetupTheme\Model\Storeview $storeviewInstall,
        \Magento\Indexer\Model\IndexerFactory $indexerFactory,
        \Magento\Indexer\Model\Indexer\CollectionFactory $indexerCollectionFactory,
        \Macri\SetupTheme\Model\DesignConfig $designConfigInstall,
        \Magento\Framework\Registry $registry,
        GroupFactory $groupFactory,
        Group $groupResourceModel,
        ManagerInterface $eventManager,
        \Magento\Cms\Model\BlockRepository $staticBlockRepository,
        CategoryFactory $categoryFactory,
        CategoryRepository $categoryRepository,
        \Macri\SetupTheme\Model\ConstantsSetup $constantsSetup,
        \Magento\Store\Model\StoreManager $storeManager
    )
    {

        $this->eavSetupFactory = $eavSetupFactory;
        $this->websiteFactory = $websiteFactory;
        $this->websiteRepository = $websiteRepository;
        $this->storeFactory = $storeFactory;
        $this->websiteResourceModel = $websiteResourceModel;
        $this->storeResourceModel = $storeResourceModel;
        $this->storeRepository = $storeRepository;
        $this->resourceConfig = $resourceConfig;
        $this->pageInstall = $pageInstall;
        $this->blockInstall = $blockInstall;
        $this->logger = $logger;
        $this->themeFactory = $themeFactory;
        $this->themeResourceModel = $themeResourceModel;
        $this->state = $state;
        $this->websiteInstall = $websiteInstall;
        $this->storeGroupInstall = $storeGroupInstall;
        $this->storeviewInstall = $storeviewInstall;
        $this->indexerFactory = $indexerFactory;
        $this->indexerCollectionFactory = $indexerCollectionFactory;
        $this->designConfigInstall = $designConfigInstall;
        $this->registry = $registry;
        $this->groupFactory = $groupFactory;
        $this->groupResourceModel = $groupResourceModel;
        $this->eventManager = $eventManager;
        $this->staticBlockRepository = $staticBlockRepository;
        $this->categoryFactory = $categoryFactory;
        $this->categoryRepository = $categoryRepository;
        $this->constantsSetup = $constantsSetup;
        $this->storeManager = $storeManager;
    }

    /**
     *
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(\Magento\Framework\Setup\ModuleDataSetupInterface $setup, \Magento\Framework\Setup\ModuleContextInterface $context)
    {
        $setup->startSetup();

        
        // 1.0.4
        //  Catgory Banners Home page
        if (version_compare($context->getVersion(), '1.0.4') < 0)
        {
            try {

                try {
                    $this->state->getAreaCode();
                } catch (LocalizedException $e) {
                    $this->state->setAreaCode(\Magento\Framework\App\Area::AREA_GLOBAL);
                }

                $this->blockInstall->install(
                    [
                        'Macri_SetupTheme::fixtures/blocks/pages_static_blocks_1.0.4.csv'
                    ]
                );

                /*Zapateria*/
                $storeZapateria = $this->storeFactory->create();
                $this->storeResourceModel->load($storeZapateria, 'zapateria', 'code');
                $blockHomeZapateria = $this->staticBlockRepository->getById('category_banners_home_zapateria');
                $category = $this->categoryFactory->create()->setStoreId($storeZapateria->getId());
                $category = $category->load(ConstantsSetup::CATEGORY_HOME_ZAPATERIA_ID);
                $category->setDisplayMode('PRODUCTS_AND_PAGE');
                $category->setLandingPage($blockHomeZapateria->getId());
                $category->save();

                /*Sport Center*/
                $storeCenter = $this->storeFactory->create();
                $this->storeResourceModel->load($storeCenter, 'sport_center', 'code');
                $blockHome = $this->staticBlockRepository->getById('category_banners_home_sport_center');
                $category = $this->categoryFactory->create()->setStoreId($storeCenter->getId());
                $category = $category->load(ConstantsSetup::CATEGORY_HOME_SPORT_CENTER_ID);
                $category->setDisplayMode('PRODUCTS_AND_PAGE');
                $category->setLandingPage($blockHome->getId());
                $category->save();

                /*Sport life*/
                $storeLife = $this->storeFactory->create();
                $this->storeResourceModel->load($storeLife, 'sport_life', 'code');
                $blockHome = $this->staticBlockRepository->getById('category_banners_home_sport_life');
                $category = $this->categoryFactory->create()->setStoreId($storeLife->getId());
                $category = $category->load(ConstantsSetup::CATEGORY_HOME_SPORT_LIFE_ID);
                $category->setDisplayMode('PRODUCTS_AND_PAGE');
                $category->setLandingPage($blockHome->getId());
                $category->save();



            } catch (\Exception $e) {
                $this->logger->error($e->getMessage(), $e->getTrace());
            }
        }


        $setup->endSetup();
    }
}