Config::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
declare(strict_types=1);
9
10
namespace Getnet\PaymentMagento\Gateway\Config;
11
12
use Getnet\PaymentMagento\Gateway\Data\Order\OrderAdapterFactory;
0 ignored issues
show
Bug introduced by
The type Getnet\PaymentMagento\Ga...der\OrderAdapterFactory 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...
13
use Getnet\PaymentMagento\Gateway\Request\AddressDataRequest;
14
use Magento\Framework\App\Config\ScopeConfigInterface;
15
use Magento\Framework\Serialize\Serializer\Json;
16
use Magento\Payment\Gateway\Config\Config as PaymentConfig;
17
use Magento\Store\Model\ScopeInterface;
18
19
/**
20
 * Class Config - Returns form of payment configuration properties.
21
 */
22
class Config extends PaymentConfig
23
{
24
    /**
25
     * @const string
26
     */
27
    public const METHOD = 'getnet_paymentmagento';
28
29
    /**
30
     * @const int
31
     */
32
    public const ROUND_UP = 100;
33
34
    /**
35
     * @const string
36
     */
37
    public const ENDPOINT_PRODUCTION = 'https://api.getnet.com.br/';
38
39
    /**
40
     * @const string
41
     */
42
    public const ENVIRONMENT_PRODUCTION = 'production';
43
44
    /**
45
     * @const string
46
     */
47
    public const ENDPOINT_HOMOLOG = 'https://api-homologacao.getnet.com.br/';
48
49
    /**
50
     * @const string
51
     */
52
    public const ENVIRONMENT_HOMOLOG = 'homolog';
53
54
    /**
55
     * @const string
56
     */
57
    public const ENDPOINT_SANDBOX = 'https://api-sandbox.getnet.com.br/';
58
59
    /**
60
     * @const string
61
     */
62
    public const ENVIRONMENT_SANDBOX = 'sandbox';
63
64
    /**
65
     * @const string
66
     */
67
    public const CLIENT = 'PaymentMagento';
68
69
    /**
70
     * @var ScopeConfigInterface
71
     */
72
    protected $scopeConfig;
73
74
    /**
75
     * @var Json
76
     */
77
    protected $json;
78
79
    /**
80
     * @param ScopeConfigInterface $scopeConfig
81
     * @param Json                 $json
82
     * @param string               $methodCode
83
     * @SuppressWarnings(PHPMD.StaticAccess)
84
     */
85
    public function __construct(
86
        ScopeConfigInterface $scopeConfig,
87
        Json $json,
88
        $methodCode = self::METHOD
89
    ) {
90
        parent::__construct($scopeConfig, $methodCode);
91
        $this->scopeConfig = $scopeConfig;
92
        $this->json = $json;
93
    }
94
95
    /**
96
     * Formant Price.
97
     *
98
     * @param string|int|float $amount
99
     *
100
     * @return float
101
     */
102
    public function formatPrice($amount): float
103
    {
104
        return round((float) $amount, 2) * self::ROUND_UP;
105
    }
106
107
    /**
108
     * Gets the API endpoint URL.
109
     *
110
     * @param int|null $storeId
111
     *
112
     * @return string
113
     */
114
    public function getApiUrl($storeId = null): ?string
115
    {
116
        $environment = $this->getEnvironmentMode($storeId);
117
118
        if ($environment === 'sandbox') {
119
            return self::ENDPOINT_SANDBOX;
120
        }
121
122
        if ($environment === 'homolog') {
123
            return self::ENDPOINT_HOMOLOG;
124
        }
125
126
        return self::ENDPOINT_PRODUCTION;
127
    }
128
129
    /**
130
     * Gets the Environment Mode.
131
     *
132
     * @param int|null $storeId
133
     *
134
     * @return string
135
     */
136
    public function getEnvironmentMode($storeId = null): ?string
137
    {
138
        $environment = $this->getAddtionalValue('environment', $storeId);
139
140
        if ($environment === 'sandbox') {
141
            return self::ENVIRONMENT_SANDBOX;
142
        }
143
144
        if ($environment === 'homolog') {
145
            return self::ENVIRONMENT_HOMOLOG;
146
        }
147
148
        return self::ENVIRONMENT_PRODUCTION;
149
    }
150
151
    /**
152
     * Gets the Merchant Gateway Seller Id.
153
     *
154
     * @param int|null $storeId
155
     *
156
     * @return string
157
     */
158
    public function getMerchantGatewaySellerId($storeId = null): ?string
159
    {
160
        $sellerId = $this->getAddtionalValue('seller_id_production', $storeId);
161
162
        $environment = $this->getEnvironmentMode($storeId);
163
164
        if ($environment === 'sandbox') {
165
            $sellerId = $this->getAddtionalValue('seller_id_sandbox', $storeId);
166
        }
167
168
        if ($environment === 'homolog') {
169
            $sellerId = $this->getAddtionalValue('seller_id_homolog', $storeId);
170
        }
171
172
        return $sellerId;
173
    }
174
175
    /**
176
     * Gets the Merchant Gateway Client Id.
177
     *
178
     * @param int|null $storeId
179
     *
180
     * @return string
181
     */
182
    public function getMerchantGatewayClientId($storeId = null): ?string
183
    {
184
        $clientId = $this->getAddtionalValue('client_id_production', $storeId);
185
186
        $environment = $this->getEnvironmentMode($storeId);
187
188
        if ($environment === 'sandbox') {
189
            $clientId = $this->getAddtionalValue('client_id_sandbox', $storeId);
190
        }
191
192
        if ($environment === 'homolog') {
193
            $clientId = $this->getAddtionalValue('client_id_homolog', $storeId);
194
        }
195
196
        return $clientId;
197
    }
198
199
    /**
200
     * Gets the Merchant Gateway Client Secret.
201
     *
202
     * @param int|null $storeId
203
     *
204
     * @return string
205
     */
206
    public function getMerchantGatewayClientSecret($storeId = null): ?string
207
    {
208
        $clientSecret = $this->getAddtionalValue('client_secret_production', $storeId);
209
210
        $environment = $this->getEnvironmentMode($storeId);
211
212
        if ($environment === 'sandbox') {
213
            $clientSecret = $this->getAddtionalValue('client_secret_sandbox', $storeId);
214
        }
215
216
        if ($environment === 'homolog') {
217
            $clientSecret = $this->getAddtionalValue('client_secret_homolog', $storeId);
218
        }
219
220
        return $clientSecret;
221
    }
222
223
    /**
224
     * Gets the Merchant Gateway OAuth.
225
     *
226
     * @param int|null $storeId
227
     *
228
     * @return string
229
     */
230
    public function getMerchantGatewayOauth($storeId = null): ?string
231
    {
232
        $oauth = $this->getAddtionalValue('access_token_production', $storeId);
233
234
        $environment = $this->getEnvironmentMode($storeId);
235
236
        if ($environment === 'sandbox') {
237
            $oauth = $this->getAddtionalValue('access_token_sandbox', $storeId);
238
        }
239
240
        if ($environment === 'homolog') {
241
            $oauth = $this->getAddtionalValue('access_token_homolog', $storeId);
242
        }
243
244
        return $oauth;
245
    }
246
247
    /**
248
     * Gets the Merchant Gateway Online Metrix Code.
249
     *
250
     * @param int|null $storeId
251
     *
252
     * @return string
253
     */
254
    public function getMerchantGatewayOnlineMetrixCode($storeId = null): ?string
255
    {
256
        $code = 'k8vif92e';
257
258
        $environment = $this->getEnvironmentMode($storeId);
259
260
        if ($environment === 'sandbox') {
261
            $code = '1snn5n9w';
262
        }
263
264
        if ($environment === 'homolog') {
265
            $code = '1snn5n9w';
266
        }
267
268
        return $code;
269
    }
270
271
    /**
272
     * Gets the Merchant Gateway Dynamic Mcc.
273
     *
274
     * @param int|null $storeId
275
     *
276
     * @return string
277
     */
278
    public function getMerchantGatewayDynamicMcc($storeId = null): ?string
279
    {
280
        return $this->getAddtionalValue('dynamic_mcc', $storeId);
281
    }
282
283
    /**
284
     * Gets the AddtionalValues.
285
     *
286
     * @param string   $field
287
     * @param int|null $storeId
288
     *
289
     * @return string|null
290
     */
291
    public function getAddtionalValue($field, $storeId = null): ?string
292
    {
293
        $pathPattern = 'payment/%s/%s';
294
295
        return $this->scopeConfig->getValue(
296
            sprintf($pathPattern, self::METHOD, $field),
297
            ScopeInterface::SCOPE_STORE,
298
            $storeId
299
        );
300
    }
301
302
    /**
303
     * Get Statement Descriptor.
304
     *
305
     * @param int|null $storeId
306
     *
307
     * @return string|null
308
     */
309
    public function getStatementDescriptor($storeId = null): ?string
310
    {
311
        return $this->getAddtionalValue('statement_descriptor', $storeId);
312
    }
313
314
    /**
315
     * Get Address Limit to Send.
316
     *
317
     * @param string $field
318
     *
319
     * @return int $limitSend
320
     */
321
    public function getAddressLimitSend($field): int
322
    {
323
        $limitSend = 57;
324
        if ($field === AddressDataRequest::STREET) {
325
            $limitSend = 57;
326
        } elseif ($field === AddressDataRequest::NUMBER) {
327
            $limitSend = 6;
328
        } elseif ($field === AddressDataRequest::DISTRICT) {
329
            $limitSend = 60;
330
        } elseif ($field === AddressDataRequest::COMPLEMENT) {
331
            $limitSend = 30;
332
        }
333
334
        return $limitSend;
335
    }
336
337
    /**
338
     * Value For Field Address.
339
     *
340
     * @param OrderAdapterFactory $adress
341
     * @param string              $field
342
     *
343
     * @return string|null
344
     */
345
    public function getValueForAddress($adress, $field): ?string
346
    {
347
        $value = (int) $this->getAddtionalValue($field);
348
        $limitSend = $this->getAddressLimitSend($field);
349
350
        if ($value === 0) {
351
            return substr($adress->getStreetLine1(), 0, $limitSend);
352
        } elseif ($value === 1) {
353
            return substr($adress->getStreetLine2(), 0, $limitSend);
354
        } elseif ($value === 2) {
355
            if ($adress->getStreetLine3()) {
356
                return substr($adress->getStreetLine3(), 0, $limitSend);
357
            }
358
        } elseif ($value === 3) {
359
            if ($adress->getStreetLine4()) {
360
                return substr($adress->getStreetLine4(), 0, $limitSend);
361
            }
362
        }
363
364
        if ($field === AddressDataRequest::DISTRICT) {
365
            return substr($adress->getStreetLine1(), 0, $limitSend);
366
        }
367
368
        return '';
369
    }
370
}
371