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/Soon/Core/Setup/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Soon/Core/Setup/StoreInstaller.php
<?php
/**
 * @license http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author Julien TRAJMAN <[email protected]> <@AgenceSoon>
 * @copyright Copyright (c) 2018 Agence Soon (http://www.agence-soon.fr)
 */
declare(strict_types=1);

namespace Soon\Core\Setup;

use Exception;
use Magento\Catalog\Helper\DefaultCategory;
use Magento\Framework\Event\Manager;
use Magento\Framework\Exception\FileSystemException;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Filesystem\Driver\File;
use Magento\Framework\Setup\SampleData\FixtureManager;
use Magento\Store\Model\Group as CoreGroup;
use Magento\Store\Model\GroupFactory;
use Magento\Store\Model\GroupRepository;
use Magento\Store\Model\ResourceModel\Group;
use Magento\Store\Model\ResourceModel\Store;
use Magento\Store\Model\ResourceModel\Website;
use Magento\Store\Model\Store as CoreStore;
use Magento\Store\Model\StoreFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Model\Website as CoreWebsite;
use Magento\Store\Model\WebsiteFactory;
use Psr\Log\LoggerInterface;
use Symfony\Component\Yaml\Parser;

class StoreInstaller
{
    const DEFAULT_GROUP_NAME = 'Main Website Store';
    const STORE_CODE_DEFAULT = 'default';

    /**
     * @var WebsiteFactory
     */
    private $websiteFactory;
    /**
     * @var Website
     */
    private $websiteResourceModel;
    /**
     * @var StoreFactory
     */
    private $storeFactory;
    /**
     * @var GroupFactory
     */
    private $groupFactory;
    /**
     * @var Group
     */
    private $groupResourceModel;
    /**
     * @var Store
     */
    private $storeResourceModel;
    /**
     * @var StoreManagerInterface
     */
    private $storeManager;
    /**
     * @var DefaultCategory
     */
    private $defaultCategoryHelper;
    /**
     * @var LoggerInterface
     */
    private $logger;
    /**
     * @var array
     */
    private $currentWebsiteData;
    /**
     * @var CoreWebsite
     */
    private $currentWebsite;
    /**
     * @var array
     */
    private $currentGroupData;
    /**
     * @var CoreGroup
     */
    private $currentGroup;
    /**
     * @var array
     */
    private $currentStoreData;
    /**
     * @var CoreStore
     */
    private $currentStore;
    /**
     * @var array
     */
    private $eventNames;
    /**
     * @var FixtureManager
     */
    private $fixtureManager;
    /**
     * @var Parser
     */
    private $yamlParser;
    /**
     * @var File
     */
    private $fileSystem;
    /**
     * @var GroupRepository
     */
    private $groupRepository;
    /**
     * @var string
     */
    private $loadName;
    /**
     * @var Manager
     */
    private $eventManager;

    /**
     * InstallData constructor.
     * @param WebsiteFactory $websiteFactory
     * @param Website $websiteResourceModel
     * @param Store $storeResourceModel
     * @param Group $groupResourceModel
     * @param StoreFactory $storeFactory
     * @param GroupFactory $groupFactory
     * @param GroupRepository $groupRepository
     * @param StoreManagerInterface $storeManager
     * @param DefaultCategory $defaultCategoryHelper
     * @param FixtureManager $fixtureManager
     * @param Parser $yamlParser
     * @param File $fileSystem
     * @param LoggerInterface $logger
     * @param Manager $eventManager
     */
    public function __construct(
        WebsiteFactory $websiteFactory,
        Website $websiteResourceModel,
        Store $storeResourceModel,
        Group $groupResourceModel,
        StoreFactory $storeFactory,
        GroupFactory $groupFactory,
        GroupRepository $groupRepository,
        StoreManagerInterface $storeManager,
        DefaultCategory $defaultCategoryHelper,
        FixtureManager $fixtureManager,
        Parser $yamlParser,
        File $fileSystem,
        LoggerInterface $logger,
        Manager $eventManager
    ) {
        $this->websiteFactory = $websiteFactory;
        $this->websiteResourceModel = $websiteResourceModel;
        $this->storeFactory = $storeFactory;
        $this->groupFactory = $groupFactory;
        $this->groupRepository = $groupRepository;
        $this->groupResourceModel = $groupResourceModel;
        $this->storeResourceModel = $storeResourceModel;
        $this->storeManager = $storeManager;
        $this->defaultCategoryHelper = $defaultCategoryHelper;
        $this->logger = $logger;
        $this->fixtureManager = $fixtureManager;
        $this->yamlParser = $yamlParser;
        $this->fileSystem = $fileSystem;
        $this->eventManager = $eventManager;
    }

