Passed
Pull Request — master (#214)
by Florian
02:54
created

AmazonPay::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 12
dl 0
loc 26
rs 9.8666
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 - 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\Service\V1;
28
29
use Payone\Core\Api\AmazonPayInterface;
30
use Payone\Core\Model\PayoneConfig;
31
use Magento\Checkout\Model\Type\Onepage;
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...
32
use Magento\Customer\Model\Group;
0 ignored issues
show
Bug introduced by
The type Magento\Customer\Model\Group 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...
33
34
/**
35
 * Web API model for the PAYONE addresscheck
36
 */
37
class AmazonPay implements AmazonPayInterface
38
{
39
    /**
40
     * Factory for the response object
41
     *
42
     * @var \Payone\Core\Api\Data\AmazonPayResponseInterfaceFactory
0 ignored issues
show
Bug introduced by
The type Payone\Core\Api\Data\Ama...esponseInterfaceFactory 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...
43
     */
44
    protected $responseFactory;
45
46
    /**
47
     * Checkout session
48
     *
49
     * @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...
50
     */
51
    protected $checkoutSession;
52
53
    /**
54
     * Object of getconfiguration request
55
     *
56
     * @var \Payone\Core\Model\Api\Request\Genericpayment\GetConfiguration
57
     */
58
    protected $getConfiguration;
59
60
    /**
61
     * Object of getorderreferencedetails request
62
     *
63
     * @var \Payone\Core\Model\Api\Request\Genericpayment\GetOrderReferenceDetails
64
     */
65
    protected $getOrderReferenceDetails;
66
67
    /**
68
     * Object of setorderreferencedetails request
69
     *
70
     * @var \Payone\Core\Model\Api\Request\Genericpayment\SetOrderReferenceDetails
71
     */
72
    protected $setOrderReferenceDetails;
73
74
    /**
75
     * Amazon Pay payment object
76
     *
77
     * @var \Payone\Core\Model\Methods\AmazonPay
78
     */
79
    protected $payment;
80
81
    /**
82
     * Cart management interface
83
     *
84
     * @var \Magento\Quote\Api\CartManagementInterface
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Api\CartManagementInterface 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...
85
     */
86
    protected $cartManagement;
87
88
    /**
89
     * PAYONE order helper
90
     *
91
     * @var \Payone\Core\Helper\Order
92
     */
93
    protected $orderHelper;
94
95
    /**
96
     * PAYONE order helper
97
     *
98
     * @var \Payone\Core\Helper\Checkout
99
     */
100
    protected $checkoutHelper;
101
102
    /**
103
     * URL builder object
104
     *
105
     * @var \Magento\Framework\UrlInterface
0 ignored issues
show
Bug introduced by
The type Magento\Framework\UrlInterface 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...
106
     */
107
    protected $urlBuilder;
108
109
    /**
110
     * Block object of review page
111
     *
112
     * @var \Payone\Core\Block\Onepage\Review
113
     */
114
    protected $reviewBlock;
115
116
    /**
117
     * @var \Magento\Framework\App\ViewInterface
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\ViewInterface 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...
118
     */
119
    protected $view;
120
121
    /**
122
     * Constructor.
123
     *
124
     * @param \Payone\Core\Api\Data\AmazonPayResponseInterfaceFactory                $responseFactory
125
     * @param \Magento\Checkout\Model\Session                                        $checkoutSession
126
     * @param \Payone\Core\Model\Api\Request\Genericpayment\GetConfiguration         $getConfiguration
127
     * @param \Payone\Core\Model\Api\Request\Genericpayment\GetOrderReferenceDetails $getOrderReferenceDetails
128
     * @param \Payone\Core\Model\Methods\AmazonPay                                   $payment
129
     * @param \Magento\Quote\Api\CartManagementInterface                             $cartManagement
130
     * @param \Payone\Core\Helper\Order                                              $orderHelper
131
     * @param \Payone\Core\Helper\Checkout                                           $checkoutHelper
132
     * @param \Magento\Framework\UrlInterface                                        $urlBuilder
133
     * @param \Payone\Core\Block\Onepage\Review                                      $reviewBlock
134
     * @param \Magento\Framework\App\ViewInterface                                   $view
135
     */
136
    public function __construct(
137
        \Payone\Core\Api\Data\AmazonPayResponseInterfaceFactory $responseFactory,
138
        \Magento\Checkout\Model\Session $checkoutSession,
139
        \Payone\Core\Model\Api\Request\Genericpayment\GetConfiguration $getConfiguration,
140
        \Payone\Core\Model\Api\Request\Genericpayment\GetOrderReferenceDetails $getOrderReferenceDetails,
141
        \Payone\Core\Model\Api\Request\Genericpayment\SetOrderReferenceDetails $setOrderReferenceDetails,
142
        \Payone\Core\Model\Methods\AmazonPay $payment,
143
        \Magento\Quote\Api\CartManagementInterface $cartManagement,
144
        \Payone\Core\Helper\Order $orderHelper,
145
        \Payone\Core\Helper\Checkout $checkoutHelper,
146
        \Magento\Framework\UrlInterface $urlBuilder,
147
        \Payone\Core\Block\Onepage\Review $reviewBlock,
148
        \Magento\Framework\App\ViewInterface $view
149
    ) {
150
        $this->responseFactory = $responseFactory;
151
        $this->checkoutSession = $checkoutSession;
152
        $this->getConfiguration = $getConfiguration;
153
        $this->getOrderReferenceDetails = $getOrderReferenceDetails;
154
        $this->setOrderReferenceDetails = $setOrderReferenceDetails;
155
        $this->payment = $payment;
156
        $this->cartManagement = $cartManagement;
157
        $this->orderHelper = $orderHelper;
158
        $this->checkoutHelper = $checkoutHelper;
159
        $this->urlBuilder = $urlBuilder;
160
        $this->reviewBlock = $reviewBlock;
161
        $this->view = $view;
162
    }
163
164
    /**
165
     * Get Amazon workorder_id from session or request
166
     *
167
     * @return string
168
     */
169
    protected function collectWorkorderId()
170
    {
171
        $sWorkorderId = $this->checkoutSession->getAmazonWorkorderId();
172
        if (empty($sWorkorderId)) {
173
            $aResult = $this->getConfiguration->sendRequest($this->payment);
174
            if (isset($aResult['status']) && $aResult['status'] == 'OK' && isset($aResult['workorderid'])) {
175
                $sWorkorderId = $aResult['workorderid'];
176
                $this->checkoutSession->setAmazonWorkorderId($aResult['workorderid']);
177
            }
178
        }
179
        return $sWorkorderId;
180
    }
181
182
    /**
183
     * PAYONE addresscheck
184
     * The full class-paths must be given here otherwise the Magento 2 WebApi
185
     * cant handle this with its fake type system!
186
     *
187
     * @param  string $amazonReferenceId
188
     * @param  string $amazonAddressToken
189
     * @return \Payone\Core\Service\V1\Data\AmazonPayResponse
190
     */
191
    public function getWorkorderId($amazonReferenceId, $amazonAddressToken) {
192
        $blSuccess = false;
193
        $sWorkorderId = $this->collectWorkorderId();
194
        if (!empty($sWorkorderId)) {
195
            $blSuccess = true;
196
197
            $aResult = $this->getOrderReferenceDetails->sendRequest($this->payment, $sWorkorderId, $amazonReferenceId, $amazonAddressToken);
198
            if (isset($aResult['status']) && $aResult['status'] == 'OK') {
199
                $this->checkoutSession->setAmazonAddressToken($amazonAddressToken);
200
                $this->checkoutSession->setAmazonReferenceId($amazonReferenceId);
201
202
                $oQuote = $this->checkoutSession->getQuote();
203
                $oQuote = $this->orderHelper->updateAddresses($oQuote, $aResult);
204
205
                $oPayment = $oQuote->getPayment();
206
                $oPayment->setMethod(PayoneConfig::METHOD_AMAZONPAY);
207
208
                $oQuote->save();
209
210
                $aResult = $this->setOrderReferenceDetails->sendRequest($this->payment, 50.00, $sWorkorderId, $amazonReferenceId, $amazonAddressToken);
211
                if (isset($aResult['status']) && $aResult['status'] == 'OK') {
212
                    if ($this->checkoutHelper->getCurrentCheckoutMethod($oQuote) == Onepage::METHOD_GUEST) {
213
                        $oQuote->setCustomerId(null)
214
                            ->setCustomerEmail($oQuote->getBillingAddress()->getEmail())
215
                            ->setCustomerIsGuest(true)
216
                            ->setCustomerGroupId(Group::NOT_LOGGED_IN_ID);
217
                    }
218
219
                    $oQuote->setPayment($oPayment);
220
                    $oQuote->setInventoryProcessed(false);
221
                    $oQuote->collectTotals()->save();
222
                    $this->cartManagement->placeOrder($oQuote->getId());
223
                    $oQuote->setIsActive(false)->save();
224
                }
225
            }
226
        }
227
228
        $oResponse = $this->responseFactory->create();
229
        $oResponse->setData('workorderId', $sWorkorderId);
230
        $oResponse->setData('success', $blSuccess);
231
        $this->reviewBlock->setArea('frontend');
232
        $this->view->loadLayout('payone_onepage_review', true, true, true);
233
234
        $html = $this->view->getLayout()->getBlock('payone_onepage_review')->toHtml();
235
236
        $oResponse->setData('amazonReviewHtml', $html);
237
        return $oResponse;
238
    }
239
}
240