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: //proc/self/root/proc/self/cwd/nueva/modules/ps_metrics/src/Handler/NativeStatsHandler.php
<?php

/**
 * Copyright since 2007 PrestaShop SA and Contributors
 * PrestaShop is an International Registered Trademark & Property of PrestaShop SA
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License version 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/AFL-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 license@prestashop.com so we can send you a copy immediately.
 *
 * @author    PrestaShop SA and Contributors <contact@prestashop.com>
 * @copyright Since 2007 PrestaShop SA and Contributors
 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0
 */

namespace PrestaShop\Module\Ps_metrics\Handler;

use PrestaShopDatabaseException;
use PrestaShopException;

class NativeStatsHandler
{
    const METRICS_STATS_CONTROLLER = 'AdminMetricsController';
    const NATIVE_STATS_CONTROLLER = 'AdminStats';
    const NATIVE_STATS_CONTROLLER_COPY = 'AdminMetricsLegacyStatsController';

    /**
     * @var \Ps_metrics
     */
    private $module;

    /**
     * NativeStatsHandler constructor.
     *
     * @param \Ps_metrics $module
     */
    public function __construct(
        \Ps_metrics $module
    ) {
        $this->module = $module;
    }

    /**
     * Run when installing the module
     *
     * @return bool
     */
    public function install()
    {
        return $this->installMetricsControllerSideBySideWithNativeStats();
    }

    /**
     * Run when uninstalling the module
     *
     * @return bool
     */
    public function uninstall()
    {
        $legacyStatsTab = new \Tab(
            \Tab::getIdFromClassName(self::NATIVE_STATS_CONTROLLER)
        );

        if (!$legacyStatsTab->active) {
            $legacyStatsTab->active = true;
            $legacyStatsTab->save();
        }

        return $this->deleteAllStatsController();
    }

    /**
     * Install metrics controller side by side with native stats controller
     *
     * @return bool
     */
    private function installMetricsControllerSideBySideWithNativeStats()
    {
        $this->deleteAllStatsController();

        $legacyStatsTab = new \Tab(
            \Tab::getIdFromClassName(self::NATIVE_STATS_CONTROLLER)
        );
        $legacyStatsTab->active = true;

        $nativeStatsTab = new \Tab();
        $nativeStatsTab->name = $legacyStatsTab->name;
        $nativeStatsTab->class_name = self::NATIVE_STATS_CONTROLLER_COPY;
        $nativeStatsTab->active = true;
        $nativeStatsTab->module = $this->module->name;
        $nativeStatsTab->id_parent = (int) $legacyStatsTab->id;

        $metricsTab = new \Tab();
        $metricsTab->name = array_fill_keys(
            \Language::getIDs(false),
            $this->module->displayName
        );
        $metricsTab->class_name = self::METRICS_STATS_CONTROLLER;
        $metricsTab->active = true;
        $metricsTab->module = $this->module->name;
        $metricsTab->id_parent = (int) $legacyStatsTab->id;

        return $legacyStatsTab->save() &&
            $nativeStatsTab->add() &&
            $metricsTab->add();
    }

    /**
     * Delete all stats controller
     *
     * @return bool
     *
     * @throws PrestaShopException
     * @throws PrestaShopDatabaseException
     */
    private function deleteAllStatsController()
    {
        $metricsTab = new \Tab(
            \Tab::getIdFromClassName(self::METRICS_STATS_CONTROLLER)
        );
        $nativeStatsTab = new \Tab(
            \Tab::getIdFromClassName(self::NATIVE_STATS_CONTROLLER_COPY)
        );

        if ($metricsTab->id) {
            $metricsTab->delete();
        }

        if ($nativeStatsTab->id) {
            $nativeStatsTab->delete();
        }

        return true;
    }
}