    /**
     * Installs data for a module
     *
     * @param string $yamlFile
     * @return void
     * @throws FileSystemException
     * @throws LocalizedException
     */
    public function install(string $yamlFile): void
    {
        array_map(function ($websiteData) {
            $this->currentWebsiteData = $websiteData;
            if ($this->canProcessWebsite()) {
                try {
                    $this->processWebsite();
                    $this->processGroups();
                } catch (Exception $e) {
                    $this->logger->critical($e->getMessage());
                }
            }
        }, $this->extractYamlData($yamlFile));
    }

    /**
     * @return bool
     */
    private function canProcessWebsite(): bool
    {
        if (isset($this->currentWebsiteData['code'])) {
            return $this->assertCurrentWebsite();
        }

        return false;
    }

    /**
     * @return bool
     */
    private function assertCurrentWebsite(): bool
    {
        try {
            $this->currentWebsite = $this->storeManager->getWebsite($this->getLoadCode($this->currentWebsiteData));
        } catch (NoSuchEntityException $e) {
            $this->currentWebsite = $this->websiteFactory->create();
        } catch (Exception $e) {
            $this->logger->critical($e->getMessage());

            return false;
        }

        return true;
    }

    /**
     * Get code which will be used to load current entity
     * (If 'old_code' is provided, use it, otherwise, fallback to 'code')
     *
     * @param array $data
     * @return string
     * @throws LocalizedException
     */
    private function getLoadCode(array $data): string
    {
        if (!isset($data['code'])) {
            throw new LocalizedException(__('No code defined.'));
        }

        return $data['code'];
    }

    private function processWebsite(): void
    {
        $this->enrichWebsite();
        $this->websiteResourceModel->save($this->currentWebsite);
    }

    private function enrichWebsite(): void
    {
        $this->currentWebsite->setCode($this->currentWebsiteData['code']);
        $this->currentWebsite->setName($this->currentWebsiteData['name']);
        $this->currentWebsite->setData(
            'sort_order',
            isset($this->currentWebsiteData['sort_order']) ? $this->currentWebsiteData['sort_order'] : 999
        );
        $this->currentWebsite->setData(
            'is_default',
            isset($this->currentWebsiteData['is_default']) ? $this->currentWebsiteData['is_default'] : 0
        );
    }

    private function processGroups(): void
    {
        if (($websiteId = $this->currentWebsite->getId()) && isset($this->currentWebsiteData['groups'])) {
            array_map(function ($groupData) {
                $this->currentGroupData = $groupData;
                if ($this->hasCurrentGroup()) {
                    $this->enrichGroup();
                    if ($this->hasSavedGroup()) {
                        $this->processStores();
                    }
                }
            }, $this->currentWebsiteData['groups']);
        }
    }

