GetpayPaymentDataRequest   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A build() 0 14 3
A __construct() 0 8 1
A getDataPaymetGetpay() 0 34 5
1
<?php
2
/**
3
 * Copyright © Getnet. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See LICENSE for license details.
7
 */
8
9
namespace Getnet\PaymentMagento\Gateway\Request;
10
11
use Getnet\PaymentMagento\Gateway\Config\Config;
12
use Getnet\PaymentMagento\Gateway\Config\ConfigGetpay;
13
use Getnet\PaymentMagento\Gateway\SubjectReader;
14
use InvalidArgumentException;
15
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
16
use Magento\Payment\Gateway\Request\BuilderInterface;
17
18
/**
19
 * Class Getpay Payment Data Request - Payment data structure for Getpay.
20
 */
21
class GetpayPaymentDataRequest implements BuilderInterface
22
{
23
    /**
24
     * Payment - Block Name.
25
     */
26
    public const PAYMENT = 'payment';
27
28
    /**
29
     * Payment Credit - Block Name.
30
     */
31
    public const PAYMENT_CREDIT = 'credit';
32
33
    /**
34
     * Payment Credit Enable - Block Name.
35
     */
36
    public const PAYMENT_CREDIT_ENABLE = 'enable';
37
38
    /**
39
     * Payment Credit Installments - Block Name.
40
     */
41
    public const PAYMENT_CREDIT_INSTALLMENTS = 'max_installments';
42
43
    /**
44
     * Payment Debit - Block Name.
45
     */
46
    public const PAYMENT_DEBIT = 'debit';
47
48
    /**
49
     * Payment Debit Enable - Block Name.
50
     */
51
    public const PAYMENT_DEBIT_ENABLE = 'enable';
52
53
    /**
54
     * Payment Debit Caixa Virtual Card - Block Name.
55
     */
56
    public const PAYMENT_DEBIT_CAIXA = 'caixa_virtual_card';
57
58
    /**
59
     * Payment Pix - Block Name.
60
     */
61
    public const PAYMENT_PIX = 'pix';
62
63
    /**
64
     * Payment Debit Enable - Block Name.
65
     */
66
    public const PAYMENT_PIX_ENABLE = 'enable';
67
68
    /**
69
     * Payment Qr Code - Block Name.
70
     */
71
    public const PAYMENT_QR_CODE = 'qr_code';
72
73
    /**
74
     * Payment Qr Code Enable - Block Name.
75
     */
76
    public const PAYMENT_QR_CODE_ENABLE = 'enable';
77
78
    /**
79
     * @var SubjectReader
80
     */
81
    protected $subjectReader;
82
83
    /**
84
     * @var Config
85
     */
86
    protected $config;
87
88
    /**
89
     * @var ConfigGetpay
90
     */
91
    protected $configGetpay;
92
93
    /**
94
     * @param SubjectReader $subjectReader
95
     * @param Config        $config
96
     * @param ConfigGetpay  $configGetpay
97
     */
98
    public function __construct(
99
        SubjectReader $subjectReader,
100
        Config $config,
101
        ConfigGetpay $configGetpay
102
    ) {
103
        $this->subjectReader = $subjectReader;
104
        $this->config = $config;
105
        $this->configGetpay = $configGetpay;
106
    }
107
108
    /**
109
     * Build.
110
     *
111
     * @param array $buildSubject
112
     */
113
    public function build(array $buildSubject)
114
    {
115
        if (!isset($buildSubject['payment'])
116
            || !$buildSubject['payment'] instanceof PaymentDataObjectInterface
117
        ) {
118
            throw new InvalidArgumentException('Payment data object should be provided');
119
        }
120
121
        $paymentDO = $buildSubject['payment'];
122
        $order = $paymentDO->getOrder();
123
        $storeId = $order->getStoreId();
124
        $result = $this->getDataPaymetGetpay($storeId);
125
126
        return $result;
127
    }
128
129
    /**
130
     * Data for Getpay.
131
     *
132
     * @param int $storeId
133
     *
134
     * @return array
135
     */
136
    public function getDataPaymetGetpay($storeId)
137
    {
138
        $instruction = [];
139
140
        $methods = $this->configGetpay->getAllowedMethods($storeId);
141
142
        $maxInstallments = $this->configGetpay->getMaxInstallments($storeId);
143
144
        if (in_array(self::PAYMENT_CREDIT, $methods)) {
145
            $instruction[self::PAYMENT][self::PAYMENT_CREDIT] = [
146
                self::PAYMENT_CREDIT_ENABLE       => true,
147
                self::PAYMENT_CREDIT_INSTALLMENTS => $maxInstallments,
148
            ];
149
        }
150
151
        if (in_array(self::PAYMENT_DEBIT, $methods)) {
152
            $instruction[self::PAYMENT][self::PAYMENT_DEBIT] = [
153
                self::PAYMENT_DEBIT_ENABLE => true,
154
            ];
155
        }
156
157
        if (in_array(self::PAYMENT_PIX, $methods)) {
158
            $instruction[self::PAYMENT][self::PAYMENT_PIX] = [
159
                self::PAYMENT_PIX_ENABLE => true,
160
            ];
161
        }
162
163
        if (in_array(self::PAYMENT_QR_CODE, $methods)) {
164
            $instruction[self::PAYMENT][self::PAYMENT_QR_CODE] = [
165
                self::PAYMENT_QR_CODE_ENABLE => true,
166
            ];
167
        }
168
169
        return $instruction;
170
    }
171
}
172