Passed
Pull Request — master (#354)
by
unknown
02:46
created

Country::getEnabledCountries()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 2
nop 1
dl 0
loc 18
rs 9.9
c 0
b 0
f 0
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
0 ignored issues
show
Bug introduced by
The type Magento\Directory\Model\Country was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Helper\Context was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
        \Magento\Store\Model\StoreManagerInterface $storeManager,
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\StoreManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
    public function getEnabledCountries($sPaymentMethod)
100
    {
101
        $aReturn = [];
102
103
        $sCountries = $this->getConfigParam('sepa_country', $sPaymentMethod, '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