Passed
Pull Request — master (#49)
by
unknown
09:10
created

AddRemainingProduct::execute()   C

Complexity

Conditions 12
Paths 58

Size

Total Lines 38
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 12
eloc 31
c 2
b 0
f 0
nc 58
nop 1
dl 0
loc 38
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Observer;
14
15
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...
16
use Magento\Framework\Event\Observer;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Event\Observer 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\Framework\Event\ObserverInterface;
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Event\ObserverInterface 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
19
class AddRemainingProduct implements ObserverInterface
20
{
21
    /**
22
     * @var \Magento\Quote\Model\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...
23
     */
24
    protected $quoteFactory;
25
26
    /**
27
     * @var \Magento\Sales\Api\OrderRepositoryInterface
0 ignored issues
show
Bug introduced by
The type Magento\Sales\Api\OrderRepositoryInterface 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
     */
29
    protected $orderRepository;
30
31
    /**
32
     * @var \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...
33
     */
34
    protected $customerRepository;
35
36
    /**
37
     * @var \Mageprince\BuyNow\Helper\Data
38
     */
39
    protected $buyNowHelper;
40
41
    /**
42
     * @var CustomerCart
43
     */
44
    protected $cart;
45
46
    /**
47
     * @param \Magento\Quote\Model\QuoteFactory $quoteFactory
48
     * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
49
     * @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
50
     * @param \Mageprince\BuyNow\Helper\Data $buyNowHelper
51
     * @param CustomerCart $cart
52
     */
53
    public function __construct(
54
        \Magento\Quote\Model\QuoteFactory $quoteFactory,
55
        \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
56
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
57
        \Mageprince\BuyNow\Helper\Data $buyNowHelper,
58
        CustomerCart $cart
59
    ) {
60
        $this->quoteFactory = $quoteFactory;
61
        $this->orderRepository = $orderRepository;
62
        $this->customerRepository = $customerRepository;
63
        $this->buyNowHelper = $buyNowHelper;
64
        $this->cart = $cart;
65
    }
66
67
    public function execute(Observer $observer)
68
    {
69
        $flage = 1;
70
        $keepCartProduct = $this->buyNowHelper->keepCartProducts();
71
        $updateBuyNowCart = $restoreCartProduct = $this->buyNowHelper->restoreCartProducts();
72
        if (($keepCartProduct == 0) && ($restoreCartProduct == 1)) {
73
            try {
74
                $orderIds = $observer->getEvent()->getOrderIds();
75
                foreach ($orderIds as $orderIdsKey => $orderIdsVal) {
76
                    $orderData = $this->orderRepository->get($orderIdsVal);
77
                    $quoteData = $this->quoteFactory->create();
78
                    $quoteData->load($orderData->getQuoteId());
79
                    if ($quoteData->hasData()) {
80
                        if (($quoteData->getBuyNowId()) && ($quoteData->getCustomerId())) {
81
                            $customerRepoData = $this->customerRepository->getById($quoteData->getCustomerId());
82
                            $quoteDataReplace = $this->quoteFactory->create();
83
                            $quoteDataReplace->load($quoteData->getBuyNowId());
84
                            if ($quoteDataReplace->hasData()) {
85
                                $quoteDataReplace->assignCustomer($customerRepoData);
86
                                $quoteDataReplace->setBuyNowId(0);
87
                                $quoteDataReplace->setIsActive(1);
88
                                $quoteDataReplace->save();
89
                                if ($updateBuyNowCart) {
90
                                    $quoteData->setBuyNowId(0);
91
                                    $quoteDataReplace->setIsActive(0);
92
                                    $quoteData->save();
93
                                }
94
                            }
95
                        }
96
                    }
97
                    break;
98
                }
99
            } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
0 ignored issues
show
Bug introduced by
The type Magento\Framework\Exception\NoSuchEntityException 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...
100
                $flage = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $flage is dead and can be removed.
Loading history...
101
            } 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...
102
                $flage = 0;
103
            } catch (\Exception $e) {
104
                $flage = 0;
105
            }
106
        }
107
    }
108
}
109