Passed
Pull Request — master (#49)
by Mage
02:28
created

OverviewPost::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 10
dl 0
loc 29
rs 9.7

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
 * MagePrince
5
 * Copyright (C) 2020 Mageprince <[email protected]>
6
 *
7
 * @package Mageprince_BuyNow
8
 * @copyright Copyright (c) 2020 Mageprince (http://www.mageprince.com/)
9
 * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License,version 3 (GPL-3.0)
10
 * @author MagePrince <[email protected]>
11
 */
12
13
declare (strict_types = 1);
14
15
namespace Mageprince\BuyNow\Controller\Checkout;
16
17
use Magento\Checkout\Api\AgreementsValidatorInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Api\AgreementsValidatorInterface 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...
18
use Magento\Checkout\Api\Exception\PaymentProcessingRateLimitExceededException;
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Api\Exc...eLimitExceededException 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...
19
use Magento\Checkout\Api\PaymentProcessingRateLimiterInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Api\Pay...ingRateLimiterInterface 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...
20
use Magento\Customer\Api\AccountManagementInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Customer\Api\AccountManagementInterface 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...
21
use Magento\Customer\Api\CustomerRepositoryInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Customer\Api\CustomerRepositoryInterface 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...
22
use 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...
23
use Magento\Framework\App\Action\Context;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Action\Context 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...
24
use Magento\Framework\App\ObjectManager;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\ObjectManager 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...
25
use Magento\Framework\Data\Form\FormKey\Validator;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Data\Form\FormKey\Validator 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...
26
use Magento\Framework\Exception\PaymentException;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Exception\PaymentException 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...
27
use Magento\Framework\Session\SessionManagerInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Session\SessionManagerInterface 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...
28
use Magento\Multishipping\Controller\Checkout;
0 ignored issues
show
Bug introduced by
The type Magento\Multishipping\Controller\Checkout 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...
29
use Magento\Multishipping\Model\Checkout\Type\Multishipping\State;
0 ignored issues
show
Bug introduced by
The type Magento\Multishipping\Mo...ype\Multishipping\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 Psr\Log\LoggerInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Log\LoggerInterface 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
32
/**
33
 * Placing orders.
34
 *
35
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
36
 */
37
class OverviewPost extends \Magento\Multishipping\Controller\Checkout\OverviewPost
0 ignored issues
show
Bug introduced by
The type Magento\Multishipping\Co...r\Checkout\OverviewPost 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...
38
{
39
    /**
40
     * @var Validator
41
     * @deprecated Form key validation is handled on the framework level.
42
     */
43
    protected $formKeyValidator;
44
45
    /**
46
     * @var LoggerInterface
47
     */
48
    protected $logger;
49
50
    /**
51
     * @var AgreementsValidatorInterface
52
     */
53
    protected $agreementsValidator;
54
55
    /**
56
     * @var SessionManagerInterface
57
     */
58
    private $session;
59
60
    /**
61
     * @var PaymentProcessingRateLimiterInterface
62
     */
63
    private $paymentRateLimiter;
64
65
    /**
66
     * @param Context $context
67
     * @param Session $customerSession
68
     * @param CustomerRepositoryInterface $customerRepository
69
     * @param AccountManagementInterface $accountManagement
70
     * @param Validator $formKeyValidator
71
     * @param LoggerInterface $logger
72
     * @param AgreementsValidatorInterface $agreementValidator
73
     * @param SessionManagerInterface $session
74
     * @param PaymentProcessingRateLimiterInterface|null $paymentRateLimiter
75
     * @param \Magento\Quote\Model\QuoteFactory $quoteFactory
76
     */
77
    public function __construct(
78
        Context $context,
79
        Session $customerSession,
80
        CustomerRepositoryInterface $customerRepository,
81
        AccountManagementInterface $accountManagement,
82
        Validator $formKeyValidator,
83
        LoggerInterface $logger,
84
        AgreementsValidatorInterface $agreementValidator,
85
        SessionManagerInterface $session,
86
        ? PaymentProcessingRateLimiterInterface $paymentRateLimiter = null,
87
        \Magento\Quote\Model\QuoteFactory $quoteFactory
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\QuoteFactory 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...
88
    ) {
89
        $this->formKeyValidator = $formKeyValidator;
0 ignored issues
show
Deprecated Code introduced by
The property Mageprince\BuyNow\Contro...Post::$formKeyValidator has been deprecated: Form key validation is handled on the framework level. ( Ignorable by Annotation )

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

89
        /** @scrutinizer ignore-deprecated */ $this->formKeyValidator = $formKeyValidator;

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

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

Loading history...
90
        $this->logger = $logger;
91
        $this->agreementsValidator = $agreementValidator;
92
        $this->session = $session;
93
        $this->paymentRateLimiter = $paymentRateLimiter ?? ObjectManager::getInstance()->get(PaymentProcessingRateLimiterInterface::class);
94
        $this->customerRepository = $customerRepository;
0 ignored issues
show
Bug Best Practice introduced by
The property customerRepository does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
95
        $this->quoteFactory = $quoteFactory;
0 ignored issues
show
Bug Best Practice introduced by
The property quoteFactory does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
96
        parent::__construct(
97
            $context,
98
            $customerSession,
99
            $customerRepository,
100
            $accountManagement,
101
            $formKeyValidator,
102
            $logger,
103
            $agreementValidator,
104
            $session,
105
            $paymentRateLimiter
106
        );
107
    }
108
109
    /**
110
     * Overview action
111
     *
112
     * @return void
113
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
114
     */
115
    public function execute()
116
    {
117
        $currenctCart = $this->_getCheckout()->getQuote()->getId();
118
        $buyNowId = $this->_getCheckout()->getQuote()->getBuyNowId();
119
        $customerId = $this->_getCheckout()->getQuote()->getCustomerId();
120
        try {
121
            $this->paymentRateLimiter->limit();
122
            if (!$this->_validateMinimumAmount()) {
123
                return;
124
            }
125
126
            if (!$this->agreementsValidator->isValid(array_keys($this->getRequest()->getPost('agreement', [])))) {
127
                $this->messageManager->addErrorMessage(
128
                    __('Please agree to all Terms and Conditions before placing the order.')
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

128
                    /** @scrutinizer ignore-call */ 
129
                    __('Please agree to all Terms and Conditions before placing the order.')
Loading history...
129
                );
130
                $this->_redirect('*/*/billing');
131
                return;
132
            }
133
134
            $payment = $this->getRequest()->getPost('payment');
135
            $paymentInstance = $this->_getCheckout()->getQuote()->getPayment();
136
            if (isset($payment['cc_number'])) {
137
                $paymentInstance->setCcNumber($payment['cc_number']);
138
            }
139
            if (isset($payment['cc_cid'])) {
140
                $paymentInstance->setCcCid($payment['cc_cid']);
141
            }
142
            $this->_getCheckout()->createOrders();
143
            $this->_getState()->setCompleteStep(State::STEP_OVERVIEW);
144
145
            if ($this->session->getAddressErrors()) {
146
                $this->_getState()->setActiveStep(State::STEP_RESULTS);
147
                $this->_redirect('*/*/results');
148
            } else {
149
                $this->_getState()->setActiveStep(State::STEP_SUCCESS);
150
                $this->_getCheckout()->getCheckoutSession()->clearQuote();
151
                $this->_getCheckout()->getCheckoutSession()->setDisplaySuccess(true);
152
                /* get buy now feture */
153
                if (($customerId) && ($buyNowId)) {
154
                    $customerRepoData = $this->customerRepository->getById($customerId);
155
                    /* Update Currenct Cart */
156
                    if ($currenctCart) {
157
                        $UpadateCurrenctCart = $this->quoteFactory->create();
158
                        $UpadateCurrenctCart->load($currenctCart);
159
                        if ($UpadateCurrenctCart->hasData()) {
160
                            $UpadateCurrenctCart->setBuyNowId(0);
161
                            $UpadateCurrenctCart->setIsActive(0);
162
                            $UpadateCurrenctCart->save();
163
                        }
164
                    }
165
                    /* Restore old Cart */
166
                    $quoteDataReplace = $this->quoteFactory->create();
167
                    $quoteDataReplace->load($buyNowId);
168
                    if ($quoteDataReplace->hasData()) {
169
                        $quoteDataReplace->assignCustomer($customerRepoData);
170
                        $quoteDataReplace->setBuyNowId(0);
171
                        $quoteDataReplace->setIsActive(1);
172
                        $quoteDataReplace->save();
173
                    }
174
                }
175
                $this->_redirect('*/*/success');
176
            }
177
        } catch (PaymentProcessingRateLimitExceededException $ex) {
178
            $this->messageManager->addErrorMessage($ex->getMessage());
179
            $this->_redirect('*/*/overview');
180
        } catch (PaymentException $e) {
181
            $message = $e->getMessage();
182
            if (!empty($message)) {
183
                $this->messageManager->addErrorMessage($message);
184
            }
185
            $this->_redirect('*/*/billing');
186
        } catch (\Magento\Checkout\Exception $e) {
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Exception 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...
187
            $this->_objectManager->get(
188
                \Magento\Checkout\Helper\Data::class
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...
189
            )->sendPaymentFailedEmail(
190
                $this->_getCheckout()->getQuote(),
191
                $e->getMessage(),
192
                'multi-shipping'
193
            );
194
            $this->_getCheckout()->getCheckoutSession()->clearQuote();
195
            $this->messageManager->addErrorMessage($e->getMessage());
196
            $this->_redirect('*/cart');
197
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Exception\LocalizedException 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...
198
            $this->_objectManager->get(
199
                \Magento\Checkout\Helper\Data::class
200
            )->sendPaymentFailedEmail(
201
                $this->_getCheckout()->getQuote(),
202
                $e->getMessage(),
203
                'multi-shipping'
204
            );
205
            $this->messageManager->addErrorMessage($e->getMessage());
206
            $this->_redirect('*/*/billing');
207
        } catch (\Exception $e) {
208
            $this->logger->critical($e);
209
            try {
210
                $this->_objectManager->get(
211
                    \Magento\Checkout\Helper\Data::class
212
                )->sendPaymentFailedEmail(
213
                    $this->_getCheckout()->getQuote(),
214
                    $e->getMessage(),
215
                    'multi-shipping'
216
                );
217
            } catch (\Exception $e) {
218
                $this->logger->error($e->getMessage());
219
            }
220
            $this->messageManager->addErrorMessage(__('Order place error'));
221
            $this->_redirect('*/*/billing');
222
        }
223
    }
224
}
225