<commit_before><?php
/**
 * Shopware 4.0
 * Copyright © 2013 shopware AG
 *
 * According to our dual licensing model, this program can be used either
 * under the terms of the GNU Affero General Public License, version 3,
 * or under a proprietary license.
 *
 * The texts of the GNU Affero General Public License with an additional
 * permission and of our proprietary license can be found at and
 * in the LICENSE file you have received along with this program.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * "Shopware" is a registered trademark of shopware AG.
 * The licensing of the program under the AGPLv3 does not imply a
 * trademark license. Therefore any rights, title and interest in
 * our trademarks remain entirely with us.
 */

namespace ShopwarePlugins\Connect\Components;

use ShopwarePlugins\Connect\Components\Marketplace\MarketplaceSettings;
use Shopware\Components\Model\ModelManager;
use Shopware\CustomModels\Connect\Config as ConfigModel;
use Shopware\Connect\Gateway\PDO;

/**
 * @category  Shopware
 * @package   Shopware\Plugins\SwagConnect
 */
class Config
{
    const MARKETPLACE_URL = 'connect.shopware.com';
    const SN_PREFIX = 'sn.';

    /**
     * @var ModelManager
     */
    private $manager;

    /**
     * @var \Shopware\CustomModels\Connect\ConfigRepository
     */
    private $repository;

    /** @var  \Shopware\Models\Shop\Shop */
    private $shopRepository;

    private $customerGroupRepository;

    private $priceGateway;

    private $connectGateway;

    /**
     * @param ModelManager $manager
     */
    public function __construct(ModelManager $manager)
    {
        $this->manager = $manager;
    }

    /**
     * @param $name
     * @param null $default
     * @param null $shopId
     * @return null
     */
    public function getConfig($name, $default = null, $shopId = null)
    {
        $result = $this->getPluginConfig($name);
        if (!empty($result)) {
            return $result;
        }

        if (is_null($shopId)) {
            return $this->getMainConfig($name, $default);
        }
        $query = $this->getConfigRepository()->getConfigsQuery($name, $shopId);
        $query->setMaxResults(1);
        $result = $query->getResult();

        if (count($result) > 0 && $model = reset($result)) {
            $decodedString = json_decode($model->getValue(), true);
            if ($decodedString !== null) {
                return $decodedString;
            }
            return $model->getValue();
        }

        $shop = $this->getShopRepository()->find($shopId);
        if (!$shop) {
            return $this->getMainConfig($name, $default);
        }

        $mainShop = $shop->getMain();
        if ($mainShop) {
            $mainShopId = $mainShop->getId();
            $query = $this->getConfigRepository()->getConfigsQuery($name, $mainShopId);
            $query->setMaxResults(1);
            $result = $query->getResult();
            $model = $result[0];

            if ($model) {
                $decodedString = json_decode($model->getValue(), true);
                if ($decodedString !== null) {
                    return $decodedString;
                }
                return $model->getValue();
            }
        }

        return $this->getMainConfig($name, $default);
    }

    /**
     * @return bool
     */
    public function hasSsl()
    {
        $builder = $this->manager->getConnection()->createQueryBuilder();
        $builder->select('cs.secure')
            ->from('s_core_shops', 'cs')
            ->where('cs.default = :default')
            ->setParameter('default', true);

        return (bool) $builder->execute()->fetchColumn();
    }

    /**
     * @param $name
     * @param null $default
     * @return mixed
     */
    private function getPluginConfig($name, $default = null)
    {
        return Shopware()->Config()->getByNamespace('SwagConnect', $name, $default);
    }

    /**
     * @param $name
     * @param null $default
     * @return null
     */
    private function getMainConfig($name, $default = null)
    {
        $query = $this->getConfigRepository()->getConfigsQuery($name);
        $query->setMaxResults(1);
        $result = $query->getResult();
        if (count($result) === 0) {
            return $default;
        }

        $decodedString = json_decode($result[0]->getValue(), true);
        if ($decodedString !== null) {
            return $decodedString;
        }
        return $result[0]->getValue();
    }

    /**
     * @param null $name
     * @param null $shopId
     * @param null $groupName
     * @return array
     */
    public function getConfigs($name = null, $shopId = null, $groupName = null)
    {
        $query = $this->getConfigRepository()->getConfigsQuery($name, $shopId, $groupName);

        return $query->getResult();
    }

