AuthorizePaymentCommand::__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 © Wirecard Brasil. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Moip\Magento2\Gateway\Command;
12
13
use Magento\Payment\Gateway\Command\CommandException;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Command\CommandException 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\Gateway\Command\CommandPoolInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\...nd\CommandPoolInterface 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\Payment\Gateway\CommandInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\CommandInterface 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\Payment\Gateway\ErrorMapper\ErrorMessageMapperInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\...rMessageMapperInterface 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\Payment\Gateway\Http\ClientInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Http\ClientInterface 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\Payment\Gateway\Http\TransferFactoryInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\...ransferFactoryInterface 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\Payment\Gateway\Request\BuilderInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Request\BuilderInterface 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\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...
21
use Magento\Payment\Gateway\Validator\ResultInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\Validator\ResultInterface 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...
22
use Magento\Payment\Gateway\Validator\ValidatorInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Gateway\...ator\ValidatorInterface 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...
23
use Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface 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...
24
25
/**
26
 * Class AuthorizePaymentCommand - Executes the payment creation command with the order creation call.
27
 */
28
class AuthorizePaymentCommand implements CommandInterface
29
{
30
    private const CREATE_ORDER_MOIP = 'create_order_moip';
31
32
    /**
33
     * @var CommandPoolInterface
34
     */
35
    private $commandPool;
36
37
    /**
38
     * @var BuilderInterface
39
     */
40
    private $requestBuilder;
41
42
    /**
43
     * @var TransferFactoryInterface
44
     */
45
    private $transferFactory;
46
47
    /**
48
     * @var ClientInterface
49
     */
50
    private $client;
51
52
    /**
53
     * @var LoggerInterface
54
     */
55
    private $logger;
56
57
    /**
58
     * @var HandlerInterface
59
     */
60
    private $handler;
61
62
    /**
63
     * @var ValidatorInterface
64
     */
65
    private $validator;
66
67
    /**
68
     * @var ErrorMessageMapperInterface
69
     */
70
    private $errorMessageMapper;
71
72
    /**
73
     * @param CommandPoolInterface             $commandPool
74
     * @param BuilderInterface                 $requestBuilder
75
     * @param TransferFactoryInterface         $transferFactory
76
     * @param ClientInterface                  $client
77
     * @param LoggerInterface                  $logger
78
     * @param HandlerInterface                 $handler
79
     * @param ValidatorInterface               $validator
80
     * @param ErrorMessageMapperInterface|null $errorMessageMapper
81
     */
82
    public function __construct(
83
        CommandPoolInterface $commandPool,
84
        BuilderInterface $requestBuilder,
85
        TransferFactoryInterface $transferFactory,
86
        ClientInterface $client,
87
        LoggerInterface $logger,
88
        HandlerInterface $handler = null,
89
        ValidatorInterface $validator = null,
90
        ErrorMessageMapperInterface $errorMessageMapper = null
91
    ) {
92
        $this->commandPool = $commandPool;
93
        $this->requestBuilder = $requestBuilder;
94
        $this->transferFactory = $transferFactory;
95
        $this->client = $client;
96
        $this->handler = $handler;
97
        $this->validator = $validator;
98
        $this->logger = $logger;
99
        $this->errorMessageMapper = $errorMessageMapper;
100
    }
101
102
    /**
103
     * Execute.
104
     *
105
     * @param array $commandSubject
106
     *
107
     * @return void
108
     */
109
    public function execute(array $commandSubject)
110
    {
111
        $this->createOrderMoip($commandSubject);
112
        $transferO = $this->transferFactory->create(
113
            $this->requestBuilder->build($commandSubject)
114
        );
115
116
        $response = $this->client->placeRequest($transferO);
117
        if ($this->validator !== null) {
118
            $result = $this->validator->validate(
119
                array_merge($commandSubject, ['response' => $response])
120
            );
121
            if (!$result->isValid()) {
122
                $this->processErrors($result);
123
            }
124
        }
125
126
        if ($this->handler) {
127
            $this->handler->handle(
128
                $commandSubject,
129
                $response
130
            );
131
        }
132
    }
133
134
    /**
135
     * Process Errors.
136
     *
137
     * @param ResultInterface $result
138
     */
139
    private function processErrors(ResultInterface $result)
140
    {
141
        $messages = [];
142
        $errorsSource = array_merge($result->getErrorCodes(), $result->getFailsDescription());
143
        foreach ($errorsSource as $errorCodeOrMessage) {
144
            $errorCodeOrMessage = (string) $errorCodeOrMessage;
145
            if ($this->errorMessageMapper !== null) {
146
                $mapped = (string) $this->errorMessageMapper->getMessage($errorCodeOrMessage);
147
                if (!empty($mapped)) {
148
                    $messages[] = $mapped;
149
                    $errorCodeOrMessage = $mapped;
150
                }
151
            }
152
            $this->logger->critical('Payment Error: '.$errorCodeOrMessage);
153
        }
154
155
        throw new CommandException(
156
            !empty($messages)
157
                ? __(implode(PHP_EOL, $messages))
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
                ? /** @scrutinizer ignore-call */ __(implode(PHP_EOL, $messages))
Loading history...
158
                : __('Transaction has been declined. Please try again later.')
159
        );
160
    }
161
162
    /**
163
     * Create Order.
164
     *
165
     * @param array $commandSubject
166
     *
167
     * @return bool
168
     */
169
    private function createOrderMoip(array $commandSubject): bool
170
    {
171
        $response = $this->commandPool->get(self::CREATE_ORDER_MOIP)
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
172
            ->execute($commandSubject);
173
174
        return true;
175
    }
176
}
177