Config.php 3.9 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
<?php
/**
 * This file is part of the Klarna Core module
 *
 * (c) Klarna Bank AB (publ)
 *
 * For the full copyright and license information, please view the NOTICE
 * and LICENSE files that were distributed with this source code.
 */

/**
 * Configuration paths storage
 *
 * @author     Magento Core Team <core@magentocommerce.com>
 */

namespace Klarna\Core\Model;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Store\Model\Store;

/**
 * @SuppressWarnings(PHPMD.ExcessivePublicCount)
 */
class Config
{
    const CONFIG_XML_PATH_KLARNA_DEBUG                      = 'klarna/api/debug';
    const CONFIG_XML_PATH_KLARNA_TEST_MODE                  = 'klarna/api/test_mode';
    const CONFIG_XML_PATH_GENERAL_STORE_INFORMATION_COUNTRY = 'general/store_information/country_id';
    const CONFIG_XML_PATH_GENERAL_STATE_OPTIONS             = 'general/region/state_required';

    /**
     * Core store config
     *
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    private $scopeConfig;

    /**
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     */
    public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig)
    {
        $this->scopeConfig = $scopeConfig;
    }

    /**
     * Check what taxes should be applied after discount
     *
     * @param   null|string|bool|int|Store $store
     * @return  bool
     */
    public function storeAddressSet($store = null)
    {
        return (bool)$this->scopeConfig->getValue(
            self::CONFIG_XML_PATH_GENERAL_STORE_INFORMATION_COUNTRY,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $store
        );
    }

    /**
     * Get configuration setting "Apply Discount On Prices Including Tax" value
     *
     * @param   null|string|bool|int|Store $store
     * @return  bool
     */
    public function debugModeWhileLive($store = null)
    {
        if ($this->testMode($store)) {
            return false;
        }
        return $this->debugMode($store);
    }

    /**
     * Get defined tax calculation algorithm
     *
     * @param   null|string|bool|int|Store $store
     * @return  string
     */
    public function testMode($store = null)
    {
        return $this->scopeConfig->isSetFlag(
            self::CONFIG_XML_PATH_KLARNA_TEST_MODE,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $store
        );
    }

    /**
     * Get tax class id specified for shipping tax estimation
     *
     * @param   null|string|bool|int|Store $store
     * @return  int
     */
    public function debugMode($store = null)
    {
        return $this->scopeConfig->isSetFlag(
            self::CONFIG_XML_PATH_KLARNA_DEBUG,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
            $store
        );
    }

    /**
     * Return a list of countries that incorrectly have the state/region marked as required
     *
     * @return array
     */
    public function requiredRegions()
    {
        $failed = [];
        $knownCountriesWithOptionalRegion = [
            'at',
            'de',
            'fi',
        ];
        $countries = $this->scopeConfig->getValue(self::CONFIG_XML_PATH_GENERAL_STATE_OPTIONS);
        $countries = explode(',', $countries);
        foreach ($knownCountriesWithOptionalRegion as $country) {
            if (in_array($country, $countries)) {
                $failed[] = $country;
            }
        }
        return $failed;
    }

    /**
     * Determine if a Klarna Payment method is enabled
     *
     * @param StoreInterface $store
     * @return bool
     * @SuppressWarnings(PMD.UnusedFormalParameter)
     */
    public function klarnaEnabled($store = null)
    {
        // It is expected that this method will have plugins added by other modules. $store is required in those cases.
        return false;
    }
}