Passed
Branch master (acde85)
by Alexander
01:40
created

Check::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 11
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
namespace B2Binpay\Payment\Controller\Adminhtml\System\Config;
4
5
use 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...
6
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...
7
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...
8
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...
9
use B2Binpay\Payment\Model\Adapter\B2BinpayAdapterFactory;
10
use B2Binpay\Exception\B2BinpayException;
0 ignored issues
show
Bug introduced by
The type B2Binpay\Exception\B2BinpayException 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...
11
12
/**
13
 * Class Check
14
 */
15
class Check extends Action
16
{
17
    const SECRET_MASK = '******';
18
19
    /**
20
     * @var \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...
21
     */
22
    private $request;
23
24
    /**
25
     * @var JsonFactory
26
     */
27
    private $resultJsonFactory;
28
29
    /**
30
     * @var StoreManagerInterface
31
     */
32
    private $storeManager;
33
34
    /**
35
     * @var B2BinpayAdapterFactory
36
     */
37
    protected $adapterFactory;
38
39
    /**
40
     * Auth constructor.
41
     *
42
     * @param Context $context
43
     * @param JsonFactory $resultJsonFactory
44
     * @param StoreManagerInterface $storeManager
45
     * @param B2BinpayAdapterFactory $adapterFactory
46
     */
47
    public function __construct(
48
        Context $context,
49
        JsonFactory $resultJsonFactory,
50
        StoreManagerInterface $storeManager,
51
        B2BinpayAdapterFactory $adapterFactory
52
    ) {
53
        $this->request = $context->getRequest();
54
        $this->storeManager = $storeManager;
55
        $this->resultJsonFactory = $resultJsonFactory;
56
        $this->adapterFactory = $adapterFactory;
57
        parent::__construct($context);
58
    }
59
60
    /**
61
     * Admin controller for auth params test
62
     *
63
     * - Check API Key/Secret
64
     * - Check Wallet ids
65
     *
66
     * @return \Magento\Framework\Controller\Result\Json
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\Result\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...
67
     * @throws \Magento\Framework\Exception\NoSuchEntityException
68
     */
69
    public function execute()
70
    {
71
        $result = $this->resultJsonFactory->create();
72
73
        $storeId = $this->storeManager->getStore()->getId();
74
        $authKey = $this->request->getParam('auth_key');
75
        $authSecret = $this->request->getParam('auth_secret');
76
        $wallets = $this->request->getParam('wallets');
77
        $testing = ('1' === $this->request->getParam('is_test'));
78
79
        if (empty($authKey)) {
80
            return $result->setData([
81
                'success' => false,
82
                'message' => __('You need to fill Auth API Key')
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

82
                'message' => /** @scrutinizer ignore-call */ __('You need to fill Auth API Key')
Loading history...
83
            ]);
84
        }
85
86
        if (empty($authSecret)) {
87
            return $result->setData([
88
                'success' => false,
89
                'message' => __('You need to fill Auth API Secret')
90
            ]);
91
        }
92
93
        if (empty($wallets)) {
94
            return $result->setData([
95
                'success' => false,
96
                'message' => __('You need to fill Wallets')
97
            ]);
98
        }
99
100
        if ($authSecret === $this::SECRET_MASK) {
101
            $authSecret = null;
102
        }
103
104
        $b2binpay = $this->adapterFactory->create(
105
            $storeId,
106
            $authKey,
107
            $authSecret,
108
            $testing
109
        );
110
111
        try {
112
            $b2binpay->getAuthToken();
113
        } catch (B2BinpayException $e) {
114
            return $result->setData([
115
                'success' => false,
116
                'message' => __('Wrong Auth API Key/Secret')
117
            ]);
118
        }
119
120
        foreach (explode('_', $wallets) as $wallet) {
121
            try {
122
                $b2binpay->getWallet($wallet);
123
            } catch (B2BinpayException $e) {
124
                return $result->setData([
125
                    'success' => false,
126
                    'message' => __('Wrong Wallet ID: ' . $wallet)
127
                ]);
128
            }
129
        }
130
131
        return $result->setData([
132
            'success' => true,
133
            'message' => __('Success')
134
        ]);
135
    }
136
137
    /**
138
     * @return bool
139
     */
140
    protected function _isAllowed()
141
    {
142
        return $this->_authorization->isAllowed('B2Binpay_Payment::config');
143
    }
144
}
145