Passed
Push — master ( f4ea74...48fc4d )
by Bruno
01:51
created

Accept::execute()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 60

Duplication

Lines 60
Ratio 100 %

Importance

Changes 0
Metric Value
dl 60
loc 60
rs 8.5616
c 0
b 0
f 0
cc 5
nc 7
nop 0

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;
13
use Magento\Framework\App\Action\Context;
14
use Magento\Framework\App\CsrfAwareActionInterface;
15
use Magento\Framework\App\Request\InvalidRequestException;
16
use Magento\Framework\App\RequestInterface;
17
use Magento\Framework\Controller\Result\JsonFactory;
18
use Magento\Payment\Model\Method\Logger;
19
use Magento\Sales\Api\Data\OrderInterfaceFactory;
20
use Magento\Sales\Model\Order\CreditmemoFactory;
21
use Magento\Sales\Model\Order\Invoice;
22
use Magento\Sales\Model\Service\CreditmemoService;
23
use Magento\Store\Model\StoreManagerInterface;
24
use Moip\Magento2\Gateway\Config\Config;
25
26
/**
27
 * Class Refund - Receives communication for refunded payment.
28
 */
29 View Code Duplication
class Accept extends Action implements CsrfAwareActionInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
{
31
    /**
32
     * createCsrfValidationException.
33
     *
34
     * @param RequestInterface $request
35
     *
36
     * @return null
37
     */
38
    public function createCsrfValidationException(RequestInterface $request): InvalidRequestException
39
    {
40
        if ($request) {
41
            return null;
42
        }
43
    }
44
45
    /**
46
     * validateForCsrf.
47
     *
48
     * @param RequestInterface $request
49
     *
50
     * @return bool true
51
     */
52
    public function validateForCsrf(RequestInterface $request): bool
53
    {
54
        if ($request) {
55
            return true;
56
        }
57
    }
58
59
    /**
60
     * @var logger
61
     */
62
    protected $logger;
63
64
    /**
65
     * @var orderFactory
66
     */
67
    protected $orderFactory;
68
69
    /**
70
     * @var resultJsonFactory
71
     */
72
    protected $resultJsonFactory;
73
74
    /**
75
     * @var storeManager
76
     */
77
    protected $storeManager;
78
79
    /**
80
     * @param Context               $context
81
     * @param logger                $logger
82
     * @param Config                $config
83
     * @param OrderInterfaceFactory $orderFactory
84
     * @param JsonFactory           $resultJsonFactory
85
     */
86
    public function __construct(
87
        Context $context,
88
        Config $config,
89
        Logger $logger,
90
        OrderInterfaceFactory $orderFactory,
91
        CreditmemoFactory $creditmemoFactory,
92
        CreditmemoService $creditmemoService,
93
        Invoice $invoice,
94
        StoreManagerInterface $storeManager,
95
        JsonFactory $resultJsonFactory
96
    ) {
97
        parent::__construct($context);
98
        $this->config = $config;
99
        $this->logger = $logger;
0 ignored issues
show
Documentation Bug introduced by
It seems like $logger of type object<Magento\Payment\Model\Method\Logger> is incompatible with the declared type object<Moip\Magento2\Controller\Webhooks\logger> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100
        $this->orderFactory = $orderFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $orderFactory of type object<Magento\Sales\Api...\OrderInterfaceFactory> is incompatible with the declared type object<Moip\Magento2\Con...\Webhooks\orderFactory> of property $orderFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
101
        $this->creditmemoFactory = $creditmemoFactory;
102
        $this->creditmemoService = $creditmemoService;
103
        $this->invoice = $invoice;
104
        $this->storeManager = $storeManager;
0 ignored issues
show
Documentation Bug introduced by
It seems like $storeManager of type object<Magento\Store\Model\StoreManagerInterface> is incompatible with the declared type object<Moip\Magento2\Con...\Webhooks\storeManager> of property $storeManager.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105
        $this->resultJsonFactory = $resultJsonFactory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $resultJsonFactory of type object<Magento\Framework...ler\Result\JsonFactory> is incompatible with the declared type object<Moip\Magento2\Con...ooks\resultJsonFactory> of property $resultJsonFactory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
106
    }
107
108
    /**
109
     * Command Accept.
110
     *
111
     * @return json
112
     */
113
    public function execute()
114
    {
115
        if (!$this->getRequest()->isPost()) {
116
            $resultPage = $this->resultJsonFactory->create();
117
            $resultPage->setHttpResponseCode(404);
118
119
            return $resultPage;
120
        }
121
122
        $resultPage = $this->resultJsonFactory->create();
123
        $response = $this->getRequest()->getContent();
124
        $originalNotification = json_decode($response, true);
125
        $authorization = $this->getRequest()->getHeader('Authorization');
126
        $storeId = $this->storeManager->getStore()->getId();
127
        $storeCaptureToken = $this->config->getMerchantGatewayCaptureToken($storeId);
128
        if ($storeCaptureToken === $authorization) {
129
            $order = $this->orderFactory->create()->load($originalNotification['id'], 'ext_order_id');
130
            $this->logger->debug([
131
                'webhook'            => 'accept',
132
                'ext_order_id'       => $originalNotification['id'],
133
                'increment_order_id' => $order->getIncrementId(),
134
            ]);
135
            $payment = $order->getPayment();
136
            if (!$order->getInvoiceCollection()->count()) {
137
                try {
138
                    $payment->accept();
139
                    $payment->save();
140
                    $order->save();
141
                } catch (\Exception $exc) {
142
                    $resultPage->setHttpResponseCode(500);
143
                    $resultPage->setJsonData(
144
                        json_encode([
145
                            'error'   => 400,
146
                            'message' => $exc->getMessage(),
147
                        ])
148
                    );
149
                }
150
151
                return $resultPage->setJsonData(
152
                    json_encode([
153
                        'success'   => 1,
154
                        'status'    => $order->getStatus(),
155
                        'state'     => $order->getState(),
156
                    ])
157
                );
158
            }
159
160
            $resultPage->setHttpResponseCode(400);
161
162
            return $resultPage->setJsonData(
163
                json_encode([
164
                    'error'   => 400,
165
                    'message' => 'The transaction could not be refund',
166
                ])
167
            );
168
        }
169
        $resultPage->setHttpResponseCode(401);
170
171
        return $resultPage;
172
    }
173
}
174