VaultDetailsHandler   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 64
c 1
b 0
f 0
dl 0
loc 153
rs 10
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
B mapperCcType() 0 16 8
A getVaultPaymentToken() 0 31 4
A handle() 0 21 5
A getExtensionAttributes() 0 9 2
A __construct() 0 12 2
1
<?php
2
/**
3
 * Copyright © Wirecard Brasil. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
namespace Moip\Magento2\Gateway\Response;
10
11
use Magento\Framework\ObjectManagerInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\ObjectManagerInterface 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...
12
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\...mentDataObjectInterface 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 Magento\Payment\Gateway\Response\HandlerInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Response\HandlerInterface 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...
14
use Magento\Payment\Model\InfoInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Model\InfoInterface 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...
15
use Magento\Sales\Api\Data\OrderPaymentExtensionInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Api\Data\O...ymentExtensionInterface 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...
16
use Magento\Sales\Api\Data\OrderPaymentExtensionInterfaceFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Api\Data\O...tensionInterfaceFactory 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...
17
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Vault\Api\Data\P...ntTokenFactoryInterface 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...
18
use Magento\Vault\Api\Data\PaymentTokenInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Vault\Api\Data\PaymentTokenInterface 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...
19
use Magento\Vault\Api\Data\PaymentTokenInterfaceFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Vault\Api\Data\P...ntTokenInterfaceFactory 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...
20
21
/**
22
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
23
 */
24
class VaultDetailsHandler implements HandlerInterface
25
{
26
    /**
27
     * @var PaymentTokenInterfaceFactory
28
     */
29
    protected $paymentTokenFactory;
30
31
    /**
32
     * @var OrderPaymentExtensionInterfaceFactory
33
     */
34
    protected $payExtensionFactory;
35
36
    /**
37
     * @var SubjectReader
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Gateway\Response\SubjectReader 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...
38
     */
39
    protected $subjectReader;
40
41
    /**
42
     * @var ObjectManagerInterface
43
     */
44
    private $objectManager;
45
46
    /**
47
     * @var Json
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Gateway\Response\Json 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...
48
     */
49
    private $serializer;
0 ignored issues
show
introduced by
The private property $serializer is not used, and could be removed.
Loading history...
50
51
    /**
52
     * AccountPaymentTokenFactory constructor.
53
     *
54
     * @param ObjectManagerInterface       $objectManager
55
     * @param PaymentTokenFactoryInterface $paymentTokenFactory
56
     */
57
    public function __construct(
58
        ObjectManagerInterface $objectManager,
59
        OrderPaymentExtensionInterfaceFactory $payExtensionFactory,
60
        PaymentTokenFactoryInterface $paymentTokenFactory = null
61
    ) {
62
        if ($paymentTokenFactory === null) {
63
            $paymentTokenFactory = $objectManager->get(PaymentTokenFactoryInterface::class);
64
        }
65
66
        $this->objectManager = $objectManager;
67
        $this->payExtensionFactory = $payExtensionFactory;
68
        $this->paymentTokenFactory = $paymentTokenFactory;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function handle(array $handlingSubject, array $response)
75
    {
76
        if (!isset($handlingSubject['payment'])
77
            || !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
78
        ) {
79
            throw new \InvalidArgumentException('Payment data object should be provided');
80
        }
81
82
        $paymentDO = $handlingSubject['payment'];
83
84
        $payment = $paymentDO->getPayment();
85
86
        if ($payment->getMethod() === 'moip_magento2_cc') {
87
            $paymentToken = $this->getVaultPaymentToken($response, $payment);
88
            if (null !== $paymentToken) {
89
                $extensionAttributes = $this->getExtensionAttributes($payment);
90
                $extensionAttributes->setVaultPaymentToken($paymentToken);
91
            }
92
            $payment->unsAdditionalInformation('cc_hash');
93
            $payment->unsAdditionalInformation('cc_exp_month');
94
            $payment->unsAdditionalInformation('cc_exp_year');
95
        }
96
    }
97
98
    /**
99
     * Get vault payment token entity.
100
     *
101
     * @param $response
102
     *
103
     * @return PaymentTokenInterface|null
104
     */
105
    protected function getVaultPaymentToken($response, $payment)
106
    {
107
        $paymentAddtional = $response['fundingInstrument'];
108
        $ccExpYear = $payment->getAdditionalInformation('cc_exp_year');
109
        $ccExpMonth = $payment->getAdditionalInformation('cc_exp_month');
110
        $payment->unsAdditionalInformation('cc_exp_year');
111
        $payment->unsAdditionalInformation('cc_exp_month');
112
113
        if (isset($paymentAddtional['creditCard'])) {
114
            if (isset($paymentAddtional['creditCard']['id'])) {
115
                $ccId = $paymentAddtional['creditCard']['id'];
116
                $ccType = $this->mapperCcType($paymentAddtional['creditCard']['brand']);
117
                $ccLast4 = $paymentAddtional['creditCard']['last4'];
118
            }
119
        } else {
120
            throw new \InvalidArgumentException('Payment data object should be provided');
121
        }
122
123
        if (empty($ccId)) {
124
            return null;
125
        }
126
127
        $paymentToken = $this->paymentTokenFactory->create();
128
        $paymentToken->setGatewayToken($ccId);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ccId does not seem to be defined for all execution paths leading up to this point.
Loading history...
129
        $paymentToken->setExpiresAt(strtotime('+1 year'));
130
        $paymentToken->setType(PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD);
131
        // phpcs:ignore Generic.Files.LineLength
132
        $details = ['cc_last4' => $ccLast4, 'cc_exp_year' => $ccExpYear, 'cc_exp_month' => $ccExpMonth, 'cc_type' => $ccType];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ccType does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $ccLast4 does not seem to be defined for all execution paths leading up to this point.
Loading history...
133
        $paymentToken->setTokenDetails(json_encode($details));
134
135
        return $paymentToken;
136
    }
137
138
    /**
139
     * Get payment extension attributes.
140
     *
141
     * @param InfoInterface $payment
142
     *
143
     * @return OrderPaymentExtensionInterface
144
     */
145
    private function getExtensionAttributes(InfoInterface $payment): OrderPaymentExtensionInterface
146
    {
147
        $extensionAttributes = $payment->getExtensionAttributes();
148
        if (null === $extensionAttributes) {
149
            $extensionAttributes = $this->payExtensionFactory->create();
150
            $payment->setExtensionAttributes($extensionAttributes);
151
        }
152
153
        return $extensionAttributes;
154
    }
155
156
    /**
157
     * Get Type Cc by response payment.
158
     *
159
     * @param string $type
160
     */
161
    public function mapperCcType($type)
162
    {
163
        if ($type === 'MASTERCARD') {
164
            return 'MC';
165
        } elseif ($type === 'VISA') {
166
            return 'VI';
167
        } elseif ($type === 'AMEX') {
168
            return 'AE';
169
        } elseif ($type === 'DINERS') {
170
            return 'DN';
171
        } elseif ($type === 'HIPERCARD') {
172
            return 'HC';
173
        } elseif ($type === 'HIPER') {
174
            return 'HI';
175
        } elseif ($type === 'ELO') {
176
            return 'ELO';
177
        }
178
    }
179
}
180