PaymentCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 18
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
declare(strict_types=1);
10
11
namespace Getnet\PaymentMagento\Gateway\Command;
12
13
use Magento\Payment\Gateway\Command\CommandException;
14
use Magento\Payment\Gateway\Command\CommandPoolInterface;
15
use Magento\Payment\Gateway\CommandInterface;
16
use Magento\Payment\Gateway\ErrorMapper\ErrorMessageMapperInterface;
17
use Magento\Payment\Gateway\Http\ClientInterface;
18
use Magento\Payment\Gateway\Http\TransferFactoryInterface;
19
use Magento\Payment\Gateway\Request\BuilderInterface;
20
use Magento\Payment\Gateway\Response\HandlerInterface;
21
use Magento\Payment\Gateway\Validator\ResultInterface;
22
use Magento\Payment\Gateway\Validator\ValidatorInterface;
23
use Psr\Log\LoggerInterface;
24
25
/**
26
 * Class PaymentCommand - Executes the payment creation command with the order creation call.
27
 */
28
class PaymentCommand implements CommandInterface
29
{
30
    /**
31
     * @var CommandPoolInterface
32
     */
33
    protected $commandPool;
34
35
    /**
36
     * @var BuilderInterface
37
     */
38
    protected $requestBuilder;
39
40
    /**
41
     * @var TransferFactoryInterface
42
     */
43
    protected $transferFactory;
44
45
    /**
46
     * @var ClientInterface
47
     */
48
    protected $client;
49
50
    /**
51
     * @var LoggerInterface
52
     */
53
    protected $logger;
54
55
    /**
56
     * @var HandlerInterface
57
     */
58
    protected $handler;
59
60
    /**
61
     * @var ValidatorInterface
62
     */
63
    protected $validator;
64
65
    /**
66
     * @var ErrorMessageMapperInterface
67
     */
68
    protected $errorMessageMapper;
69
70
    /**
71
     * @param CommandPoolInterface             $commandPool
72
     * @param BuilderInterface                 $requestBuilder
73
     * @param TransferFactoryInterface         $transferFactory
74
     * @param ClientInterface                  $client
75
     * @param LoggerInterface                  $logger
76
     * @param HandlerInterface                 $handler
77
     * @param ValidatorInterface               $validator
78
     * @param ErrorMessageMapperInterface|null $errorMessageMapper
79
     */
80
    public function __construct(
81
        CommandPoolInterface $commandPool,
82
        BuilderInterface $requestBuilder,
83
        TransferFactoryInterface $transferFactory,
84
        ClientInterface $client,
85
        LoggerInterface $logger,
86
        HandlerInterface $handler = null,
87
        ValidatorInterface $validator = null,
88
        ErrorMessageMapperInterface $errorMessageMapper = null
89
    ) {
90
        $this->commandPool = $commandPool;
91
        $this->requestBuilder = $requestBuilder;
92
        $this->transferFactory = $transferFactory;
93
        $this->client = $client;
94
        $this->handler = $handler;
95
        $this->validator = $validator;
96
        $this->logger = $logger;
97
        $this->errorMessageMapper = $errorMessageMapper;
98
    }
99
100
    /**
101
     * Execute.
102
     *
103
     * @param array $commandSubject
104
     *
105
     * @return void
106
     */
107
    public function execute(array $commandSubject)
108
    {
109
        $transferO = $this->transferFactory->create(
110
            $this->requestBuilder->build($commandSubject)
111
        );
112
113
        $response = $this->client->placeRequest($transferO);
114
        if ($this->validator !== null) {
115
            $result = $this->validator->validate(
116
                array_merge($commandSubject, ['response' => $response])
117
            );
118
            if (!$result->isValid()) {
119
                $this->processErrors($result);
120
            }
121
        }
122
123
        if ($this->handler) {
124
            $this->handler->handle(
125
                $commandSubject,
126
                $response
127
            );
128
        }
129
    }
130
131
    /**
132
     * Process Errors.
133
     *
134
     * @param ResultInterface $result
135
     */
136
    protected function processErrors(ResultInterface $result)
137
    {
138
        $messages = [];
139
        $errorsSource = array_merge($result->getErrorCodes(), $result->getFailsDescription());
140
        foreach ($errorsSource as $errorCodeOrMessage) {
141
            $errorCodeOrMessage = (string) $errorCodeOrMessage;
142
            if ($this->errorMessageMapper !== null) {
143
                $mapped = (string) $this->errorMessageMapper->getMessage($errorCodeOrMessage);
144
                if (!empty($mapped)) {
145
                    $messages[] = $mapped;
146
                    $errorCodeOrMessage = $mapped;
147
                }
148
            }
149
            $this->logger->critical('Payment Error: '.$errorCodeOrMessage);
150
        }
151
152
        throw new CommandException(
153
            !empty($messages)
154
                ? __(implode(PHP_EOL, $messages))
155
                : __('Transaction has been declined. Please try again later.')
156
        );
157
    }
158
}
159