<?php
/**
 * @author Aitoc Team
 * @copyright Copyright (c) 2020 Aitoc (https://www.aitoc.com)
 * @package Aitoc_PreOrders
 */

/**
 * Copyright © 2018 Aitoc. All rights reserved.
 * Developed by Alex Yakimovich
 */

namespace Aitoc\PreOrders\Setup;

use Magento\Framework\Setup\SchemaSetupInterface;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Aitoc\PreOrders\Setup\Operations\CreateOrderItemPreOrderSettingsTable;
use Aitoc\PreOrders\Setup\Operations\CreatePreOrderSubscribersTable;
use Aitoc\PreOrders\Setup\Operations\CreateProductAttributesTable;

/**
 * Upgrade PreOrders DB scheme
 */
class UpgradeSchema extends AbstractUpgrade implements UpgradeSchemaInterface
{
    /**
     * @var CreateOrderItemPreOrderSettingsTable
     */
    private $createOrderItemPreOrderSettingsTableOperation;

    /**
     * @var CreatePreOrderSubscribersTable
     */
    private $createPreOrderSubscribersTableOperation;

    /**
     * @var CreateProductAttributesTable
     */
    private $createProductAttributesTableOperation;

    /**
     * @param CreateOrderItemPreOrderSettingsTable $createOrderItemPreOrderSettingsTableOperation
     * @param CreatePreOrderSubscribersTable $createPreOrderSubscribersTableOperation
     * @param CreateProductAttributesTable $createProductAttributesTableOperation
     */
    public function __construct(
        CreateOrderItemPreOrderSettingsTable $createOrderItemPreOrderSettingsTableOperation,
        CreatePreOrderSubscribersTable $createPreOrderSubscribersTableOperation,
        CreateProductAttributesTable $createProductAttributesTableOperation
    ) {
        $this->createOrderItemPreOrderSettingsTableOperation = $createOrderItemPreOrderSettingsTableOperation;
        $this->createPreOrderSubscribersTableOperation = $createPreOrderSubscribersTableOperation;
        $this->createProductAttributesTableOperation = $createProductAttributesTableOperation;
    }

    /**
     * @param SchemaSetupInterface $setup
     * @param ModuleContextInterface $context
     * @throws \Zend_Db_Exception
     */
    public function upgrade(SchemaSetupInterface $setup, ModuleContextInterface $context)
    {
        $this->context = $context;
        $setup->startSetup();

        if ($this->checkVersion('1.2.2')) {
            $this->createOrderItemPreOrderSettingsTableOperation->execute($setup);
        }
        if ($this->checkVersion('1.2.3')) {
            $this->createPreOrderSubscribersTableOperation->execute($setup);
        }
        if ($this->checkVersion('1.2.4')) {
            $this->createProductAttributesTableOperation->execute($setup);
        }

        $setup->endSetup();
    }
}
