Passed
Pull Request — master (#600)
by
unknown
03:42
created

Index   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 15
eloc 50
c 2
b 0
f 0
dl 0
loc 197
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 8 1
A getParam() 0 3 1
A __construct() 0 21 1
A getPostArray() 0 3 1
A logTransactionStatus() 0 3 1
B handleTransactionStatus() 0 40 10
1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2016 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Controller\Transactionstatus;
28
29
use Magento\Sales\Model\Order;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Order 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...
30
31
/**
32
 * TransactionStatus receiver
33
 */
34
class Index extends \Payone\Core\Controller\ExternalAction
35
{
36
    /**
37
     * @var array
38
     */
39
    protected $aFirstContactWhitelist = [
40
        'appointed',
41
        'failed',
42
    ];
43
44
    /**
45
     * TransactionStatus model
46
     *
47
     * @var \Payone\Core\Model\ResourceModel\TransactionStatus
48
     */
49
    protected $transactionStatus;
50
51
    /**
52
     * PAYONE toolkit helper
53
     *
54
     * @var \Payone\Core\Helper\Toolkit
55
     */
56
    protected $toolkitHelper;
57
58
    /**
59
     * PAYONE environment helper
60
     *
61
     * @var \Payone\Core\Helper\Environment
62
     */
63
    protected $environmentHelper;
64
65
    /**
66
     * PAYONE order helper
67
     *
68
     * @var \Payone\Core\Helper\Order
69
     */
70
    protected $orderHelper;
71
72
    /**
73
     * TransactionStatus handler
74
     *
75
     * @var \Payone\Core\Model\Handler\TransactionStatus
76
     */
77
    protected $transactionStatusHandler;
78
79
    /**
80
     * Result factory for file-download
81
     *
82
     * @var \Magento\Framework\Controller\Result\RawFactory
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\Result\RawFactory 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...
83
     */
84
    protected $resultRawFactory;
85
86
    /**
87
     * PAYONE substitute order handler
88
     *
89
     * @var \Payone\Core\Model\Handler\SubstituteOrder
90
     */
91
    protected $substituteOrder;
92
93
    /**
94
     * CacheInterface object
95
     *
96
     * @var \Magento\Framework\App\CacheInterface
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\CacheInterface 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...
97
     */
98
    protected $cache;
99
100
    /**
101
     * Constructor
102
     *
103
     * @param \Magento\Framework\App\Action\Context              $context
104
     * @param \Magento\Framework\Data\Form\FormKey               $formKey
105
     * @param \Payone\Core\Model\ResourceModel\TransactionStatus $transactionStatus
106
     * @param \Payone\Core\Helper\Toolkit                        $toolkitHelper
107
     * @param \Payone\Core\Helper\Environment                    $environmentHelper
108
     * @param \Payone\Core\Helper\Order                          $orderHelper
109
     * @param \Payone\Core\Model\Handler\TransactionStatus       $transactionStatusHandler,
110
     * @param \Magento\Framework\Controller\Result\RawFactory    $resultRawFactory
111
     * @param \Payone\Core\Model\Handler\SubstituteOrder         $substituteOrder
112
     * @param \Magento\Framework\App\CacheInterface              $cache
113
     */
114
    public function __construct(
115
        \Magento\Framework\App\Action\Context $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...
116
        \Magento\Framework\Data\Form\FormKey $formKey,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Data\Form\FormKey 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...
117
        \Payone\Core\Model\ResourceModel\TransactionStatus $transactionStatus,
118
        \Payone\Core\Helper\Toolkit $toolkitHelper,
119
        \Payone\Core\Helper\Environment $environmentHelper,
120
        \Payone\Core\Helper\Order $orderHelper,
121
        \Payone\Core\Model\Handler\TransactionStatus $transactionStatusHandler,
122
        \Magento\Framework\Controller\Result\RawFactory $resultRawFactory,
123
        \Payone\Core\Model\Handler\SubstituteOrder $substituteOrder,
124
        \Magento\Framework\App\CacheInterface $cache
125
    ) {
126
        parent::__construct($context, $formKey);
127
        $this->transactionStatus = $transactionStatus;
128
        $this->toolkitHelper = $toolkitHelper;
129
        $this->environmentHelper = $environmentHelper;
130
        $this->orderHelper = $orderHelper;
131
        $this->transactionStatusHandler = $transactionStatusHandler;
132
        $this->resultRawFactory = $resultRawFactory;
133
        $this->substituteOrder = $substituteOrder;
134
        $this->cache = $cache;
135
    }
136
137
    /**
138
     * Return request parameter value
139
     *
140
     * @param  string $sKey
141
     * @return string
142
     */
143
    protected function getParam($sKey)
144
    {
145
        return $this->getRequest()->getParam($sKey, '');
146
    }
147
148
    /**
149
     * Return Post array
150
     *
151
     * @return array
152
     */
153
    protected function getPostArray()
154
    {
155
        return $this->getRequest()->getPostValue();
156
    }
157
158
    /**
159
     * Write the TransactionStatus to the database
160
     *
161
     * @param  Order $oOrder
162
     * @param  array $aRequest
163
     * @param  bool  $blWillBeHandled
164
     * @return void
165
     */
166
    protected function logTransactionStatus(Order $oOrder, $aRequest, $blWillBeHandled)
167
    {
168
        $this->transactionStatus->addTransactionLogEntry($aRequest, $oOrder, $blWillBeHandled);
169
    }
170
171
    /**
172
     * Main method for executing all needed actions for the incoming TransactionStatus
173
     *
174
     * @return string
175
     */
176
    protected function handleTransactionStatus()
177
    {
178
        if (!$this->environmentHelper->isRemoteIpValid()) {
179
            return 'Access denied';
180
        } elseif (!$this->toolkitHelper->isKeyValid($this->getParam('key'))) {
181
            return 'Key wrong or missing!';
182
        }
183
184
        $oOrder = $this->orderHelper->getOrderByTxid($this->getParam('txid'));
185
        if (!$oOrder) {
186
            return 'Order not found';
187
        }
188
189
        if (in_array($this->getParam('txaction'), $this->aFirstContactWhitelist) === false && empty($this->transactionStatus->getAppointedIdByTxid($this->getParam('txid')))) {
190
            return 'Appointed status has to be handled first!';
191
        }
192
193
        $lockKey = 'payone_order_status_lock_' . $oOrder->getId();
194
        if ($this->cache->load($lockKey)) {
195
            return 'Another status is being handled at the moment.';
196
        }
197
198
        // Lock transaction status handling for this order for 50 seconds to not handle multiple statuses at the same time
199
        $this->cache->save('1', $lockKey, [], 50);
200
201
        try {
202
            if ($this->getParam('txaction') == 'appointed' && $oOrder->getStatus() == 'canceled') {
203
                // order was canceled in checkout, probably due to browser-back-button usage -> create a new order for incoming payment
204
                $oOrder = $this->substituteOrder->createSubstituteOrder($oOrder, false);
205
            }
206
207
            $this->logTransactionStatus($oOrder, $this->getPostArray(), true);
208
209
            $this->transactionStatusHandler->handle($oOrder, $this->getPostArray());
210
        } catch (\Exception $e) {
211
            $this->cache->remove($lockKey);
212
            throw $e;
213
        }
214
215
        return 'TSOK';
216
    }
217
218
    /**
219
     * Executing TransactionStatus handling
220
     *
221
     * @return \Magento\Framework\Controller\Result\Raw
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\Result\Raw 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...
222
     */
223
    public function execute()
224
    {
225
        $sOutput = $this->handleTransactionStatus();
226
227
        $oResultRaw = $this->resultRawFactory->create();
228
        $oResultRaw->setContents($sOutput);
229
        
230
        return $oResultRaw;
231
    }
232
}
233