DataAssignObserverCcVault::execute()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 21
c 1
b 0
f 0
nc 7
nop 1
dl 0
loc 32
rs 8.6506
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\Observer;
10
11
use Getnet\PaymentMagento\Gateway\Config\ConfigCc;
12
use Magento\Framework\Event\Observer;
13
use Magento\Payment\Observer\AbstractDataAssignObserver;
14
use Magento\Quote\Api\Data\PaymentInterface;
15
16
/**
17
 * Class Data Assign Observer Vault Cc - Capture vault payment information.
18
 */
19
class DataAssignObserverCcVault extends AbstractDataAssignObserver
20
{
21
    /**
22
     * @const string
23
     */
24
    public const PAYMENT_INFO_CC_CID = 'cc_cid';
25
26
    /**
27
     * @const string
28
     */
29
    public const PAYMENT_INFO_CC_INSTALLMENTS = 'cc_installments';
30
31
    /**
32
     * @const string
33
     */
34
    public const PAYMENT_INFO_NUMBER_TOKEN = 'cc_number_token';
35
36
    /**
37
     * @const string
38
     */
39
    public const PAYMENT_INFO_CC_NUMBER = 'cc_number';
40
41
    /**
42
     * @const string
43
     */
44
    public const PAYMENT_INFO_CC_TYPE = 'cc_type';
45
46
    /**
47
     * @const string
48
     */
49
    public const PAYMENT_INFO_CC_EXP_M = 'cc_exp_month';
50
51
    /**
52
     * @const string
53
     */
54
    public const PAYMENT_INFO_CC_EXP_Y = 'cc_exp_year';
55
56
    /**
57
     * @const string
58
     */
59
    public const PAYMENT_INFO_CARDHOLDER_NAME = 'cc_cardholder_name';
60
61
    /**
62
     * @var array
63
     */
64
    protected $addInformationList = [
65
        self::PAYMENT_INFO_NUMBER_TOKEN,
66
        self::PAYMENT_INFO_CARDHOLDER_NAME,
67
        self::PAYMENT_INFO_CC_NUMBER,
68
        self::PAYMENT_INFO_CC_TYPE,
69
        self::PAYMENT_INFO_CC_CID,
70
        self::PAYMENT_INFO_CC_EXP_M,
71
        self::PAYMENT_INFO_CC_EXP_Y,
72
        self::PAYMENT_INFO_CC_INSTALLMENTS,
73
    ];
74
75
    /**
76
     * @var ConfigCc
77
     */
78
    protected $config;
79
80
    /**
81
     * @param ConfigCc $config
82
     */
83
    public function __construct(
84
        ConfigCc $config
85
    ) {
86
        $this->config = $config;
87
    }
88
89
    /**
90
     * Execute.
91
     *
92
     * @param Observer $observer
93
     *
94
     * @return void
95
     */
96
    public function execute(Observer $observer)
97
    {
98
        $data = $this->readDataArgument($observer);
99
100
        $additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
101
102
        if (!is_array($additionalData)) {
103
            return;
104
        }
105
106
        $paymentInfo = $this->readPaymentModelArgument($observer);
107
108
        foreach ($this->addInformationList as $addInformationKey) {
109
            if (isset($additionalData[$addInformationKey])) {
110
                if ($addInformationKey === self::PAYMENT_INFO_CC_TYPE) {
111
                    $paymentInfo->setAdditionalInformation(
112
                        $addInformationKey,
113
                        $this->getFullTypeName($additionalData[$addInformationKey])
114
                    );
115
                    continue;
116
                }
117
                if ($addInformationKey === self::PAYMENT_INFO_CC_NUMBER) {
118
                    $paymentInfo->setAdditionalInformation(
119
                        $addInformationKey,
120
                        'xxxx xxxx xxxx '.$additionalData[$addInformationKey]
121
                    );
122
                    continue;
123
                }
124
                if ($additionalData[$addInformationKey]) {
125
                    $paymentInfo->setAdditionalInformation(
126
                        $addInformationKey,
127
                        $additionalData[$addInformationKey]
128
                    );
129
                }
130
            }
131
        }
132
    }
133
134
    /**
135
     * Get Name for Cc Type.
136
     *
137
     * @param string $type
138
     *
139
     * @return string
140
     */
141
    public function getFullTypeName(string $type): string
142
    {
143
        $mapper = $this->config->getCcTypesMapper();
144
        $type = array_search($type, $mapper);
145
146
        return  ucfirst($type);
147
    }
148
}
149