Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Cnc/Store/Setup/Patch/Data/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Cnc/Store/Setup/Patch/Data/CreateWebsites.php
<?php

declare(strict_types=1);

namespace Cnc\Store\Setup\Patch\Data;

use Cnc\Store\Model\Config;
use Exception;
use Magento\Framework\App\Config\ConfigResource\ConfigInterface;
use Magento\Framework\App\State;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Store\Model\GroupFactory;
use Magento\Store\Model\ResourceModel\Group;
use Magento\Store\Model\ResourceModel\Store;
use Magento\Store\Model\ResourceModel\Website;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\WebsiteFactory;

class CreateWebsites implements DataPatchInterface
{
    /** @var WebsiteFactory */
    private $websiteFactory;
    /** @var Website */
    private $websiteResourceModel;
    /** @var GroupFactory */
    private $groupFactory;
    /** @var Group */
    private $groupResourceModel;
    /** @var StoreFactory */
    private $storeFactory;
    /** @var Store */
    private $storeResourceModel;
    /** @var ConfigInterface */
    private $configResource;
    /** @var State */
    private $state;
    /** @var ModuleDataSetupInterface */
    private $setup;
    /** @var StoreManagerInterface */
    private $storeManager;

    public function __construct(
        WebsiteFactory $websiteFactory,
        Website $websiteResourceModel,
        GroupFactory $groupFactory,
        Group $groupResourceModel,
        StoreFactory $storeFactory,
        Store $storeResourceModel,
        ConfigInterface $configResource,
        State $state,
        StoreManagerInterface $storeManager,
        ModuleDataSetupInterface $setup
    ) {
        $this->websiteFactory = $websiteFactory;
        $this->websiteResourceModel = $websiteResourceModel;
        $this->groupFactory = $groupFactory;
        $this->groupResourceModel = $groupResourceModel;
        $this->storeFactory = $storeFactory;
        $this->storeResourceModel = $storeResourceModel;
        $this->configResource = $configResource;
        $this->state = $state;
        $this->storeManager = $storeManager;
        $this->setup = $setup;
    }

    public static function getDependencies(): array
    {
        return [];
    }

    public function apply(): void
    {
        $websites = $this->getWebSites();
        $installer = $this->setup;
        $installer->startSetup();

        try {
            $this->state->setAreaCode('adminhtml');
        } catch (Exception $e) {
            $e->getCode();
        }

        foreach ($websites as $code => $website) {
            $this->createWebSite($website);
        }

        $websites = [];
        foreach ($this->storeManager->getWebsites() as $website) {
            $websiteCode = $website->getCode();
            $websiteId = (int)$website->getId();
            $this->setWebSiteConfig($websiteCode, $websiteId);
            $websites[$website->getId()] = $website->getCode();
        }

        foreach ($this->storeManager->getStores() as $store) {
            $storeCode = $store->getCode();
            $storeId = (int)$store->getId();
            $websiteCode = $websites[$store->getWebsiteId()];
            $this->setStoreConfig($websiteCode, $storeCode, $storeId);
        }

        $installer->endSetup();
    }

    private function getWebsites(): array
    {
        return [
            Config::STORE_GROUP_CODE_EU => [
                'code' => Config::STORE_GROUP_CODE_EU,
                'country' => 'FR',
                'currency' => 'EUR',
                'weight' => 'kgs',
                'name' => 'CNC EU',
                'group' => [
                    'code' => Config::STORE_GROUP_CODE_EU,
                    'name' => 'CNC Store Group EU',
                ],
                'stores' => [
                    Config::STORE_CODE_FR_EU => [
                        'code' => Config::STORE_CODE_FR_EU,
                        'name' => 'CNC fr_EU',
                        'country' => 'FR',
                        'currency' => 'EUR',
                        'weight' => 'kgs',
                        'default' => true,
                    ],
                    Config::STORE_CODE_EN_EU => [
                        'code' => Config::STORE_CODE_EN_EU,
                        'name' => 'CNC en_EU',
                        'country' => 'FR',
                        'currency' => 'EUR',
                        'weight' => 'kgs',
                        'default' => false,
                    ],
                ],
            ],
            Config::WEBSITE_CODE_GB => [
                'code' => Config::WEBSITE_CODE_GB,
                'country' => 'GB',
                'currency' => 'GBP',
                'weight' => 'kgs',
                'name' => 'CNC GB',
                'group' => [
                    'code' => Config::STORE_GROUP_CODE_GB,
                    'name' => 'CNC Store Group GB',
                ],
                'stores' => [
                    Config::STORE_CODE_EN_GB => [
                        'code' => Config::STORE_CODE_EN_GB,
                        'name' => 'CNC en_GB',
                        'country' => 'GB',
                        'currency' => 'GBP',
                        'weight' => 'kgs',
                        'default' => true,
                    ],
                ],
            ],
            Config::WEBSITE_CODE_US => [
                'code' => Config::WEBSITE_CODE_US,
                'country' => 'US',
                'currency' => 'USD',
                'weight' => 'kgs',
                'name' => 'CNC US',
                'group' => [
                    'code' => Config::STORE_GROUP_CODE_US,
                    'name' => 'CNC Store Group US',
                ],
                'stores' => [
                    Config::STORE_CODE_EN_US => [
                        'code' => Config::STORE_CODE_EN_US,
                        'name' => 'CNC en_US',
                        'country' => 'US',
                        'currency' => 'USD',
                        'weight' => 'kgs',
                        'default' => true,
                    ],
                ],
            ],
        ];
    }