    /**
     * @return bool
     */
    private function hasCurrentGroup(): bool
    {
        try {
            $this->loadName = $this->currentGroupData['name'];

            array_map(function ($group) {
                if ($group->getId() == 1 && $group->getName() == self::DEFAULT_GROUP_NAME) {
                    $this->loadName = self::DEFAULT_GROUP_NAME;
                }
            }, $this->groupRepository->getList());

            $this->currentGroup = $this->getGroupByName($this->loadName);
        } catch (NoSuchEntityException $e) {
            $this->currentGroup = $this->groupFactory->create();
        } catch (Exception $e) {
            $this->logger->critical($e->getMessage());

            return false;
        }

        return true;
    }

    /**
     * Get group by name
     *
     * @param $name
     * @return CoreGroup
     * @throws NoSuchEntityException
     */
    private function getGroupByName($name): CoreGroup
    {
        $group = $this->groupFactory->create();

        $this->groupResourceModel->load($group, $name, 'name');

        if (!$group->getId()) {
            throw new NoSuchEntityException();
        }

        return $group;
    }

    private function enrichGroup(): void
    {
        $this->currentGroup->setCode($this->currentGroupData['code']);
        $this->currentGroup->setName($this->currentGroupData['name']);
        $this->currentGroup->setRootCategoryId($this->defaultCategoryHelper->getId());
        $this->currentGroup->setWebsiteId($this->currentWebsite->getId());
    }

    /**
     * @return bool
     */
    private function hasSavedGroup(): bool
    {
        try {
            $this->groupResourceModel->save($this->currentGroup);
            $this->eventManager->dispatch('store_group_save', ['group' => $this->currentGroup]);
        } catch (Exception $e) {
            $this->logger->critical($e->getMessage());

            return false;
        }

        return true;
    }

    private function processStores(): void
    {
        if (($groupId = $this->currentGroup->getId()) && isset($this->currentGroupData['store_views'])) {
            array_map(function ($storeData) {
                $this->currentStoreData = $storeData;
                if ($this->hasCurrentStore()) {
                    $this->enrichStore();
                    $this->saveStore();
                }
            }, $this->currentGroupData['store_views']);
        }
    }

    /**
     * @return bool
     */
    private function hasCurrentStore(): bool
    {
        try {
            $this->currentStore = $this->storeManager->getStore($this->getLoadCode($this->currentStoreData));
            $this->eventNames['store'] = 'store_edit';
        } catch (NoSuchEntityException $e) {
            $this->currentStore = $this->storeFactory->create();
            $this->eventNames['store'] = 'store_add';
        } catch (Exception $e) {
            $this->logger->critical($e->getMessage());

            return false;
        }

        return true;
    }

    private function enrichStore(): void
    {
        $this->currentStore->setCode($this->currentStoreData['code']);
        $this->currentStore->setName($this->currentStoreData['name']);
        $this->currentStore->setWebsite($this->currentWebsite);
        $this->currentStore->setData('group_id', $this->currentGroup->getId());
        $this->currentStore->setData(
            'sort_order',
            isset($this->currentWebsiteData['sort_order']) ? $this->currentWebsiteData['sort_order'] : 999
        );
        $this->currentStore->setData(
            'is_active',
            isset($this->currentStoreData['is_active']) ? $this->currentStoreData['is_active'] : 0
        );
    }

    private function saveStore(): void
    {
        try {
            $this->storeResourceModel->save($this->currentStore);
            $this->storeManager->reinitStores();
            $this->eventManager->dispatch($this->eventNames['store'], ['store' => $this->currentStore]);
        } catch (Exception $e) {
            $this->logger->critical($e->getMessage());
        }
    }

    /**
     * Get the data from the YAML config file
     *
     * @param string $yamlFile
     * @return array
     * @throws FileSystemException
     * @throws LocalizedException
     */
    private function extractYamlData(string $yamlFile): array
    {
        $fileName = $this->fixtureManager->getFixture($yamlFile);

        if ($this->fileSystem->isFile($fileName)) {
            return $this->yamlParser->parse($this->fileSystem->fileGetContents($fileName));
        }

        return [];
    }
}

Spamworldpro Mini