Preference::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 2
b 0
f 0
nc 1
nop 9
dl 0
loc 20
rs 9.9666

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\Controller\Adminhtml\System\Config;
12
13
use Magento\Backend\App\Action\Context;
0 ignored issues
show
Bug introduced by
The type Magento\Backend\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\Config\Model\ResourceModel\Config;
0 ignored issues
show
Bug introduced by
The type Magento\Config\Model\ResourceModel\Config 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\Cache\Frontend\Pool;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Cache\Frontend\Pool 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\Cache\TypeListInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Cache\TypeListInterface 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\App\Config\ConfigResource\ConfigInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Co...esource\ConfigInterface 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\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...
19
use Magento\Framework\Controller\ResultFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\ResultFactory 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\Framework\HTTP\ZendClient;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\HTTP\ZendClient 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\Framework\HTTP\ZendClientFactory;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\HTTP\ZendClientFactory 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\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...
23
use Moip\Magento2\Gateway\Config\Config as ConfigBase;
24
25
/*
26
 * Class Preferenc
27
 */
28
class Preference extends \Magento\Backend\App\Action
0 ignored issues
show
Bug introduced by
The type Magento\Backend\App\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...
29
{
30
    /*
31
     * @var cacheTypeList
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Controller...em\Config\cacheTypeList 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...
32
     */
33
    protected $cacheTypeList;
34
35
    /*
36
     * @var cacheFrontendPool
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Controller...onfig\cacheFrontendPool 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...
37
     */
38
    protected $cacheFrontendPool;
39
40
    /*
41
     * @var resultJsonFactory
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Controller...onfig\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...
42
     */
43
    protected $resultJsonFactory;
44
45
    /*
46
     * @var configInterface
47
     */
48
    protected $configInterface;
49
50
    /*
51
     * @var configBase
52
     */
53
    protected $configBase;
54
55
    /*
56
     * @var resourceConfig
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Controller...m\Config\resourceConfig 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...
57
     */
58
    protected $resourceConfig;
59
60
    /*
61
     * @var storeManager
0 ignored issues
show
Bug introduced by
The type Moip\Magento2\Controller...tem\Config\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...
62
     */
63
    protected $storeManager;
64
65
    /*
66
     * @param Context
67
     * @param TypeListInterface
68
     * @param Pool
69
     * @param JsonFactory
70
     * @param ConfigInterface
71
     * @param Config
72
     * @param ConfigBase
73
     * @param StoreManagerInterface
74
     */
75
    public function __construct(
76
        Context $context,
77
        TypeListInterface $cacheTypeList,
78
        Pool $cacheFrontendPool,
79
        JsonFactory $resultJsonFactory,
80
        ConfigInterface $configInterface,
81
        Config $resourceConfig,
82
        ConfigBase $configBase,
83
        StoreManagerInterface $storeManager,
84
        ZendClientFactory $httpClientFactory
85
    ) {
86
        $this->cacheTypeList = $cacheTypeList;
87
        $this->cacheFrontendPool = $cacheFrontendPool;
88
        $this->resultJsonFactory = $resultJsonFactory;
89
        $this->configInterface = $configInterface;
90
        $this->resourceConfig = $resourceConfig;
91
        $this->configBase = $configBase;
92
        $this->storeManager = $storeManager;
93
        $this->httpClientFactory = $httpClientFactory;
0 ignored issues
show
Bug Best Practice introduced by
The property httpClientFactory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
94
        parent::__construct($context);
95
    }
96
97
    /*
98
     * Is Allowed
99
     *
100
     * @return boolean
101
     */
102
    protected function _isAllowed()
103
    {
104
        return $this->_authorization->isAllowed('Moip_Magento2::preference');
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function execute()
111
    {
112
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
113
114
        $captureUrl = $this->getUrlCapture();
115
        $webhookCapture = $this->setUrlWebhookCapture($captureUrl);
116
        if (isset($webhookCapture['code'])) {
117
            if ($webhookCapture['code'] === 'API-009') {
118
                $this->messageManager->addError(
119
                    __('Your module is authorized, but you have reached the notification url preference limit.')
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

119
                    /** @scrutinizer ignore-call */ 
120
                    __('Your module is authorized, but you have reached the notification url preference limit.')
Loading history...
120
                );
121
                $this->cacheTypeList->cleanType('config');
122
                $resultRedirect->setUrl($this->getUrlConfig());
123
124
                return $resultRedirect;
125
            }
126
        }
127
        $this->setUrlInfoCapture($webhookCapture);
128
129
        $cancelUrl = $this->getUrlCancel();
130
        $webhookCancel = $this->setUrlWebhookCancel($cancelUrl);
131
        if (isset($webhookCancel['code'])) {
132
            if ($webhookCancel['code'] === 'API-009') {
133
                $this->messageManager->addError(
134
                    __('Your module is authorized, but you have reached the notification url preference limit.')
135
                );
136
                $this->cacheTypeList->cleanType('config');
137
                $resultRedirect->setUrl($this->getUrlConfig());
138
139
                return $resultRedirect;
140
            }
141
        }
142
143
        $this->setUrlInfoCancel($webhookCancel);
144
145
        $refundUrl = $this->getUrlRefund();
146
        $webhookRefund = $this->setUrlWebhookRefund($refundUrl);
147
        if (isset($webhookRefund['code'])) {
148
            if ($webhookRefund['code'] === 'API-009') {
149
                $this->messageManager->addError(
150
                    __('Your module is authorized, but you have reached the notification url preference limit.')
151
                );
152
                $this->cacheTypeList->cleanType('config');
153
                $resultRedirect->setUrl($this->getUrlConfig());
154
155
                return $resultRedirect;
156
            }
157
        }
158
        $this->setUrlInfoRefund($webhookRefund);
159
160
        $this->messageManager->addSuccess(__('Your module is authorized. =)'));
161
        $this->cacheTypeList->cleanType('config');
162
        $resultRedirect->setUrl($this->getUrlConfig());
163
164
        return $resultRedirect;
165
    }
166
167
    /*
168
     * Url Config
169
     *
170
     * @return string
171
     */
172
    private function getUrlConfig()
173
    {
174
        return $this->getUrl('adminhtml/system_config/edit/section/payment/');
175
    }
176
177
    /*
178
     * Url Capture
179
     *
180
     * @return string
181
     */
182
    private function getUrlCapture()
183
    {
184
        $storeId = $this->storeManager->getDefaultStoreView()->getStoreId();
185
186
        return $this->storeManager->getStore($storeId)->getUrl('moip/webhooks/accept');
187
    }
188
189
    /*
190
     * Url Cancel
191
     *
192
     * @return string
193
     */
194
    private function getUrlCancel()
195
    {
196
        $storeId = $this->storeManager->getDefaultStoreView()->getStoreId();
197
198
        return $this->storeManager->getStore($storeId)->getUrl('moip/webhooks/deny');
199
    }
200
201
    /*
202
     * Url Refund
203
     *
204
     * @return string
205
     */
206
    private function getUrlRefund()
207
    {
208
        $storeId = $this->storeManager->getDefaultStoreView()->getStoreId();
209
210
        return $this->storeManager->getStore($storeId)->getUrl('moip/webhooks/refund');
211
    }
212
213
    /*
214
     * Set Url Info Refund
215
     */
216
    private function setUrlInfoRefund($webhook)
217
    {
218
        $environment = $this->configBase->getEnvironmentMode();
219
220
        $this->resourceConfig->saveConfig(
221
            'payment/moip_magento2/refund_id_'.$environment,
222
            $webhook['id'],
223
            'default',
224
            0
225
        );
226
227
        $this->resourceConfig->saveConfig(
228
            'payment/moip_magento2/refund_token_'.$environment,
229
            $webhook['token'],
230
            'default',
231
            0
232
        );
233
234
        return $this;
235
    }
236
237
    /*
238
     * Set Url Info Cancel
239
     */
240
    private function setUrlInfoCancel($webhook)
241
    {
242
        $environment = $this->configBase->getEnvironmentMode();
243
244
        $this->resourceConfig->saveConfig(
245
            'payment/moip_magento2/cancel_id_'.$environment,
246
            $webhook['id'],
247
            'default',
248
            0
249
        );
250
251
        $this->resourceConfig->saveConfig(
252
            'payment/moip_magento2/cancel_token_'.$environment,
253
            $webhook['token'],
254
            'default',
255
            0
256
        );
257
258
        return $this;
259
    }
260
261
    /*
262
     * Set Url Info Capture
263
     */
264
    private function setUrlInfoCapture($webhook)
265
    {
266
        $environment = $this->configBase->getEnvironmentMode();
267
268
        $this->resourceConfig->saveConfig(
269
            'payment/moip_magento2/capture_id_'.$environment,
270
            $webhook['id'],
271
            'default',
272
            0
273
        );
274
275
        $this->resourceConfig->saveConfig(
276
            'payment/moip_magento2/capture_token_'.$environment,
277
            $webhook['token'],
278
            'default',
279
            0
280
        );
281
282
        return $this;
283
    }
284
285
    /*
286
     * Set Url Webhook Refund
287
     */
288
    private function setUrlWebhookRefund($url)
289
    {
290
        $webhook = [
291
            'events' => ['REFUND.COMPLETED', 'REFUND.FAILED'],
292
            'target' => $url,
293
            'media'  => 'WEBHOOK',
294
        ];
295
296
        return $this->setWebhooks($webhook);
297
    }
298
299
    /*
300
     * Set Url Webhook Cancel
301
     */
302
    private function setUrlWebhookCancel($url)
303
    {
304
        $webhook = [
305
            'events' => ['ORDER.NOT_PAID'],
306
            'target' => $url,
307
            'media'  => 'WEBHOOK',
308
        ];
309
310
        return $this->setWebhooks($webhook);
311
    }
312
313
    /*
314
     * Set Url Webhook Capture
315
     */
316
    private function setUrlWebhookCapture($url)
317
    {
318
        $webhook = [
319
            'events' => ['ORDER.PAID'],
320
            'target' => $url,
321
            'media'  => 'WEBHOOK',
322
        ];
323
324
        return $this->setWebhooks($webhook);
325
    }
326
327
    /*
328
     * Set Webhooks
329
     *
330
     * @param $webhook
331
     *
332
     * @return array
333
     */
334
    private function setWebhooks($webhook)
335
    {
336
        $params = $this->getRequest()->getParams();
337
        $url = ConfigBase::ENDPOINT_PREFERENCES_PRODUCTION;
338
        $environment = $this->configBase->getEnvironmentMode();
339
        if ($environment === ConfigBase::ENVIRONMENT_SANDBOX) {
340
            $url = ConfigBase::ENDPOINT_PREFERENCES_SANDBOX;
341
        }
342
        $apiBearer = $params['oauth'];
343
344
        $client = $this->httpClientFactory->create();
345
346
        $client->setUri($url);
347
        $client->setConfig(['maxredirects' => 0, 'timeout' => 30]);
348
        $client->setHeaders('Authorization', 'Bearer '.$apiBearer);
349
        $client->setRawData(json_encode($webhook), 'application/json');
350
        $client->setMethod(ZendClient::POST);
351
352
        $result = $client->request()->getBody();
353
354
        return json_decode($result, true);
355
    }
356
}
357