Completed
Push — master ( b50753...c0fc73 )
by Florian
10s
created

PredispatchCheckoutIndex::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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\Observer;
28
29
use Magento\Framework\Event\ObserverInterface;
30
use Magento\Framework\Event\Observer;
31
use Magento\Checkout\Model\Session;
32
use Magento\Sales\Model\OrderFactory;
33
use Magento\Framework\Exception\LocalizedException;
34
35
/**
36
 * Event class to prevent the basket from getting lost with redirect payment types
37
 * when the customer uses the browser back-button
38
 */
39
class PredispatchCheckoutIndex implements ObserverInterface
40
{
41
    /**
42
     * Checkout session
43
     *
44
     * @var \Magento\Checkout\Model\Session
45
     */
46
    protected $checkoutSession;
47
48
    /**
49
     * Order factory
50
     *
51
     * @var OrderFactory
52
     */
53
    protected $orderFactory;
54
55
    /**
56
     * Constructor
57
     *
58
     * @param Session      $checkoutSession
59
     * @param OrderFactory $orderFactory
60
     */
61
    public function __construct(Session $checkoutSession, OrderFactory $orderFactory)
62
    {
63
        $this->checkoutSession = $checkoutSession;
64
        $this->orderFactory = $orderFactory;
65
    }
66
67
    /**
68
     * @param  Observer $observer
69
     * @return $this
70
     */
71
    public function execute(Observer $observer)
72
    {
73
        if ($this->checkoutSession->getPayoneCustomerIsRedirected()) {
74
            try {
75
                $orderId = $this->checkoutSession->getLastOrderId();
76
                $order = $orderId ? $this->orderFactory->create()->load($orderId) : false;
77
                if ($order) {
78
                    $order->cancel()->save();
79
80
                    $this->checkoutSession->restoreQuote();
81
82
                    $this->checkoutSession
83
                        ->unsLastQuoteId()
84
                        ->unsLastSuccessQuoteId()
85
                        ->unsLastOrderId()
86
                        ->unsLastRealOrderId();
87
                }
88
            } catch (LocalizedException $e) {
89
                // catch and continue - do something when needed
90
            } catch (\Exception $e) {
91
                // catch and continue - do something when needed
92
            }
93
94
            $this->checkoutSession->unsPayoneCustomerIsRedirected();
95
            $this->checkoutSession->setIsPayoneRedirectCancellation(true);
96
        }
97
    }
98
}
99