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

Add::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
c 0
b 0
f 0
nc 1
nop 11
dl 0
loc 29
rs 9.7333

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
namespace Mageprince\Buynow\Controller\Cart;
14
15
use Magento\Catalog\Api\ProductRepositoryInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Catalog\Api\ProductRepositoryInterface 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...
16
use Magento\Checkout\Model\Cart as CustomerCart;
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Model\Cart 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...
17
use Magento\Checkout\Model\Cart\RequestQuantityProcessor;
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Model\C...equestQuantityProcessor 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\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...
19
20
class Add extends \Magento\Checkout\Controller\Cart\Add
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Controller\Cart\Add 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
{
22
    /**
23
     * @var \Mageants\GiftCard\Model\Account
0 ignored issues
show
Bug introduced by
The type Mageants\GiftCard\Model\Account 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
     */
25
    protected $productRepository;
26
27
    /**
28
     * @var \Magento\Quote\Model\QuoteRepository
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Model\QuoteRepository 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
     */
30
    protected $quoteRepository;
31
32
    /**
33
     * @var RequestQuantityProcessor
34
     */
35
    private $quantityProcessor;
36
37
    /**
38
     * @param \Magento\Framework\App\Action\Context $context
39
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
40
     * @param \Magento\Checkout\Model\Session $checkoutSession
41
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
42
     * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
43
     * @param CustomerCart $cart
44
     * @param ProductRepositoryInterface $productRepository
45
     * @param RequestQuantityProcessor $quantityProcessor
46
     * @param \Magento\Quote\Model\QuoteRepository $quoteRepository
47
     * @param \Magento\Quote\Model\QuoteFactory $quoteFactory
48
     * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
49
     */
50
    public function __construct(
51
        \Magento\Framework\App\Action\Context $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...
52
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\Config\ScopeConfigInterface 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...
53
        \Magento\Checkout\Model\Session $checkoutSession,
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...
54
        \Magento\Store\Model\StoreManagerInterface $storeManager,
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\StoreManagerInterface 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...
55
        \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
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...
56
        CustomerCart $cart,
57
        ProductRepositoryInterface $productRepository,
58
        ? RequestQuantityProcessor $quantityProcessor = null,
59
        \Magento\Quote\Model\QuoteRepository $quoteRepository,
60
        \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...
61
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
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...
62
    ) {
63
        $this->productRepository = $productRepository;
64
        $this->quantityProcessor = $quantityProcessor ?? ObjectManager::getInstance()->get(RequestQuantityProcessor::class);
65
        $this->quoteRepository = $quoteRepository;
66
        $this->storeManager = $storeManager;
0 ignored issues
show
Bug Best Practice introduced by
The property storeManager does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
67
        $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...
68
        $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...
69
        $this->scopeConfig = $scopeConfig;
0 ignored issues
show
Bug Best Practice introduced by
The property scopeConfig does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
70
        parent::__construct(
71
            $context,
72
            $scopeConfig,
73
            $checkoutSession,
74
            $storeManager,
75
            $formKeyValidator,
76
            $cart,
77
            $productRepository,
78
            $quantityProcessor
79
        );
80
    }
81
82
    /**
83
     * Add product to shopping cart action
84
     *
85
     * @return \Magento\Framework\Controller\Result\Redirect
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Controller\Result\Redirect 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...
86
     * @SuppressWarnings(PHPMD.CyclomaticComplexity)
87
     */
88
    public function execute()
89
    {
90
        $mainQuoteId = $this->cart->getQuote()->getEntityId();
91
        if (!$this->_formKeyValidator->validate($this->getRequest())) {
92
            return $this->resultRedirectFactory->create()->setPath('*/*/');
93
        }
94
        $params = $this->getRequest()->getParams();
95
        try {
96
            if (isset($params['qty'])) {
97
                $filter = new \Zend_Filter_LocalizedToNormalized(
0 ignored issues
show
Bug introduced by
The type Zend_Filter_LocalizedToNormalized 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...
98
                    ['locale' => $this->_objectManager->get('Magento\Framework\Locale\ResolverInterface')->getLocale()]
99
                );
100
                $params['qty'] = $filter->filter($params['qty']);
101
            }
102
            $product = $this->_initProduct();
103
            $related = $this->getRequest()->getParam('related_product');
104
            if (!$product) {
105
                return $this->goBack();
106
            }
107
            $cartProducts = $this->scopeConfig->getValue(
108
                'buynow/general/keep_cart_products',
109
                \Magento\Store\Model\ScopeInterface::SCOPE_STORE
0 ignored issues
show
Bug introduced by
The type Magento\Store\Model\ScopeInterface 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...
110
            );
111
            if (!$cartProducts) {
112
                $cartRestore = $this->scopeConfig->getValue(
113
                    'buynow/general/restore_cart_products',
114
                    \Magento\Store\Model\ScopeInterface::SCOPE_STORE
115
                );
116
                if ($cartRestore) {
117
                    if ($mainQuoteId) {
118
                        $quoteRepo = $this->quoteRepository->get($mainQuoteId);
119
                        $quoteRepo->setData('is_active', 0);
120
                        $this->quoteRepository->save($quoteRepo);
121
                    }
122
                    $store = $this->storeManager->getStore();
123
                    $websiteId = $this->storeManager->getStore()->getWebsiteId();
0 ignored issues
show
Unused Code introduced by
The assignment to $websiteId is dead and can be removed.
Loading history...
124
                    $quoteData = $this->quoteFactory->create();
125
                    $quoteData->setStore($store);
126
                    $quoteData->setCurrency();
127
                    if ($this->cart->getQuote()->getCustomerId()) {
128
                        $customerId = $this->cart->getQuote()->getCustomerId();
129
                        $customerEmail = $this->cart->getQuote()->getCustomerEmail();
0 ignored issues
show
Unused Code introduced by
The assignment to $customerEmail is dead and can be removed.
Loading history...
130
                        $customerRepoData = $this->customerRepository->getById($customerId);
131
                        $quoteData->assignCustomer($customerRepoData);
132
                    }
133
                    if ($mainQuoteId) {
134
                        $quoteData->setBuyNowId($mainQuoteId);
135
                    }
136
                    $quoteData->save();
137
                    $this->cart->setQuote($quoteData);
138
                } else {
139
                    $this->cart->truncate(); //remove all products from cart
140
                }
141
            }
142
            $this->cart->addProduct($product, $params);
143
            if (!empty($related)) {
144
                $this->cart->addProductsByIds(explode(',', $related));
145
            }
146
            $this->cart->save();
147
148
            // $this->_eventManager->dispatch(
149
            //     'checkout_cart_add_product_complete',
150
            //     ['product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse()]
151
            // );
152
153
            if (!$this->_checkoutSession->getNoCartRedirect(true)) {
154
                $baseUrl = $this->_objectManager->get('\Magento\Store\Model\StoreManagerInterface')
155
                    ->getStore()->getBaseUrl();
156
                return $this->goBack($baseUrl . 'checkout/', $product);
157
            }
158
        } 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...
159
            if ($this->_checkoutSession->getUseNotice(true)) {
160
                $this->messageManager->addNotice(
161
                    $this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($e->getMessage())
162
                );
163
            } else {
164
                $messages = array_unique(explode("\n", $e->getMessage()));
165
                foreach ($messages as $message) {
166
                    $this->messageManager->addError(
167
                        $this->_objectManager->get('Magento\Framework\Escaper')->escapeHtml($message)
168
                    );
169
                }
170
            }
171
            $url = $this->_checkoutSession->getRedirectUrl(true);
172
            if (!$url) {
173
                $cartUrl = $this->_objectManager->get('Magento\Checkout\Helper\Cart')->getCartUrl();
174
                $url = $this->_redirect->getRedirectUrl($cartUrl);
175
            }
176
            return $this->goBack($url);
177
        } catch (\Exception $e) {
178
            $this->messageManager->addException($e, __('We can\'t add this item to your shopping cart right now.'));
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

178
            $this->messageManager->addException($e, /** @scrutinizer ignore-call */ __('We can\'t add this item to your shopping cart right now.'));
Loading history...
179
            $this->_objectManager->get('Psr\Log\LoggerInterface')->critical($e);
180
            return $this->goBack();
181
        }
182
    }
183
}
184