Completed
Push — master ( 9097d5...0ce82e )
by Florian
06:13
created

Express   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 131
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 1
C execute() 0 32 7
B loginNeededForExpressCheckout() 0 12 5
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\Paypal;
28
29
use Magento\Framework\Controller\ResultFactory;
30
use Magento\Quote\Model\Quote;
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\Quote 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\PayoneConfig;
32
33
/**
34
 * Controller for PayPal Express initiation
35
 */
36
class Express extends \Magento\Framework\App\Action\Action
37
{
38
    /**
39
     * Checkout session
40
     *
41
     * @var \Magento\Checkout\Model\Session
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Model\Session 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...
42
     */
43
    protected $checkoutSession;
44
45
    /**
46
     * PAYONE request model
47
     *
48
     * @var \Payone\Core\Model\Api\Request\Genericpayment\PayPalExpress
49
     */
50
    protected $genericRequest;
51
52
    /**
53
     * PayPal payment model
54
     *
55
     * @var \Payone\Core\Model\Methods\Paypal
56
     */
57
    protected $paypalPayment;
58
59
    /**
60
     * Checkout helper
61
     *
62
     * @var \Magento\Checkout\Helper\Data
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Helper\Data 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...
63
     */
64
    protected $checkoutHelper;
65
66
    /**
67
     * Customer session
68
     *
69
     * @var \Magento\Customer\Model\Session
0 ignored issues
show
Bug introduced by
The type Magento\Customer\Model\Session 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...
70
     */
71
    protected $customerSession;
72
73
    /**
74
     * PAYONE payment helper
75
     *
76
     * @var \Payone\Core\Helper\Payment
77
     */
78
    protected $paymentHelper;
79
80
    /**
81
     * Constructor
82
     *
83
     * @param \Magento\Framework\App\Action\Context                       $context
84
     * @param \Magento\Checkout\Model\Session                             $checkoutSession
85
     * @param \Payone\Core\Model\Api\Request\Genericpayment\PayPalExpress $genericRequest
86
     * @param \Payone\Core\Model\Methods\Paypal                           $paypalPayment
87
     * @param \Magento\Checkout\Helper\Data                               $checkoutHelper
88
     * @param \Magento\Customer\Model\Session                             $customerSession
89
     * @param \Payone\Core\Helper\Payment                                 $paymentHelper
90
     */
91
    public function __construct(
92
        \Magento\Framework\App\Action\Context $context,
93
        \Magento\Checkout\Model\Session $checkoutSession,
94
        \Payone\Core\Model\Api\Request\Genericpayment\PayPalExpress $genericRequest,
95
        \Payone\Core\Model\Methods\Paypal $paypalPayment,
96
        \Magento\Checkout\Helper\Data $checkoutHelper,
97
        \Magento\Customer\Model\Session $customerSession,
98
        \Payone\Core\Helper\Payment $paymentHelper
99
    ) {
100
        parent::__construct($context);
101
        $this->checkoutSession = $checkoutSession;
102
        $this->genericRequest = $genericRequest;
103
        $this->paypalPayment = $paypalPayment;
104
        $this->checkoutHelper = $checkoutHelper;
105
        $this->customerSession = $customerSession;
106
        $this->paymentHelper = $paymentHelper;
107
    }
108
109
    /**
110
     * Determine if a logged in customer is required for an express checkout
111
     * For example needed for virtual products ( download-products etc. )
112
     *
113
     * @param  Quote $oQuote
114
     * @return bool
115
     */
116
    protected function loginNeededForExpressCheckout(Quote $oQuote)
117
    {
118
        $oCustomer = $this->customerSession->getCustomerDataObject();
119
        if (!$oCustomer->getId()) {
120
            $sCheckoutMethod = $oQuote->getCheckoutMethod();
121
            if ((!$sCheckoutMethod || $sCheckoutMethod != \Magento\Checkout\Model\Type\Onepage::METHOD_REGISTER)
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Model\Type\Onepage 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...
122
                && !$this->checkoutHelper->isAllowedGuestCheckout($oQuote, $oQuote->getStoreId())
123
            ) {
124
                return true;
125
            }
126
        }
127
        return false;
128
    }
129
130
    /**
131
     * Redirect to payment-provider or to success page
132
     *
133
     * @return void
134
     */
135
    public function execute()
136
    {
137
        $oQuote = $this->checkoutSession->getQuote();
138
139
        if ($this->paymentHelper->isPayPalExpressActive() && $oQuote && $oQuote->hasItems()) {
140
            if ($this->loginNeededForExpressCheckout($oQuote)) {
141
                $this->messageManager->addNoticeMessage(__('Please sign in to check out.'));
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
                $this->messageManager->addNoticeMessage(/** @scrutinizer ignore-call */ __('Please sign in to check out.'));
Loading history...
142
143
                $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
144
                return $resultRedirect->setPath('customer/account/login');
0 ignored issues
show
Bug introduced by
The method setPath() does not exist on Magento\Framework\Controller\ResultInterface. It seems like you code against a sub-type of Magento\Framework\Controller\ResultInterface such as Magento\Framework\Controller\Result\Redirect. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
                return $resultRedirect->/** @scrutinizer ignore-call */ setPath('customer/account/login');
Loading history...
145
            }
146
147
            $this->paypalPayment->setIsPayPalExpress(true);
148
            $aResponse = $this->genericRequest->sendRequest($oQuote, $this->paypalPayment);
149
            if ($aResponse['status'] == 'ERROR') {
150
                $this->messageManager->addError(__($aResponse['customermessage']));
0 ignored issues
show
Deprecated Code introduced by
The function Magento\Framework\Messag...erInterface::addError() has been deprecated: 100.1.0 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

150
                /** @scrutinizer ignore-deprecated */ $this->messageManager->addError(__($aResponse['customermessage']));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
151
152
                $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
153
                return $resultRedirect->setPath('checkout/cart');
154
            } elseif ($aResponse['status'] == 'REDIRECT') {
155
                $oPayment = $oQuote->getPayment();
156
                $oPayment->setMethod(PayoneConfig::METHOD_PAYPAL);
157
                $oQuote->setPayment($oPayment);
158
                $oQuote->save();
159
160
                $this->checkoutSession->setPayoneWorkorderId($aResponse['workorderid']);
161
                $this->_redirect($aResponse['redirecturl']);
162
            }
163
            return;
164
        }
165
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
166
        return $resultRedirect->setPath('checkout/cart');
167
    }
168
}
169