Passed
Push — master ( a098e0...40eb44 )
by Florian
03:06 queued 10s
created

SubstituteOrder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 18
rs 10
c 0
b 0
f 0

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
/**
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 - 2019 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\Model\Handler;
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
class SubstituteOrder
32
{
33
    /**
34
     * Checkout session
35
     *
36
     * @var \Magento\Checkout\Model\Session\Proxy
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Model\Session\Proxy 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 $checkoutSession;
39
40
    /**
41
     * Order repository
42
     *
43
     * @var \Magento\Quote\Api\CartRepositoryInterface
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Api\CartRepositoryInterface 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...
44
     */
45
    protected $quoteRepository;
46
47
    /**
48
     * Order repository
49
     *
50
     * @var \Magento\Sales\Api\OrderRepositoryInterface
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Api\OrderRepositoryInterface 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...
51
     */
52
    protected $orderRepository;
53
54
    /**
55
     * Quote management object
56
     *
57
     * @var \Magento\Quote\Model\QuoteManagement
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\QuoteManagement 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...
58
     */
59
    protected $quoteManagement;
60
61
    /**
62
     * PAYONE database helper
63
     *
64
     * @var \Payone\Core\Helper\Database
65
     */
66
    protected $databaseHelper;
67
68
    /**
69
     * Registry object
70
     *
71
     * @var \Magento\Framework\Registry
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Registry 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 $registry;
74
75
    /**
76
     * TransactionStatus factory
77
     *
78
     * @var \Payone\Core\Model\Entities\TransactionStatusFactory
0 ignored issues
show
Bug introduced by
The type Payone\Core\Model\Entiti...ransactionStatusFactory 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...
79
     */
80
    protected $statusFactory;
81
82
    /**
83
     * TransactionStatus handler
84
     *
85
     * @var \Payone\Core\Model\Handler\TransactionStatus
86
     */
87
    protected $transactionStatusHandler;
88
89
    /**
90
     * Constructor
91
     *
92
     * @param \Magento\Checkout\Model\Session\Proxy                $checkoutSession
93
     * @param \Magento\Quote\Api\CartRepositoryInterface           $quoteRepository
94
     * @param \Magento\Sales\Api\OrderRepositoryInterface          $orderRepository
95
     * @param \Magento\Quote\Model\QuoteManagement                 $quoteManagement
96
     * @param \Payone\Core\Helper\Database                         $databaseHelper
97
     * @param \Magento\Framework\Registry                          $registry
98
     * @param \Payone\Core\Model\Entities\TransactionStatusFactory $statusFactory
99
     * @param \Payone\Core\Model\Handler\TransactionStatus         $transactionStatusHandler
100
     */
101
    public function __construct(
102
        \Magento\Checkout\Model\Session\Proxy $checkoutSession,
103
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository,
104
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
105
        \Magento\Quote\Model\QuoteManagement $quoteManagement,
106
        \Payone\Core\Helper\Database $databaseHelper,
107
        \Magento\Framework\Registry $registry,
108
        \Payone\Core\Model\Entities\TransactionStatusFactory $statusFactory,
109
        \Payone\Core\Model\Handler\TransactionStatus $transactionStatusHandler
110
    ) {
111
        $this->checkoutSession = $checkoutSession;
112
        $this->quoteRepository = $quoteRepository;
113
        $this->orderRepository = $orderRepository;
114
        $this->quoteManagement = $quoteManagement;
115
        $this->databaseHelper = $databaseHelper;
116
        $this->registry = $registry;
117
        $this->statusFactory = $statusFactory;
118
        $this->transactionStatusHandler = $transactionStatusHandler;
119
    }
120
121
    /**
122
     * Updates the checkout session to the new order
123
     *
124
     * @param  Order $oOrder
125
     * @return void
126
     */
127
    public function updateCheckoutSession(Order $oOrder)
128
    {
129
        $this->checkoutSession->setLastOrderId($oOrder->getId());
130
        $this->checkoutSession->setLastRealOrderId($oOrder->getIncrementId());
131
        $this->checkoutSession->setLastSuccessQuoteId($oOrder->getQuoteId());
132
        $this->checkoutSession->setLastQuoteId($oOrder->getQuoteId());
133
        $this->checkoutSession->getQuote()->setIsActive(false)->save();
134
    }
135
136
    /**
137
     * Order was canceled before because of multi-tab browsing or back-button cancelling
138
     * Create a clean order for the payment
139
     *
140
     * @param  Order $canceledOrder
141
     * @param  bool  $blUpdateSession
142
     * @return Order
143
     */
144
    public function createSubstituteOrder(Order $canceledOrder, $blUpdateSession = true)
145
    {
146
        $this->registry->register('payone_creating_substitute_order', true, true);
147
148
        $oOldQuote = $this->quoteRepository->get($canceledOrder->getQuoteId());
149
        $oOldQuote->setIsActive(true);
150
        $oOldQuote->setReservedOrderId(null);
151
        $oOldQuote->save();
152
153
        $orderId = $this->quoteManagement->placeOrder($oOldQuote->getId());
154
        $newOrder = $this->orderRepository->get($orderId);
155
156
        $oldData = $canceledOrder->getData();
157
        foreach ($oldData as $sKey => $sValue) {
158
            if (stripos($sKey, 'payone') !== false) {
159
                $newOrder->setData($sKey, $sValue);
160
            }
161
        }
162
163
        $newOrder->setPayoneCancelSubstituteIncrementId($canceledOrder->getIncrementId());
164
        $newOrder->save();
165
166
        if ($blUpdateSession === true) {
167
            $this->updateCheckoutSession($newOrder);
168
        }
169
170
        $this->databaseHelper->relabelTransaction($canceledOrder->getId(), $newOrder->getId(), $newOrder->getPayment()->getId());
171
        $this->databaseHelper->relabelApiProtocol($canceledOrder->getIncrementId(), $newOrder->getIncrementId());
172
        $this->databaseHelper->relabelOrderPayment($canceledOrder->getIncrementId(), $newOrder->getId());
173
174
        $this->handleTransactionStatus($canceledOrder, $newOrder);
175
176
        $this->registry->unregister('payone_creating_substitute_order');
177
178
        return $newOrder;
179
    }
180
181
    /**
182
     * Handle stored TransactionStatus
183
     *
184
     * @param  Order $canceledOrder
185
     * @param  Order $newOrder
186
     * @return void
187
     */
188
    protected function handleTransactionStatus(Order $canceledOrder, Order $newOrder)
189
    {
190
        $aTransactionStatusIds = $this->databaseHelper->getNotHandledTransactionsByOrderId($canceledOrder->getIncrementId());
191
        foreach ($aTransactionStatusIds as $aRow) {
192
            $oTransactionStatus = $this->statusFactory->create();
193
            $oTransactionStatus->load($aRow['id']);
194
195
            $this->transactionStatusHandler->handle($newOrder, $oTransactionStatus->getRawStatusArray());
196
197
            $oTransactionStatus->setHasBeenHandled(true);
198
            $oTransactionStatus->save();
199
        }
200
    }
201
}