HEX
Server: Apache
System: Linux srv13.cpanelhost.cl 3.10.0-962.3.2.lve1.5.38.el7.x86_64 #1 SMP Thu Jun 18 05:28:41 EDT 2020 x86_64
User: cca63905 (4205)
PHP: 7.3.20
Disabled: NONE
Upload Files
File: //home/cca63905/www/nueva/modules/wnetsecurity/src/Controller/Admin/ConfigurationController.php
<?php
/**
 * Copyright since 2014 Waynet Sp. z o.o.
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.md.
 * It is also available through the world-wide-web at this URL:
 * https://opensource.org/licenses/OSL-3.0
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to kontakt@waynet.pl so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
 * versions in the future. If you wish to customize PrestaShop for your
 * needs please refer to https://devdocs.prestashop-project.org/ for more information.
 *
 * @author    Waynet Sp. z o.o. <kontakt@waynet.pl>
 * @copyright since 2014 Waynet Sp. z o.o.
 * @license   https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
 */

declare(strict_types=1);

namespace Waynet\Security\Controller\Admin;

use PrestaShop\Module\PsAccounts\Service\PsAccountsService;
use PrestaShop\Module\PsEventbus\Service\PresenterService;
use PrestaShop\PrestaShop\Core\Addon\Module\ModuleManagerBuilder;
use PrestaShop\PsAccountsInstaller\Installer\Exception\InstallerException;
use PrestaShop\PsAccountsInstaller\Installer\Exception\ModuleNotInstalledException;
use PrestaShop\PsAccountsInstaller\Installer\Facade\PsAccounts;
use PrestaShop\PsAccountsInstaller\Installer\Installer;
use PrestaShopCorp\Billing\Presenter\BillingPresenter;
use Waynet\Security\Presenter\ConfigurationPresenter;

class ConfigurationController
{
    private const TRANSLATION_SOURCE = 'configurationcontroller';
    public const CONTACT_EMAIL = 'kontakt@waynet.pl';
    private const TOS_AND_PRIVACY_POLICY_URL = 'https://www.waynet.io/waynet-security/';

    private $module;
    private $context;
    private $accountsFacade;
    private $accountsInstaller;
    private $billingPresenter;
    private $moduleManager;
    private $configurationPresenter;

    /** @param \PrestaShop\PrestaShop\Core\Module\ModuleManagerInterface|null $moduleManager */
    public function __construct(
        \Module $module,
        \Context $context,
        PsAccounts $accountsFacade,
        Installer $accountsInstaller,
        BillingPresenter $billingPresenter,
        ConfigurationPresenter $configurationPresenter,
        $moduleManager = null
    ) {
        $this->module = $module;
        $this->context = $context;
        $this->accountsFacade = $accountsFacade;
        $this->accountsInstaller = $accountsInstaller;
        $this->billingPresenter = $billingPresenter;
        $this->configurationPresenter = $configurationPresenter;
        $this->moduleManager = $moduleManager ?? ModuleManagerBuilder::getInstance()->build();
    }

    public function indexAction(): string
    {
        try {
            $this->presentPsAccounts();
        } catch (\Exception $exception) {
            $this->context->controller->errors[] = $exception->getMessage();

            return '';
        }

        $this->presentPsBilling();
        $this->presentPsCloudSync();

        $this->context->controller->addJS($this->module->getPathUri() . 'views/js/admin/config.js');
        $this->context->controller->addCSS($this->module->getPathUri() . 'views/css/admin/config.css');

        \Media::addJsDef([
            'wnetSecurityConfig' => $this->configurationPresenter->present(),
        ]);

        $this->context->smarty->assign([
            'contact_email' => self::CONTACT_EMAIL,
            'shop_url' => \Tools::getHttpHost(true),
        ]);

        return $this->module->display($this->module->name, 'views/templates/admin/config.tpl');
    }

    private function presentPsAccounts(): void
    {
        try {
            /** @var PsAccountsService $accountsService */
            $accountsService = $this->accountsFacade->getPsAccountsService();
        } catch (ModuleNotInstalledException $exception) {
            if (!$this->accountsInstaller->install()) {
                throw $exception;
            }

            $accountsService = $this->accountsFacade->getPsAccountsService();
        } catch (InstallerException $exception) {
            $accounts = \Module::getInstanceByName($this->accountsInstaller->getModuleName());
            assert($accounts instanceof \Ps_accounts);

            $accountsService = $accounts->getService(PsAccountsService::class);
        }

        $presenter = $this->accountsFacade->getPsAccountsPresenter();

        \Media::addJsDef([
            'contextPsAccounts' => $presenter->present(),
        ]);

        $this->context->smarty->assign('psAccountsCdnUrl', $accountsService->getAccountsCdn());
    }

    private function presentPsBilling(): void
    {
        \Media::addJsDef($this->billingPresenter->present([
            'logo' => $this->module->getLocalPath() . 'logo.png',
            'tosUrl' => self::TOS_AND_PRIVACY_POLICY_URL,
            'tosLink' => self::TOS_AND_PRIVACY_POLICY_URL,
            'privacyUrl' => self::TOS_AND_PRIVACY_POLICY_URL,
            'privacyLink' => self::TOS_AND_PRIVACY_POLICY_URL,
            'emailSupport' => self::CONTACT_EMAIL,
        ]));
    }

    private function presentPsCloudSync(): void
    {
        if (!$this->moduleManager->isInstalled('ps_eventbus')) {
            $this->context->controller->errors[] = $this->module->l('Please install the "ps_eventbus" module', self::TRANSLATION_SOURCE);

            return;
        }

        $eventbus = \Module::getInstanceByName('ps_eventbus');
        assert($eventbus instanceof \Ps_eventbus);

        if (\Tools::version_compare($eventbus->version, '1.9.0')) {
            $this->context->controller->errors[] = $this->module->l('You are using an outdated version of the "ps_eventbus" module, please upgrade to version 1.9.0 or later', self::TRANSLATION_SOURCE);

            return;
        }

        /** @var PresenterService $presenter */
        $presenter = $eventbus->getService(PresenterService::class);
        \Media::addJsDef([
            'contextPsEventbus' => $presenter->expose($this->module, [
                'info',
                'modules',
                'themes',
            ]),
        ]);

        $this->context->smarty->assign('psCloudSyncCdcUrl', 'https://assets.prestashop3.com/ext/cloudsync-merchant-sync-consent/latest/cloudsync-cdc.js');
    }
}