Accept::execute()   B
last analyzed

Complexity

Conditions 6
Paths 8

Size

Total Lines 73
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 49
c 1
b 0
f 0
nc 8
nop 0
dl 0
loc 73
rs 8.4905

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
declare(strict_types=1);
9
10
namespace Moip\Magento2\Controller\Webhooks;
11
12
use Magento\Framework\App\Action\Action;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Action\Action 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\Framework\App\Action\Context;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Action\Context 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\Framework\App\CsrfAwareActionInterface as Csrf;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\CsrfAwareActionInterface 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\Framework\App\Request\InvalidRequestException;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Re...InvalidRequestException 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\Framework\App\RequestInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\RequestInterface 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\Framework\Controller\Result\JsonFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\Result\JsonFactory 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\Framework\Serialize\Serializer\Json;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Serialize\Serializer\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...
19
use Magento\Payment\Model\Method\Logger;
0 ignored issues
show
Bug introduced by
The type Magento\Payment\Model\Method\Logger 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\Sales\Api\Data\OrderInterfaceFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Api\Data\OrderInterfaceFactory 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\Sales\Model\Order\CreditmemoFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order\CreditmemoFactory 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\Sales\Model\Order\Invoice;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order\Invoice 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 Magento\Sales\Model\Service\CreditmemoService;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Service\CreditmemoService 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
use Magento\Store\Model\StoreManagerInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\StoreManagerInterface 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...
25
use Moip\Magento2\Gateway\Config\Config;
26
27
/**
28
 * Class Accept - Receives communication for accept payment.
29
 */
30
class Accept extends Action implements Csrf
31
{
32
    /**
33
     * createCsrfValidationException.
34
     *
35
     * @param RequestInterface $request
36
     *
37
     * @return null
38
     */
39
    public function createCsrfValidationException(RequestInterface $request): InvalidRequestException
40
    {
41
        if ($request) {
0 ignored issues
show
introduced by
$request is of type Magento\Framework\App\RequestInterface, thus it always evaluated to true.
Loading history...
42
            return null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return null returns the type null which is incompatible with the type-hinted return Magento\Framework\App\Re...InvalidRequestException.
Loading history...
43
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 41 is false. This is incompatible with the type-hinted return Magento\Framework\App\Re...InvalidRequestException. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
44
    }
45
46
    /**
47
     * validateForCsrf.
48
     *
49
     * @param RequestInterface $request
50
     *
51
     * @return bool true
52
     */
53
    public function validateForCsrf(RequestInterface $request): bool
54
    {
55
        if ($request) {
0 ignored issues
show
introduced by
$request is of type Magento\Framework\App\RequestInterface, thus it always evaluated to true.
Loading history...
56
            return true;
57
        }
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 55 is false. This is incompatible with the type-hinted return boolean. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
58
    }
59
60
    /**
61
     * @var logger
62
     */
63
    protected $logger;
64
65
    /**
66
     * @var orderFactory
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Controller\Webhooks\orderFactory 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...
67
     */
68
    protected $orderFactory;
69
70
    /**
71
     * @var resultJsonFactory
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Controller...hooks\resultJsonFactory 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...
72
     */
73
    protected $resultJsonFactory;
74
75
    /**
76
     * @var storeManager
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Controller\Webhooks\storeManager 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...
77
     */
78
    protected $storeManager;
79
80
    /**
81
     * @var Json
82
     */
83
    protected $json;
84
85
    /**
86
     * @param Context               $context
87
     * @param logger                $logger
88
     * @param Config                $config
89
     * @param OrderInterfaceFactory $orderFactory
90
     * @param JsonFactory           $resultJsonFactory
91
     * @param Json                  $json
92
     */
93
    public function __construct(
94
        Context $context,
95
        Config $config,
96
        Logger $logger,
97
        OrderInterfaceFactory $orderFactory,
98
        CreditmemoFactory $creditmemoFactory,
99
        CreditmemoService $creditmemoService,
100
        Invoice $invoice,
101
        StoreManagerInterface $storeManager,
102
        JsonFactory $resultJsonFactory,
103
        Json $json
104
    ) {
105
        parent::__construct($context);
106
        $this->config = $config;
0 ignored issues
show
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
107
        $this->logger = $logger;
108
        $this->orderFactory = $orderFactory;
109
        $this->creditmemoFactory = $creditmemoFactory;
0 ignored issues
show
Bug Best Practice introduced by
The property creditmemoFactory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
110
        $this->creditmemoService = $creditmemoService;
0 ignored issues
show
Bug Best Practice introduced by
The property creditmemoService does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
111
        $this->invoice = $invoice;
0 ignored issues
show
Bug Best Practice introduced by
The property invoice does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
112
        $this->storeManager = $storeManager;
113
        $this->resultJsonFactory = $resultJsonFactory;
114
        $this->json = $json;
115
    }
116
117
    /**
118
     * Command Accept.
119
     *
120
     * @return json
121
     */
122
    public function execute()
123
    {
124
        if (!$this->getRequest()->isPost()) {
125
            $resultPage = $this->resultJsonFactory->create();
126
            $resultPage->setHttpResponseCode(404);
127
128
            return $resultPage;
129
        }
130
131
        $resultPage = $this->resultJsonFactory->create();
132
        $response = $this->getRequest()->getContent();
133
        $originalNotification = $this->json->unserialize($response);
134
        $authorization = $this->getRequest()->getHeader('Authorization');
135
        $storeId = $this->storeManager->getStore()->getId();
136
        $storeCaptureToken = $this->config->getMerchantGatewayCaptureToken($storeId);
137
        if ($storeCaptureToken === $authorization) {
138
            $data = $originalNotification['resource']['order'];
139
            $order = $this->orderFactory->create()->load($data['id'], 'ext_order_id');
140
141
            if(!$order->getId()) {
142
                $resultPage->setHttpResponseCode(406);
143
                return $resultPage->setJsonData(
144
                    $this->json->serialize([
145
                        'error' => 400,
146
                        'message' => __('Can not find this order'),
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

146
                        'message' => /** @scrutinizer ignore-call */ __('Can not find this order'),
Loading history...
147
                    ])
148
                );
149
            }
150
151
            $this->logger->debug([
152
                'webhook'            => 'accept',
153
                'ext_order_id'       => $data['id'],
154
                'increment_order_id' => $order->getIncrementId(),
155
                'webhook_data'       => $response,
156
            ]);
157
            $payment = $order->getPayment();
158
            if (!$order->getInvoiceCollection()->count()) {
159
                try {
160
                    $isOnline = true;
161
                    $payment->accept($isOnline);
162
                    $payment->save();
163
                    $order->save();
164
                } catch (\Exception $exc) {
165
                    $resultPage->setHttpResponseCode(500);
166
                    return $resultPage->setJsonData(
167
                        $this->json->serialize([
168
                            'error'   => 400,
169
                            'message' => $exc->getMessage(),
170
                        ])
171
                    );
172
                }
173
174
                return $resultPage->setJsonData(
175
                    $this->json->serialize([
176
                        'success'   => 1,
177
                        'status'    => $order->getStatus(),
178
                        'state'     => $order->getState(),
179
                    ])
180
                );
181
            }
182
183
            $resultPage->setHttpResponseCode(400);
184
185
            return $resultPage->setJsonData(
186
                $this->json->serialize([
187
                    'error'   => 400,
188
                    'message' => 'The transaction could not be refund',
189
                ])
190
            );
191
        }
192
        $resultPage->setHttpResponseCode(401);
193
194
        return $resultPage;
195
    }
196
}
197