HostedIframe   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 16
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 147
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B getHostedParams() 0 14 5
B getFieldConfigField() 0 22 4
A getFieldConfig() 0 11 2
A getDefaultStyles() 0 11 1
A getHostedFieldConfig() 0 15 3
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
/**
30
 * Helper class for everything that has to do with hosted Iframe
31
 */
32
class HostedIframe extends \Payone\Core\Helper\Base
33
{
34
    /**
35
     * Configuration params for the hosted Iframe creditcard implementation
36
     *
37
     * @var array
38
     */
39
    protected $aHostedParams = null;
40
41
    /**
42
     * PAYONE payment helper
43
     *
44
     * @var \Payone\Core\Helper\Payment
45
     */
46
    protected $paymentHelper;
47
48
    /**
49
     * PAYONE toolkit helper
50
     *
51
     * @var \Payone\Core\Helper\Toolkit
52
     */
53
    protected $toolkitHelper;
54
55
    /**
56
     * Constructor
57
     *
58
     * @param \Magento\Framework\App\Helper\Context      $context
59
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
60
     * @param \Payone\Core\Helper\Shop                   $shopHelper
61
     * @param \Payone\Core\Helper\Payment                $paymentHelper
62
     */
63
    public function __construct(
64
        \Magento\Framework\App\Helper\Context $context,
65
        \Magento\Store\Model\StoreManagerInterface $storeManager,
66
        \Payone\Core\Helper\Shop $shopHelper,
67
        \Payone\Core\Helper\Payment $paymentHelper
68
    ) {
69
        parent::__construct($context, $storeManager, $shopHelper);
70
        $this->paymentHelper = $paymentHelper;
71
    }
72
73
    /**
74
     * Get hosted params configuration
75
     *
76
     * @return array
77
     */
78
    protected function getHostedParams()
79
    {
80
        if ($this->aHostedParams === null) {
81
            $this->aHostedParams = [];
82
            $sHostedParams = $this->getConfigParam('cc_template', 'creditcard'); // get params from config
83
            if ($sHostedParams) { // params set in config?
84
                $aHostedParams = $this->unserialize($sHostedParams); // array from serialized string
85
                if (is_array($aHostedParams) && !empty($aHostedParams)) {
86
                    $this->aHostedParams = $aHostedParams;
87
                }
88
            }
89
        }
90
        return $this->aHostedParams;
91
    }
92
93
    /**
94
     * Get the field config for one field type for hosted Iframe implementation
95
     *
96
     * @param  string $sName
97
     * @param  string $sParamPrefix
98
     * @return array
99
     */
100
    protected function getFieldConfigField($sName, $sParamPrefix)
101
    {
102
        $aHostedParams = $this->getHostedParams();
103
104
        $aField = [];
105
        if (!empty($aHostedParams)) {
106
            $aField['selector'] = $sName;
107
            $aField['type'] = $aHostedParams[$sParamPrefix.'_type'];
108
            $aField['size'] = $aHostedParams[$sParamPrefix.'_count'];
109
            $aField['maxlength'] = $aHostedParams[$sParamPrefix.'_max'];
110
            if ($aHostedParams[$sParamPrefix.'_style'] == "custom") {
111
                $aField['style'] = $aHostedParams[$sParamPrefix.'_css'];
112
            }
113
            if ($aHostedParams[$sParamPrefix.'_iframe'] == "custom") {
114
                $aField['iframe'] = [
115
                    'width' => $aHostedParams[$sParamPrefix.'_width'],
116
                    'height' => $aHostedParams[$sParamPrefix.'_height'],
117
                ];
118
            }
119
        }
120
        return $aField;
121
    }
122
123
    /**
124
     * Create field config array
125
     *
126
     * @return array
127
     */
128
    protected function getFieldConfig()
129
    {
130
        $aFields = [];
131
        $aFields['cardpan'] = $this->getFieldConfigField('cardpan', 'Number');
132
        if ($this->paymentHelper->isCheckCvcActive() === true) { // cvc field activated?
133
            $aFields['cardcvc2'] = $this->getFieldConfigField('cardcvc2', 'CVC');
134
        }
135
        $aFields['cardexpiremonth'] = $this->getFieldConfigField('cardexpiremonth', 'Month');
136
        $aFields['cardexpireyear'] = $this->getFieldConfigField('cardexpireyear', 'Year');
137
        return $aFields;
138
    }
139
140
    /**
141
     * Create default style array
142
     *
143
     * @param  array $aHostedParams
144
     * @return array
145
     */
146
    protected function getDefaultStyles($aHostedParams)
147
    {
148
        $aDefaultStyle = [];
149
        $aDefaultStyle['input'] = $aHostedParams['Standard_input'];
150
        $aDefaultStyle['select'] = $aHostedParams['Standard_selection'];
151
        $aDefaultStyle['iframe'] = [
152
            'width' => $aHostedParams['Iframe_width'],
153
            'height' => $aHostedParams['Iframe_height'],
154
        ];
155
        return $aDefaultStyle;
156
    }
157
158
    /**
159
     * Generate the complete hosted iframe configuration
160
     *
161
     * @return array
162
     */
163
    public function getHostedFieldConfig()
164
    {
165
        $aHostedParams = $this->getHostedParams(); // get hosted params from config
166
167
        $aFieldConfig = [];
168
        if (!empty($aHostedParams)) { // hosted iframe config existing?
169
            $aFieldConfig['fields'] = $this->getFieldConfig(); // generate config for all field types
170
            $aFieldConfig['defaultStyle'] = $this->getDefaultStyles($aHostedParams);
171
            if ($aHostedParams['Errors_active'] == "true") {
172
                $aFieldConfig['error'] = 'errorOutput'; // area to display error-messages (optional)
173
                $aFieldConfig['language'] = $aHostedParams['Errors_lang']; // has to be defined in javascript
174
            }
175
        }
176
        return $aFieldConfig;
177
    }
178
}
179