    /**
     * @param $name
     * @param $value
     * @param null $shopId
     * @param null $groupName
     */
    public function setConfig($name, $value, $shopId = null, $groupName = null)
    {
        $model = $this->getConfigRepository()->findOneBy(array('name' => $name));

        if (!$model) {
            $model = new ConfigModel();<commit_msg>CON-4184 Removed shopId from getConfig<commit_after><?php
/**
 * Shopware 4.0
 * Copyright © 2013 shopware AG
 *
 * According to our dual licensing model, this program can be used either
 * under the terms of the GNU Affero General Public License, version 3,
 * or under a proprietary license.
 *
 * The texts of the GNU Affero General Public License with an additional
 * permission and of our proprietary license can be found at and
 * in the LICENSE file you have received along with this program.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * "Shopware" is a registered trademark of shopware AG.
 * The licensing of the program under the AGPLv3 does not imply a
 * trademark license. Therefore any rights, title and interest in
 * our trademarks remain entirely with us.
 */

namespace ShopwarePlugins\Connect\Components;

use ShopwarePlugins\Connect\Components\Marketplace\MarketplaceSettings;
use Shopware\Components\Model\ModelManager;
use Shopware\CustomModels\Connect\Config as ConfigModel;
use Shopware\Connect\Gateway\PDO;

/**
 * @category  Shopware
 * @package   Shopware\Plugins\SwagConnect
 */
class Config
{
    const MARKETPLACE_URL = 'connect.shopware.com';
    const SN_PREFIX = 'sn.';

    /**
     * @var ModelManager
     */
    private $manager;

    /**
     * @var \Shopware\CustomModels\Connect\ConfigRepository
     */
    private $repository;

    /** @var  \Shopware\Models\Shop\Shop */
    private $shopRepository;

    private $customerGroupRepository;

    private $priceGateway;

    private $connectGateway;

    /**
     * @param ModelManager $manager
     */
    public function __construct(ModelManager $manager)
    {
        $this->manager = $manager;
    }

    /**
     * @param $name
     * @param null $default
     * @param null $shopId
     * @return null
     */
    public function getConfig($name, $default = null, $shopId = null)
    {
        $result = $this->getPluginConfig($name);
        if (!empty($result)) {
            return $result;
        }

        $query = $this->getConfigRepository()->getConfigsQuery($name);
        $query->setMaxResults(1);
        $result = $query->getResult();
        if (count($result) === 0) {
            return $default;
        }

        $decodedString = json_decode($result[0]->getValue(), true);
        if ($decodedString !== null) {
            return $decodedString;
        }
        return $result[0]->getValue();
    }

    /**
     * @return bool
     */
    public function hasSsl()
    {
        $builder = $this->manager->getConnection()->createQueryBuilder();
        $builder->select('cs.secure')
            ->from('s_core_shops', 'cs')
            ->where('cs.default = :default')
            ->setParameter('default', true);

        return (bool) $builder->execute()->fetchColumn();
    }

    /**
     * @param $name
     * @param null $default
     * @return mixed
     */
    private function getPluginConfig($name, $default = null)
    {
        return Shopware()->Config()->getByNamespace('SwagConnect', $name, $default);
    }

    /**
     * @param null $name
     * @param null $shopId
     * @param null $groupName
     * @return array
     */
    public function getConfigs($name = null, $shopId = null, $groupName = null)
    {
        $query = $this->getConfigRepository()->getConfigsQuery($name, $shopId, $groupName);

        return $query->getResult();
    }

    /**
     * @param $name
     * @param $value
     * @param null $shopId
     * @param null $groupName
     */
    public function setConfig($name, $value, $shopId = null, $groupName = null)
    {
        $model = $this->getConfigRepository()->findOneBy(array('name' => $name));

        if (!$model) {
            $model = new ConfigModel();
{'fraction_non_alphanumeric': 0.1284246575342466, 'fraction_numerical': 0.003424657534246575, 'mean_word_length': 2.4457964601769913, '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': '6160892', 'n_tokens_mistral': 2652, 'n_tokens_neox': 2580, 'n_words': 723}