Passed
Pull Request — main (#3)
by Bruno
04:28
created

ConfigGetpay   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 59
c 1
b 0
f 0
dl 0
loc 217
rs 10
wmc 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllowedMethods() 0 9 1
A getExpiration() 0 10 1
A getInstructionCheckout() 0 8 1
A getMaxInstallments() 0 7 1
A isActive() 0 8 1
A getProductType() 0 7 1
A __construct() 0 10 1
A getExpirationFormat() 0 10 1
A getTitle() 0 8 1
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 Magento\Framework\App\Config\ScopeConfigInterface;
13
use Magento\Framework\Stdlib\DateTime\DateTime;
14
use Magento\Payment\Gateway\Config\Config as PaymentConfig;
15
use Magento\Store\Model\ScopeInterface;
16
17
/**
18
 * Class ConfigGetpay - Returns form of payment configuration properties.
19
 */
20
class ConfigGetpay extends PaymentConfig
21
{
22
    /**
23
     * @const string
24
     */
25
    public const METHOD = 'getnet_paymentmagento_getpay';
26
27
    /**
28
     * @const string
29
     */
30
    public const ACTIVE = 'active';
31
32
    /**
33
     * @const string
34
     */
35
    public const TITLE = 'title';
36
37
    /**
38
     * @const string
39
     */
40
    public const INSTRUCTION_CHECKOUT = 'instruction_checkout';
41
42
    /**
43
     * @const string
44
     */
45
    public const EXPIRATION = 'expiration';
46
47
    /**
48
     * @const string
49
     */
50
    public const PRODUCT_TYPE = 'product_type';
51
52
    /**
53
     * @const string
54
     */
55
    public const ALLOWED_METHODS = 'allowed_methods';
56
    
57
    /**
58
     * @const string
59
     */
60
    public const MAX_INSTALLMENTS = "max_installments";
61
62
    /**
63
     * @var ScopeConfigInterface
64
     */
65
    protected $scopeConfig;
66
67
    /**
68
     * @var DateTime
69
     */
70
    protected $date;
71
72
    /**
73
     * @var Config
74
     */
75
    protected $config;
76
77
    /**
78
     * @param ScopeConfigInterface $scopeConfig
79
     * @param DateTime             $date
80
     * @param Config               $config
81
     * @param string|null          $methodCode
82
     */
83
    public function __construct(
84
        ScopeConfigInterface $scopeConfig,
85
        DateTime $date,
86
        Config $config,
87
        $methodCode = null
88
    ) {
89
        parent::__construct($scopeConfig, $methodCode);
90
        $this->scopeConfig = $scopeConfig;
91
        $this->date = $date;
92
        $this->config = $config;
93
    }
94
95
    /**
96
     * Get Payment configuration status.
97
     *
98
     * @param int|null $storeId
99
     *
100
     * @return bool
101
     */
102
    public function isActive($storeId = null): bool
103
    {
104
        $pathPattern = 'payment/%s/%s';
105
106
        return (bool) $this->scopeConfig->getValue(
107
            sprintf($pathPattern, self::METHOD, self::ACTIVE),
108
            ScopeInterface::SCOPE_STORE,
109
            $storeId
110
        );
111
    }
112
113
    /**
114
     * Get title of payment.
115
     *
116
     * @param int|null $storeId
117
     *
118
     * @return string|null
119
     */
120
    public function getTitle($storeId = null): ?string
121
    {
122
        $pathPattern = 'payment/%s/%s';
123
124
        return $this->scopeConfig->getValue(
125
            sprintf($pathPattern, self::METHOD, self::TITLE),
126
            ScopeInterface::SCOPE_STORE,
127
            $storeId
128
        );
129
    }
130
131
    /**
132
     * Get Expiration.
133
     *
134
     * @param int|null $storeId
135
     *
136
     * @return string
137
     */
138
    public function getExpiration($storeId = null): ?string
139
    {
140
        $pathPattern = 'payment/%s/%s';
141
        $due = $this->scopeConfig->getValue(
142
            sprintf($pathPattern, self::METHOD, self::EXPIRATION),
143
            ScopeInterface::SCOPE_STORE,
144
            $storeId
145
        );
146
147
        return $this->date->gmtDate('Y-m-d H:i:s', strtotime("+{$due} days"));
148
    }
149
150
    /**
151
     * Get Instruction - Checkoout.
152
     *
153
     * @param int|null $storeId
154
     *
155
     * @return string|null
156
     */
157
    public function getInstructionCheckout($storeId = null): ?string
158
    {
159
        $pathPattern = 'payment/%s/%s';
160
161
        return $this->scopeConfig->getValue(
162
            sprintf($pathPattern, self::METHOD, self::INSTRUCTION_CHECKOUT),
163
            ScopeInterface::SCOPE_STORE,
164
            $storeId
165
        );
166
    }
167
168
    /**
169
     * Get Expiration Formart.
170
     *
171
     * @param int|null $storeId
172
     *
173
     * @return string
174
     */
175
    public function getExpirationFormat($storeId = null): ?string
176
    {
177
        $pathPattern = 'payment/%s/%s';
178
        $due = $this->scopeConfig->getValue(
179
            sprintf($pathPattern, self::METHOD, self::EXPIRATION),
180
            ScopeInterface::SCOPE_STORE,
181
            $storeId
182
        );
183
184
        return $this->date->gmtDate('d/m/Y h:m:s', strtotime("+{$due} days"));
185
    }
186
187
    /**
188
     * Get Expiration Formart.
189
     *
190
     * @param int|null $storeId
191
     *
192
     * @return string
193
     */
194
    public function getProductType($storeId = null): ?string
195
    {
196
        $pathPattern = 'payment/%s/%s';
197
        return $this->scopeConfig->getValue(
198
            sprintf($pathPattern, self::METHOD, self::PRODUCT_TYPE),
199
            ScopeInterface::SCOPE_STORE,
200
            $storeId
201
        );
202
    }
203
204
    /**
205
     * Get Max Installments.
206
     *
207
     * @param int|null $storeId
208
     *
209
     * @return string
210
     */
211
    public function getMaxInstallments($storeId = null): ?string
212
    {
213
        $pathPattern = 'payment/%s/%s';
214
        return $this->scopeConfig->getValue(
215
            sprintf($pathPattern, self::METHOD, self::MAX_INSTALLMENTS),
216
            ScopeInterface::SCOPE_STORE,
217
            $storeId
218
        );
219
    }
220
221
    /**
222
     * Get Allowed Methods.
223
     *
224
     * @param int|null $storeId
225
     *
226
     * @return array
227
     */
228
    public function getAllowedMethods($storeId = null): ?array
229
    {
230
        $pathPattern = 'payment/%s/%s';
231
        $methods = $this->scopeConfig->getValue(
232
            sprintf($pathPattern, self::METHOD, self::ALLOWED_METHODS),
233
            ScopeInterface::SCOPE_STORE,
234
            $storeId
235
        );
236
        return explode(',', $methods);
237
    }
238
}
239