State::aroundCheck()   C
last analyzed

Complexity

Conditions 15
Paths 11

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 15
eloc 13
c 2
b 0
f 0
nc 11
nop 3
dl 0
loc 25
rs 5.9166

How to fix   Complexity   

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
/**
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\Plugins;
28
29
use Magento\Sales\Model\ResourceModel\Order\Handler\State as OrigState;
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Model\Reso...del\Order\Handler\State 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
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...
31
use Payone\Core\Model\Methods\PayoneMethod;
32
33
class State
34
{
35
    /**
36
     * Payone shop helper
37
     *
38
     * @var \Payone\Core\Helper\Shop
39
     */
40
    protected $shopHelper = null;
41
42
    /**
43
     * Constructor
44
     *
45
     * @param \Payone\Core\Helper\Shop $shopHelper
46
     */
47
    public function __construct(\Payone\Core\Helper\Shop $shopHelper) {
48
        $this->shopHelper = $shopHelper;
49
    }
50
51
    /**
52
     * Closed status bug exists since Magento 2.3.1
53
     * So if the used payment method is not a Payone method or the shop-version is 2.3.0 or lower, the original method can be used
54
     * Version-condition must be completed with an endversion when the problem is solved in Mage2 core!
55
     *
56
     * Plugin method will only be used for virtual orders payed with Payone for Magento2 shops 2.3.1 and higher
57
     * Otherwise the core method is used
58
     *
59
     * @param  OrigState $subject
60
     * @param  callable  $proceed
61
     * @param  Order     $order
62
     * @return OrigState
63
     */
64
    public function aroundCheck(OrigState $subject, callable $proceed, Order $order) {
65
        if (version_compare($this->shopHelper->getMagentoVersion(), '2.3.0', '<=') || !$order->getPayment()->getMethodInstance() instanceof PayoneMethod) {
66
            return $proceed($order);
67
        }
68
69
        $currentState = $order->getState();
70
        if ($currentState == Order::STATE_NEW && $order->getIsInProcess()) {
71
            $order->setState(Order::STATE_PROCESSING)->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_PROCESSING));
72
            $currentState = Order::STATE_PROCESSING;
73
        }
74
75
        if (!$order->isCanceled() && !$order->canUnhold() && !$order->canInvoice()) {
76
            // Virtual orders were being falsely set to closed here for authorization orders when the appointed status arrived because canCreditmemo is faulty in Mage 2.3.1 - 2.3.5
77
            // and because canShip will return false when the order is virtual therefore setStatus(Closed) was removed here for virtual orders
78
            // PLUS: Authorization orders where shipment was created before invoice was paid were falsely set to CLOSED status
79
            if (in_array($currentState, [Order::STATE_PROCESSING, Order::STATE_COMPLETE]) && !$order->canCreditmemo() && !$order->canShip() && !$order->getIsVirtual()) {
80
                if ($order->getTotalDue() == 0) {
81
                    $order->setState(Order::STATE_CLOSED)->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_CLOSED));
82
                }
83
            } elseif ($currentState === Order::STATE_PROCESSING && !$order->canShip()) {
84
                $order->setState(Order::STATE_COMPLETE)->setStatus($order->getConfig()->getStateDefaultStatus(Order::STATE_COMPLETE));
85
            }
86
        }
87
88
        return $subject;
89
    }
90
}
91