Country::isStateNeeded()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Helper;
28
29
use Payone\Core\Model\PayoneConfig;
30
31
/**
32
 * Helper class for everything that has to do with countries
33
 */
34
class Country extends \Payone\Core\Helper\Base
35
{
36
    /**
37
     * List of all countries where the state parameter has to be submitted
38
     *
39
     * @var array
40
     */
41
    static protected $aStateNeeded = [
42
        'US',
43
        'CA',
44
        'CN',
45
        'JP',
46
        'MX',
47
        'BR',
48
        'AR',
49
        'ID',
50
        'TH',
51
        'IN',
52
    ];
53
54
    /**
55
     * Country object
56
     *
57
     * @var \Magento\Directory\Model\Country
58
     */
59
    protected $country;
60
61
    /**
62
     * Constructor
63
     *
64
     * @param \Magento\Framework\App\Helper\Context      $context
65
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
66
     * @param \Payone\Core\Helper\Shop                   $shopHelper
67
     * @param \Magento\Directory\Model\Country           $country
68
     */
69
    public function __construct(
70
        \Magento\Framework\App\Helper\Context $context,
71
        \Magento\Store\Model\StoreManagerInterface $storeManager,
72
        \Payone\Core\Helper\Shop $shopHelper,
73
        \Magento\Directory\Model\Country $country
74
    ) {
75
        parent::__construct($context, $storeManager, $shopHelper);
76
        $this->country = $country;
77
    }
78
79
    /**
80
     * Get country name by its ISO2 abbreviation
81
     *
82
     * @param  string $sCountryCode
83
     * @return string|bool
84
     */
85
    public function getCountryNameByIso2($sCountryCode)
86
    {
87
        $oCountry = $this->country->loadByCode($sCountryCode);
88
        if ($oCountry) {
89
            return $oCountry->getName();
90
        }
91
        return false;
92
    }
93
94
    /**
95
     * Get all activated debit SEPA countries
96
     *
97
     * @return array
98
     */
99 View Code Duplication
    public function getDebitSepaCountries()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
100
    {
101
        $aReturn = [];
102
103
        $sCountries = $this->getConfigParam('sepa_country', PayoneConfig::METHOD_DEBIT, 'payone_payment');
104
        if ($sCountries) {
105
            $aCountries = explode(',', $sCountries);
106
            foreach ($aCountries as $sCountryCode) {
107
                $sCountryName = $this->getCountryNameByIso2($sCountryCode);
108
                if ($sCountryName) {
109
                    $aReturn[] = [
110
                        'id' => $sCountryCode,
111
                        'title' => $sCountryName,
112
                    ];
113
                }
114
            }
115
        }
116
        return $aReturn;
117
    }
118
119
    /**
120
     * Return if state parameter has to be added for the given country
121
     *
122
     * @param  string $sIsoToCountry
123
     * @return bool
124
     */
125
    public static function isStateNeeded($sIsoToCountry)
126
    {
127
        if (array_search($sIsoToCountry, self::$aStateNeeded) !== false) {
128
            return true;
129
        }
130
        return false;
131
    }
132
}
133