<commit_before><?php

namespace Remp\MailerModule\Config;

use Remp\MailerModule\Repository\ConfigsRepository;

class Config
{
    const TYPE_STRING = 'string';
    const TYPE_INT = 'integer';
    const TYPE_TEXT = 'text';
    const TYPE_PASSWORD = 'password';
    const TYPE_HTML = 'html';
    const TYPE_SELECT = 'select';

    private $loaded = false;

    /** @var  ConfigsRepository */
    private $configsRepository;

    /** @var LocalConfig  */
    private $localConfig;

    private $items = null;

    public function __construct(ConfigsRepository $configsRepository, LocalConfig $localConfig)
    {
        $this->configsRepository = $configsRepository;
        $this->localConfig = $localConfig;
    }

    public function get($name)
    {
        if (!$this->loaded) {
            $this->initAutoload();
        }

        if (isset($this->items[$name])) {
            $item = $this->items[$name];
            $value = $item->value;

            if ($this->localConfig->exists($name)) {
                $value = $this->localConfig->value($name);
            }

            return $this->formatValue($value, $item->type);
        }

        $item = $this->configsRepository->loadByName($name);
        if ($item) {
            $value = $item->value;

            if ($this->localConfig->exists($name)) {
                $value = $this->localConfig->value($name);
            }

            return $this->formatValue($value, $item->type);
        }

        throw new ConfigNotExistsException("Setting {$name} does not exists.");
    }

    private function initAutoload()
    {
        $items = $this->configsRepository->loadAllAutoload();
        foreach ($items as $item) {
            $this->items[$item->name] = $item;
        }
        $this->loaded = true;
    }

    private function formatValue($value, $type = 'string')
    {
        if ($type == self::TYPE_INT) {
            return intval($value);
        }

        return $value;
    }<commit_msg>Caching application config to avoid connection occupation<commit_after><?php

namespace Remp\MailerModule\Config;

use Remp\MailerModule\Repository\ConfigsRepository;
use Nette\Caching\Cache;
use Nette\Caching\IStorage;

class Config
{
    const TYPE_STRING = 'string';
    const TYPE_INT = 'integer';
    const TYPE_TEXT = 'text';
    const TYPE_PASSWORD = 'password';
    const TYPE_HTML = 'html';
    const TYPE_SELECT = 'select';

    private $loaded = false;

    private $configsRepository;

    private $localConfig;

    private $cacheStorage;

    private $items = null;

    public function __construct(
        ConfigsRepository $configsRepository,
        LocalConfig $localConfig,
        IStorage $cacheStorage
    ) {
        $this->configsRepository = $configsRepository;
        $this->localConfig = $localConfig;
        $this->cacheStorage = $cacheStorage;
    }

    public function get($name)
    {
        if (!$this->loaded) {
            $this->initAutoload();
        }

        if (isset($this->items[$name])) {
            $item = $this->items[$name];
            $value = $item->value;

            if ($this->localConfig->exists($name)) {
                $value = $this->localConfig->value($name);
            }

            return $this->formatValue($value, $item->type);
        }

        $item = $this->configsRepository->loadByName($name);
        if ($item) {
            $value = $item->value;

            if ($this->localConfig->exists($name)) {
                $value = $this->localConfig->value($name);
            }

            return $this->formatValue($value, $item->type);
        }

        throw new ConfigNotExistsException("Setting {$name} does not exists.");
    }

    private function initAutoload()
    {
        $cacheData = $this->cacheStorage->read('application_autoload_cache');
        if ($cacheData) {
            $this->items = $cacheData;
        } else {
            $items = $this->configsRepository->loadAllAutoload();
            foreach ($items as $item) {
                $this->items[$item->name] = (object)$item->toArray();
            }
            $this->cacheStorage->write('application_autoload_cache', $this->items, [Cache::EXPIRE => 60]);
        }
        $this->loaded = true;
    }

    private function formatValue($value, $type = 'string')
    {
        if ($type == self::TYPE_INT) {
            return intval($value);
        }

        return $value;
    }
{'fraction_non_alphanumeric': 0.1414324569356301, 'fraction_numerical': 0.00045330915684496827, 'mean_word_length': 2.1771058315334773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 66, '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': '4647854', 'n_tokens_mistral': 1271, 'n_tokens_neox': 1235, 'n_words': 250}