<commit_before><?php

namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Manifest;

use Fastly\Cdn\Model\ManifestFactory;
use Fastly\Cdn\Model\ResourceModel\Manifest as ManifestResource;
use Fastly\Cdn\Model\Manifest;
use Fastly\Cdn\Model\Modly\Manifest as Modly;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Fastly\Cdn\Model\ResourceModel\Manifest\CollectionFactory;
use Fastly\Cdn\Model\Api;
use Fastly\Cdn\Helper\Vcl;
use Fastly\Cdn\Model\Config;

/**
 * Class Create
 *
 * @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Manifest
 */
class ToggleModules extends Action
{
    /**
     * @var ManifestFactory
     */
    private $manifestFactory;
    /**
     * @var Manifest
     */
    private $manifest;
    /**
     * @var Modly
     */
    private $modly;
    /**
     * @var ManifestResource
     */
    private $manifestResource;
    /**
     * @var JsonFactory
     */
    private $resultJson;
    /**
     * @var Http
     */
    private $request;
    /**
     * @var CollectionFactory
     */
    private $collectionFactory;
    /**
     * @var Api
     */
    private $api;

    private $vcl;

    private $enabledModules = [];

    public function __construct(
        Context $context,
        ManifestFactory $manifestFactory,
        ManifestResource $manifestResource,
        Manifest $manifest,
        Modly $modly,
        JsonFactory $resultJsonFactory,
        Http $request,
        CollectionFactory $collectionFactory,
        Api $api,
        Vcl $vcl
    ) {
        $this->manifestFactory = $manifestFactory;
        $this->manifestResource = $manifestResource;
        $this->manifest = $manifest;
        $this->modly = $modly;
        $this->resultJson = $resultJsonFactory;
        $this->request = $request;
        $this->collectionFactory = $collectionFactory;
        $this->api = $api;
        $this->vcl = $vcl;
        parent::__construct($context);
    }

    public function execute()
    {
        $result = $this->resultJson->create();
        try {
            $checkedModules = $this->getRequest()->getParam('checked_modules');
            if ($checkedModules == null) {
                $checkedModules = [];
            }
            $moduleCollection = $this->collectionFactory->create()->getData();

            foreach ($moduleCollection as $module) {
                $manifest = $this->manifestFactory->create();
                $moduleId = $module['manifest_id'];
                if (in_array($moduleId, $checkedModules)) {
                    $manifest->setManifestId($moduleId);
                    $manifest->setManifestStatus(1);
                } else {
                    if ($module['manifest_status'] == 1) {
                        $this->enabledModules[$moduleId] = $module['manifest_vcl'];
                    }
                    $manifest->setManifestId($moduleId);
                    $manifest->setManifestStatus(0);
                }
                $this->saveManifest($manifest);
            }

            if ($this->enabledModules) {
                $removeStatus = $this->removeManifests($this->enabledModules);
                if ($removeStatus) {
                    throw new \Exception(__($removeStatus));
                }
            }

            return $result->setData([
                'status' => true
            ]);
        } catch (\Exception $e) {
            return $result->setData([
                'status'    => false,
                'msg'       => $e->getMessage()
            ]);
        }
    }

    /**
     * @param $manifest
     * @throws \Exception
     */
    private function saveManifest($manifest)
    {
        $this->manifestResource->save($manifest);
    }

    /**
     * @param $enabledModules
     * @return bool|\Exception
     */
    private function removeManifests($enabledModules)<commit_msg>Added LocalizedException throw<commit_after><?php

namespace Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Manifest;

use Fastly\Cdn\Model\ManifestFactory;
use Fastly\Cdn\Model\ResourceModel\Manifest as ManifestResource;
use Fastly\Cdn\Model\Manifest;
use Fastly\Cdn\Model\Modly\Manifest as Modly;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Fastly\Cdn\Model\ResourceModel\Manifest\CollectionFactory;
use Fastly\Cdn\Model\Api;
use Fastly\Cdn\Helper\Vcl;
use Fastly\Cdn\Model\Config;
use Magento\Framework\Exception\LocalizedException;

/**
 * Class Create
 *
 * @package Fastly\Cdn\Controller\Adminhtml\FastlyCdn\Manifest
 */
class ToggleModules extends Action
{
    /**
     * @var ManifestFactory
     */
    private $manifestFactory;
    /**
     * @var Manifest
     */
    private $manifest;
    /**
     * @var Modly
     */
    private $modly;
    /**
     * @var ManifestResource
     */
    private $manifestResource;
    /**
     * @var JsonFactory
     */
    private $resultJson;
    /**
     * @var Http
     */
    private $request;
    /**
     * @var CollectionFactory
     */
    private $collectionFactory;
    /**
     * @var Api
     */
    private $api;

    private $vcl;

    private $enabledModules = [];

    public function __construct(
        Context $context,
        ManifestFactory $manifestFactory,
        ManifestResource $manifestResource,
        Manifest $manifest,
        Modly $modly,
        JsonFactory $resultJsonFactory,
        Http $request,
        CollectionFactory $collectionFactory,
        Api $api,
        Vcl $vcl
    ) {
        $this->manifestFactory = $manifestFactory;
        $this->manifestResource = $manifestResource;
        $this->manifest = $manifest;
        $this->modly = $modly;
        $this->resultJson = $resultJsonFactory;
        $this->request = $request;
        $this->collectionFactory = $collectionFactory;
        $this->api = $api;
        $this->vcl = $vcl;
        parent::__construct($context);
    }

    public function execute()
    {
        $result = $this->resultJson->create();
        try {
            $checkedModules = $this->getRequest()->getParam('checked_modules');
            if ($checkedModules == null) {
                $checkedModules = [];
            }
            $moduleCollection = $this->collectionFactory->create()->getData();

            foreach ($moduleCollection as $module) {
                $manifest = $this->manifestFactory->create();
                $moduleId = $module['manifest_id'];
                if (in_array($moduleId, $checkedModules)) {
                    $manifest->setManifestId($moduleId);
                    $manifest->setManifestStatus(1);
                } else {
                    if ($module['manifest_status'] == 1) {
                        $this->enabledModules[$moduleId] = $module['manifest_vcl'];
                    }
                    $manifest->setManifestId($moduleId);
                    $manifest->setManifestStatus(0);
                }
                $this->saveManifest($manifest);
            }

            if ($this->enabledModules) {
                $removeStatus = $this->removeManifests($this->enabledModules);
                if ($removeStatus != false) {
                    throw new LocalizedException($removeStatus);
                }
            }

            return $result->setData([
                'status' => true
            ]);
        } catch (\Exception $e) {
            return $result->setData([
                'status'    => false,
                'msg'       => $e->getMessage()
            ]);
        }
    }

    /**
     * @param $manifest
     * @throws \Exception
     */
    private function saveManifest($manifest)
    {
        $this->manifestResource->save($manifest);
    }

    /**
     * @param $enabledModules
     * @return bool|\Exception
     */
    private function removeManifests($enabledModules)
{'fraction_non_alphanumeric': 0.12606409614421632, 'fraction_numerical': 0.0007511266900350526, 'mean_word_length': 1.9231613611416027, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 73, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4666856', 'n_tokens_mistral': 2246, 'n_tokens_neox': 2190, 'n_words': 384}