    private function createWebSite(array $websiteValues): void
    {
        $website = $this->websiteFactory->create();

        if (Config::DEFAULT_WEBSITE === $websiteValues['code']) {
            $this->websiteResourceModel->load($website, 'base', 'code');
            if (!$website->getId()) {
                $this->websiteResourceModel->load($website, Config::DEFAULT_WEBSITE, 'code');
            }
        } else {
            $this->websiteResourceModel->load($website, $websiteValues['code'], 'code');
        }

        $website->setCode($websiteValues['code']);
        $website->setName($websiteValues['name']);
        $this->websiteResourceModel->save($website);

        $group = $this->groupFactory->create();
        $this->groupResourceModel->load($group, $websiteValues['group']['code'], 'code');
        $group->setWebsite($website);

        if (Config::DEFAULT_WEBSITE === $websiteValues['code']) {
            $group = $website->getDefaultGroup();
            $rootCategoryId = $group->getRootCategoryId() ?: 0;
        } else {
            $groupDefault = $this->groupFactory->create();
            $this->groupResourceModel->load($groupDefault, 'cnc_eu_default', 'code');
            $rootCategoryId = $groupDefault->getRootCategoryId() ?: 0;
        }

        $group->setCode($websiteValues['group']['code']);
        $group->setName($websiteValues['group']['name']);
        $group->setRootCategoryId($rootCategoryId);
        $this->groupResourceModel->save($group);

        foreach ($websiteValues['stores'] as $storeValues) {
            $store = $this->storeFactory->create();

            if (Config::DEFAULT_STORE === $storeValues['code']) {
                $this->storeResourceModel->load($store, 'default', 'code');
                if (!$store->getId()) {
                    $this->storeResourceModel->load($store, Config::DEFAULT_STORE, 'code');
                }
            } else {
                $this->storeResourceModel->load($store, $storeValues['code'], 'code');
            }

            $store->setName($storeValues['name']);
            $store->setCode($storeValues['code']);
            $store->setWebsite($website);
            $store->setIsActive(1);
            $store->setGroup($group);
            $this->storeResourceModel->save($store);

            if ($storeValues['default']) {
                $group->setDefaultStoreId($store->getId());
                $this->groupResourceModel->save($group);
            }
        }
    }

    private function setWebSiteConfig(string $code, int $websiteId): void
    {
        $websites = $this->getWebSites();

        $this->configResource->saveConfig(
            'general/country/default',
            $websites[$code]['country'],
            ScopeInterface::SCOPE_WEBSITES,
            $websiteId
        );
        $this->configResource->saveConfig(
            'currency/options/default',
            $websites[$code]['currency'],
            ScopeInterface::SCOPE_WEBSITES,
            $websiteId
        );
        $this->configResource->saveConfig(
            'general/locale/weight_unit',
            $websites[$code]['weight'],
            ScopeInterface::SCOPE_WEBSITES,
            $websiteId
        );
    }

    private function setStoreConfig(string $websiteCode, string $storeCode, int $storeId): void
    {
        $websites = $this->getWebSites();

        $this->configResource->saveConfig(
            'general/country/default',
            $websites[$websiteCode]['stores'][$storeCode]['country'],
            ScopeInterface::SCOPE_STORES,
            $storeId
        );

        $this->configResource->saveConfig(
            'currency/options/default',
            $websites[$websiteCode]['stores'][$storeCode]['currency'],
            ScopeInterface::SCOPE_STORES,
            $storeId
        );

        $this->configResource->saveConfig(
            'general/locale/weight_unit',
            $websites[$websiteCode]['stores'][$storeCode]['weight'],
            ScopeInterface::SCOPE_STORES,
            $storeId
        );
    }

    public function getAliases(): array
    {
        return [];
    }
}

Spamworldpro Mini