<commit_before><?php

namespace lo\plugins\services;

use lo\plugins\dto\EventsDiffDto;
use lo\plugins\dto\EventsPoolDto;
use lo\plugins\dto\PluginDataDto;
use lo\plugins\dto\PluginsDiffDto;
use lo\plugins\dto\PluginsPoolDto;
use lo\plugins\repositories\EventDbRepository;
use lo\plugins\repositories\EventDirRepository;
use lo\plugins\repositories\PluginDbRepository;
use lo\plugins\repositories\PluginDirRepository;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;

class PluginService
{
    private $eventDbRepository;
    private $eventDirRepository;
    private $pluginDbRepository;
    private $pluginDirRepository;

    public function __construct(
        PluginDirRepository $pluginDirRepository,
        PluginDbRepository $pluginDbRepository,
        EventDirRepository $eventDirRepository,
        EventDbRepository $eventDbRepository
    )
    {
        $this->pluginDirRepository = $pluginDirRepository;
        $this->pluginDbRepository = $pluginDbRepository;
        $this->eventDirRepository = $eventDirRepository;
        $this->eventDbRepository = $eventDbRepository;
    }


    /**
     * @param $dirs
     * @return mixed
     * @throws InvalidConfigException
     */
    public function getPlugins($dirs)
    {
        $this->pluginDirRepository->setDirs($dirs);

        $pluginsArrayDir = $this->pluginDirRepository->findAllAsArray();
        $pluginsArrayDb = $this->pluginDbRepository->findAllAsArray();

        $pluginsDiffDir = new PluginsDiffDto($pluginsArrayDir);
        $pluginsDiffDb = new PluginsDiffDto($pluginsArrayDb);

        $pluginsPoolDir = new PluginsPoolDto($pluginsArrayDir);
        $pluginsPoolDb = new PluginsPoolDto($pluginsArrayDb);

        $data = [];
        foreach (array_filter(array_diff($pluginsDiffDir->getDiff(), $pluginsDiffDb->getDiff())) as $key => $value) {
            $pool = ArrayHelper::merge($pluginsPoolDb->getInfo($key), $pluginsPoolDir->getInfo($key));
            $data[$key] = new PluginDataDto($pool);
        }
        return $data;
    }

    /**
     * @param $hash
     */
    public function installPlugin($hash)
    {
        $pluginsArrayDir = $this->pluginDirRepository->findAllAsArray();
        $pluginsArrayDb = $this->pluginDbRepository->findAllAsArray();

        $pluginsPoolDir = new PluginsPoolDto($pluginsArrayDir);
        $pluginsPoolDb = new PluginsPoolDto($pluginsArrayDb);

        $pluginInfoDir = $pluginsPoolDir->getInfo($hash);
        $pluginInfoDb = $pluginsPoolDb->getInfo($hash);

        $pluginDataDto = new PluginDataDto($pluginInfoDir);
        $pluginClass = $pluginDataDto->getPluginClass();

        $eventsArrayDir = $this->eventDirRepository->findEventsByHandler($pluginClass);

        if ($pluginInfoDb) {
            /** Update plugin */
            $data = ArrayHelper::merge($pluginInfoDb, $pluginInfoDir);
            $plugin = $this->pluginDbRepository->savePlugin($hash, $data);

            $eventsArrayDb = $this->eventDbRepository->findEventsByHandler($pluginClass);

            $eventsDiffDir = new EventsDiffDto($eventsArrayDir);
            $eventsDiffDb = new EventsDiffDto($eventsArrayDb);

            $eventsPoolDir = new EventsPoolDto($eventsArrayDir);
            $eventsPoolDb = new EventsPoolDto($eventsArrayDb);

            /** Get Deleted events */
            foreach (array_filter(array_diff($eventsDiffDb->getDiff(), $eventsDiffDir->getDiff())) as $key) {
                $data = $eventsPoolDb->getInfo($key);
                $this->eventDbRepository->deleteEvent($data);
            }

            /** Get Installed events */
            foreach (array_filter(array_diff($eventsDiffDir->getDiff(), $eventsDiffDb->getDiff())) as $key) {
                $data = $eventsPoolDir->getInfo($key);
                $event = $this->eventDbRepository->addEvent($data);
                $this->pluginDbRepository->link($plugin, $event);
            }

            Yii::$app->session->setFlash('success', 'Plugin updated');

        } else {
            /** Install plugin */
            $plugin = $this->pluginDbRepository->addPlugin($pluginInfoDir);

            foreach ($eventsArrayDir as $data) {
                $event = $this->eventDbRepository->addEvent($data);
                $this->pluginDbRepository->link($plugin, $event);
            }

            Yii::$app->session->setFlash('success', 'Plugin installed');
        }
    }
}<commit_msg>refactoring dto<commit_after><?php

namespace lo\plugins\services;

use lo\plugins\dto\EventsDiffDto;
use lo\plugins\dto\EventsPoolDto;
use lo\plugins\dto\PluginDataDto;
use lo\plugins\dto\PluginsDiffDto;
use lo\plugins\dto\PluginsPoolDto;
use lo\plugins\repositories\EventDbRepository;
use lo\plugins\repositories\EventDirRepository;
use lo\plugins\repositories\PluginDbRepository;
use lo\plugins\repositories\PluginDirRepository;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;

class PluginService
{
    /**
     *  Repositories
     */
    private $eventDbRepository;
    private $eventDirRepository;
    private $pluginDbRepository;
    private $pluginDirRepository;

