Completed
Push — master ( 0dbe68...17d58d )
by Florian
04:25
created

Review::updateShippingMethod()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 12
nc 4
nop 1
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 - 2017 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\Onepage;
28
29
use Magento\Framework\View\Result\Page;
30
use Magento\Quote\Model\Quote;
31
use Magento\Framework\Controller\ResultFactory;
32
use Magento\Framework\Controller\Result\Redirect as CoreRedirect;
33
34
/**
35
 * Controller for mandate management with debit payment
36
 */
37
class Review extends \Magento\Framework\App\Action\Action
38
{
39
    /**
40
     * Checkout session
41
     *
42
     * @var \Magento\Checkout\Model\Session
43
     */
44
    protected $checkoutSession;
45
46
    /**
47
     * Page result factory
48
     *
49
     * @var \Magento\Framework\View\Result\PageFactory
50
     */
51
    protected $pageFactory;
52
53
    /**
54
     * @var \Magento\Quote\Api\CartRepositoryInterface
55
     */
56
    protected $quoteRepository;
57
58
    /**
59
     * Constructor
60
     *
61
     * @param \Magento\Framework\App\Action\Context        $context
62
     * @param \Magento\Checkout\Model\Session              $checkoutSession
63
     * @param \Magento\Framework\View\Result\PageFactory   $pageFactory
64
     */
65
    public function __construct(
66
        \Magento\Framework\App\Action\Context $context,
67
        \Magento\Checkout\Model\Session $checkoutSession,
68
        \Magento\Framework\View\Result\PageFactory $pageFactory,
69
        \Magento\Quote\Api\CartRepositoryInterface $quoteRepository
70
    ) {
71
        parent::__construct($context);
72
        $this->checkoutSession = $checkoutSession;
73
        $this->pageFactory = $pageFactory;
74
        $this->quoteRepository = $quoteRepository;
75
    }
76
77
    /**
78
     * Render order review
79
     * Redirect to basket if quote or payment is missing
80
     *
81
     * @return null|Page|CoreRedirect
82
     */
83
    public function execute()
84
    {
85
        $sWorkorderId = $this->checkoutSession->getPayoneWorkorderId();
86
        if (empty($sWorkorderId)) {
87
            /** @var CoreRedirect $resultRedirect */
88
            $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
89
            return $resultRedirect->setPath('checkout');
90
        }
91
92
        $oPageObject = $this->pageFactory->create();
93
94
        $sSelectedShippingMethod = $this->getRequest()->getParam('shipping_method');
95
        if ($sSelectedShippingMethod) {
96
            $this->updateShippingMethod($sSelectedShippingMethod);
97
        }
98
99
        return $oPageObject;
100
    }
101
102
    /**
103
     * Update shipping method
104
     *
105
     * @param  string $sShippingMethod
106
     * @return void
107
     */
108
    protected function updateShippingMethod($sShippingMethod)
109
    {
110
        $oQuote = $this->checkoutSession->getQuote();
111
        $oShippingAddress = $oQuote->getShippingAddress();
112
        if (!$oQuote->getIsVirtual() && $oShippingAddress) {
113
            if ($sShippingMethod != $oShippingAddress->getShippingMethod()) {
114
                $this->ignoreAddressValidation($oQuote);
115
                $oShippingAddress->setShippingMethod($sShippingMethod)->setCollectShippingRates(true);
116
                $cartExtension = $oQuote->getExtensionAttributes();
117
                if ($cartExtension && $cartExtension->getShippingAssignments()) {
118
                    $cartExtension->getShippingAssignments()[0]->getShipping()->setMethod($sShippingMethod);
119
                }
120
                $oQuote->collectTotals();
121
                $this->quoteRepository->save($oQuote);
122
            }
123
        }
124
    }
125
126
    /**
127
     * Disable validation to make sure addresses will always be saved
128
     *
129
     * @param  Quote $oQuote
130
     * @return void
131
     */
132
    protected function ignoreAddressValidation(Quote $oQuote)
133
    {
134
        $oQuote->getBillingAddress()->setShouldIgnoreValidation(true);
135
        if (!$oQuote->getIsVirtual()) {
136
            $oQuote->getShippingAddress()->setShouldIgnoreValidation(true);
137
        }
138
    }
139
}
140