VaultDetailsHandler::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
c 1
b 0
f 0
nc 2
nop 5
dl 0
loc 16
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
9
namespace Getnet\PaymentMagento\Gateway\Response;
10
11
use Getnet\PaymentMagento\Gateway\Config\ConfigCc;
12
use InvalidArgumentException;
13
use Magento\Framework\ObjectManagerInterface;
14
use Magento\Framework\Serialize\Serializer\Json;
15
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
16
use Magento\Payment\Gateway\Response\HandlerInterface;
17
use Magento\Payment\Model\InfoInterface;
18
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...
19
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...
20
use Magento\Vault\Api\Data\PaymentTokenFactoryInterface;
21
use Magento\Vault\Api\Data\PaymentTokenInterface;
22
use Magento\Vault\Api\Data\PaymentTokenInterfaceFactory;
23
24
/**
25
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26
 */
27
class VaultDetailsHandler implements HandlerInterface
28
{
29
    /**
30
     * Response Pay Credit - Block name.
31
     */
32
    public const CREDIT = 'credit';
33
34
    /**
35
     * Response Brand - Block name.
36
     */
37
    public const BRAND = 'brand';
38
    /**
39
     * @var PaymentTokenInterfaceFactory
40
     */
41
    protected $paymentTokenFactory;
42
43
    /**
44
     * @var OrderPaymentExtensionInterfaceFactory
45
     */
46
    protected $payExtensionFactory;
47
48
    /**
49
     * @var ObjectManagerInterface
50
     */
51
    private $objectManager;
52
53
    /**
54
     * @var Json
55
     */
56
    private $json;
57
58
    /**
59
     * @var ConfigCc
60
     */
61
    private $configCc;
62
63
    /**
64
     * @param Json                                  $json
65
     * @param ObjectManagerInterface                $objectManager
66
     * @param OrderPaymentExtensionInterfaceFactory $payExtensionFactory
67
     * @param ConfigCc                              $configCc
68
     * @param PaymentTokenFactoryInterface          $paymentTokenFactory
69
     */
70
    public function __construct(
71
        Json $json,
72
        ObjectManagerInterface $objectManager,
73
        OrderPaymentExtensionInterfaceFactory $payExtensionFactory,
74
        ConfigCc $configCc,
75
        PaymentTokenFactoryInterface $paymentTokenFactory = null
76
    ) {
77
        if ($paymentTokenFactory === null) {
78
            $paymentTokenFactory = $objectManager->get(PaymentTokenFactoryInterface::class);
79
        }
80
81
        $this->objectManager = $objectManager;
82
        $this->payExtensionFactory = $payExtensionFactory;
83
        $this->paymentTokenFactory = $paymentTokenFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $paymentTokenFactory can also be of type Magento\Vault\Api\Data\P...ntTokenFactoryInterface. However, the property $paymentTokenFactory is declared as type Magento\Vault\Api\Data\P...ntTokenInterfaceFactory. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
84
        $this->json = $json;
85
        $this->configCc = $configCc;
86
    }
87
88
    /**
89
     * Handle.
90
     *
91
     * @param array $handlingSubject
92
     * @param array $response
93
     *
94
     * @return void
95
     */
96
    public function handle(array $handlingSubject, array $response)
97
    {
98
        if (!isset($handlingSubject['payment'])
99
            || !$handlingSubject['payment'] instanceof PaymentDataObjectInterface
100
        ) {
101
            throw new InvalidArgumentException('Payment data object should be provided');
102
        }
103
104
        $paymentDO = $handlingSubject['payment'];
105
106
        $payment = $paymentDO->getPayment();
107
        $paymentToken = $this->getVaultPaymentToken($payment, $response);
108
        if (null !== $paymentToken) {
109
            $extensionAttributes = $this->getExtensionAttributes($payment);
110
            $extensionAttributes->setVaultPaymentToken($paymentToken);
111
        }
112
    }
113
114
    /**
115
     * Get vault payment token entity.
116
     *
117
     * @param InfoInterface $payment
118
     * @param array         $response
119
     *
120
     * @return PaymentTokenInterface|null
121
     */
122
    protected function getVaultPaymentToken($payment, $response)
123
    {
124
        $ccNumberToken = $payment->getAdditionalInformation('cc_public_id');
125
        $ccLast4 = $payment->getAdditionalInformation('cc_number');
126
        $ccType = $payment->getAdditionalInformation('cc_type');
127
        $ccExpMonth = $payment->getAdditionalInformation('cc_exp_month');
128
129
        $ccExpYear = $payment->getAdditionalInformation('cc_exp_year');
130
        if (empty($ccNumberToken)) {
131
            return null;
132
        }
133
134
        $ccType = $this->mapperCcType($ccType);
135
        if (!$ccType) {
136
            $ccType = $response[self::CREDIT][self::BRAND];
137
        }
138
139
        $ccLast4 = preg_replace('/[^0-9]/', '', $ccLast4);
140
        $paymentToken = $this->paymentTokenFactory->create();
141
        $paymentToken->setGatewayToken($ccNumberToken);
142
        $paymentToken->setExpiresAt(strtotime('+1 year'));
143
        $paymentToken->setType(PaymentTokenFactoryInterface::TOKEN_TYPE_CREDIT_CARD);
144
        // phpcs:ignore Generic.Files.LineLength
145
        $details = ['cc_last4' => $ccLast4, 'cc_exp_year' => $ccExpYear, 'cc_exp_month' => $ccExpMonth, 'cc_type' => $ccType];
146
        $paymentToken->setTokenDetails($this->json->serialize($details));
147
148
        return $paymentToken;
149
    }
150
151
    /**
152
     * Get payment extension attributes.
153
     *
154
     * @param InfoInterface $payment
155
     *
156
     * @return OrderPaymentExtensionInterface
157
     */
158
    private function getExtensionAttributes(InfoInterface $payment): OrderPaymentExtensionInterface
159
    {
160
        $extensionAttributes = $payment->getExtensionAttributes();
161
        if (null === $extensionAttributes) {
162
            $extensionAttributes = $this->payExtensionFactory->create();
163
            $payment->setExtensionAttributes($extensionAttributes);
164
        }
165
166
        return $extensionAttributes;
167
    }
168
169
    /**
170
     * Get Type Cc by response payment.
171
     *
172
     * @param string $type
173
     *
174
     * @return string
175
     */
176
    public function mapperCcType($type)
177
    {
178
        $type = ucfirst($type);
179
        $mapper = $this->configCc->getCcTypesMapper();
180
181
        return $mapper[$type];
182
    }
183
}
184