    /**
     * Dto
     */
    private $_pluginsPoolDir;
    private $_pluginsPoolDb;
    private $_pluginsDiffDb;
    private $_pluginsDiffDir;

    /**
     * PluginService constructor.
     * @param PluginDirRepository $pluginDirRepository
     * @param PluginDbRepository $pluginDbRepository
     * @param EventDirRepository $eventDirRepository
     * @param EventDbRepository $eventDbRepository
     */
    public function __construct(
        PluginDirRepository $pluginDirRepository,
        PluginDbRepository $pluginDbRepository,
        EventDirRepository $eventDirRepository,
        EventDbRepository $eventDbRepository
    )
    {
        $this->pluginDirRepository = $pluginDirRepository;
        $this->pluginDbRepository = $pluginDbRepository;
        $this->eventDirRepository = $eventDirRepository;
        $this->eventDbRepository = $eventDbRepository;
    }


    /**
     * @param $dirs
     * @return mixed
     * @throws InvalidConfigException
     */
    public function getPlugins($dirs)
    {
        $this->pluginDirRepository->setDirs($dirs);

        $pluginsDiffDir = $this->getPluginsDiffDir();
        $pluginsDiffDb = $this->getPluginsDiffDb();

        $pluginsPoolDir = $this->getPluginsPoolDir();
        $pluginsPoolDb = $this->getPluginsPoolDb();

        $data = [];
        foreach (array_filter(array_diff($pluginsDiffDir->getDiff(), $pluginsDiffDb->getDiff())) as $key => $value) {
            $pool = ArrayHelper::merge($pluginsPoolDb->getInfo($key), $pluginsPoolDir->getInfo($key));
            $data[$key] = new PluginDataDto($pool);
        }
        return $data;
    }

    /**
     * @param $hash
     */
    public function installPlugin($hash)
    {
        $pluginsPoolDir = $this->getPluginsPoolDir();
        $pluginsPoolDb = $this->getPluginsPoolDb();

        $pluginInfoDir = $pluginsPoolDir->getInfo($hash);
        $pluginInfoDb = $pluginsPoolDb->getInfo($hash);

        $pluginDataDto = new PluginDataDto($pluginInfoDir);
        $pluginClass = $pluginDataDto->getPluginClass();

        $eventsArrayDir = $this->eventDirRepository->findEventsByHandler($pluginClass);

        if ($pluginInfoDb) {
            /** Update plugin */
            $data = ArrayHelper::merge($pluginInfoDb, $pluginInfoDir);
            $plugin = $this->pluginDbRepository->savePlugin($hash, $data);

            $eventsArrayDb = $this->eventDbRepository->findEventsByHandler($pluginClass);

            $eventsDiffDir = new EventsDiffDto($eventsArrayDir);
            $eventsDiffDb = new EventsDiffDto($eventsArrayDb);

            $eventsPoolDir = new EventsPoolDto($eventsArrayDir);
            $eventsPoolDb = new EventsPoolDto($eventsArrayDb);

            /** Get Deleted events */
            foreach (array_filter(array_diff($eventsDiffDb->getDiff(), $eventsDiffDir->getDiff())) as $key) {
                $data = $eventsPoolDb->getInfo($key);
                $this->eventDbRepository->deleteEvent($data);
            }

            /** Get Installed events */
            foreach (array_filter(array_diff($eventsDiffDir->getDiff(), $eventsDiffDb->getDiff())) as $key) {
                $data = $eventsPoolDir->getInfo($key);
                $event = $this->eventDbRepository->addEvent($data);
                $this->pluginDbRepository->link($plugin, $event);
            }

            Yii::$app->session->setFlash('success', 'Plugin updated');

        } else {
            /** Install plugin */
            $plugin = $this->pluginDbRepository->addPlugin($pluginInfoDir);

            foreach ($eventsArrayDir as $data) {
                $event = $this->eventDbRepository->addEvent($data);
                $this->pluginDbRepository->link($plugin, $event);
            }

            Yii::$app->session->setFlash('success', 'Plugin installed');
        }
    }

    /**
     * @return PluginsPoolDto
     */
    protected function getPluginsPoolDb()
    {
        if (!$this->_pluginsPoolDb) {
            $this->_pluginsPoolDb = new PluginsPoolDto($this->pluginDbRepository->findAllAsArray());
        }
        return $this->_pluginsPoolDb;
    }

    /**
     * @return PluginsPoolDto
     */
    protected function getPluginsPoolDir()
    {
        if (!$this->_pluginsPoolDir) {
            $this->_pluginsPoolDir = new PluginsPoolDto($this->pluginDirRepository->findAllAsArray());
        }
        return $this->_pluginsPoolDir;
    }

    /**
     * @return PluginsDiffDto
     */
    protected function getPluginsDiffDb()
    {
        if (!$this->_pluginsDiffDb) {
            $this->_pluginsDiffDb = new PluginsDiffDto($this->pluginDbRepository->findAllAsArray());
        }
        return $this->_pluginsDiffDb;
    }

    /**
     * @return PluginsDiffDto
     */
    protected function getPluginsDiffDir()
    {
        if (!$this->_pluginsDiffDir) {
            $this->_pluginsDiffDir = new PluginsDiffDto($this->pluginDirRepository->findAllAsArray());
        }
        return $this->_pluginsDiffDir;
    }


}
{'fraction_non_alphanumeric': 0.12602875557759047, 'fraction_numerical': 0.0, 'mean_word_length': 2.8291571753986333, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 121, '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': '7034429', 'n_tokens_mistral': 2820, 'n_tokens_neox': 2783, 'n_words': 441}