GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 451ccd...4a5da4 )
by Ryan
17:00 queued 04:51
created

handleAdminOrderException()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
/**
3
 * Copyright (c) 2013-2014 eBay Enterprise, Inc.
4
 *
5
 * NOTICE OF LICENSE
6
 *
7
 * This source file is subject to the Open Software License (OSL 3.0)
8
 * that is bundled with this package in the file LICENSE.md.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * @copyright   Copyright (c) 2013-2014 eBay Enterprise, Inc. (http://www.ebayenterprise.com/)
13
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
14
 */
15
16
class EbayEnterprise_Inventory_Model_Observer
17
{
18
    /** @var EbayEnterprise_Inventory_Model_Quantity_Service */
19
    protected $quantityService;
20
    /** @var EbayEnterprise_Inventory_Model_Details_Service */
21
    protected $detailsService;
22
    /** @var EbayEnterprise_Inventory_Model_Allocation_Service */
23
    protected $allocationService;
24
    /** @var EbayEnterprise_MageLog_Helper_Data */
25
    protected $logger;
26
    /** @var EbayEnterprise_MageLog_Helper_Context */
27
    protected $logContext;
28
    /** @var Mage_Core_Model_App */
29
    protected $app;
30
    /** @var Mage_Adminhtml_Model_Session_Quote */
31
    protected $adminQuoteSession;
32
33
    /**
34
     * @param array $args May contain:
35
     *                    - quantity_service => EbayEnterprise_Inventory_Model_Quantity_Service
36
     *                    - details_service => EbayEnterprise_Inventory_Model_Details_Service
37
     *                    - logger => EbayEnterprise_MageLog_Helper_Data
38
     *                    - log_context => EbayEnterprise_MageLog_Helper_Context
39
     */
40 View Code Duplication
    public function __construct(array $args = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        list(
43
            $this->quantityService,
44
            $this->detailsService,
45
            $this->allocationService,
46
            $this->logger,
47
            $this->logContext,
48
            $this->app
49
        ) = $this->checkTypes(
50
            $this->nullCoalesce(
51
                $args,
52
                'quantity_service',
53
                Mage::getModel('ebayenterprise_inventory/quantity_service')
54
            ),
55
            $this->nullCoalesce($args, 'details_service', Mage::getModel('ebayenterprise_inventory/details_service')),
56
            $this->nullCoalesce(
57
                $args,
58
                'allocation_service',
59
                Mage::getModel('ebayenterprise_inventory/allocation_service')
60
            ),
61
            $this->nullCoalesce($args, 'logger', Mage::helper('ebayenterprise_magelog')),
62
            $this->nullCoalesce($args, 'log_context', Mage::helper('ebayenterprise_magelog/context')),
63
            Mage::app()
64
        );
65
    }
66
67
    /**
68
     * Enforce type checks on constructor init params.
69
     *
70
     * @param EbayEnterprise_Inventory_Model_Quantity_Service
71
     * @param EbayEnterprise_Inventory_Model_Details_Service
72
     * @param EbayEnterprise_Inventory_Model_Allocation_Service
73
     * @param EbayEnterprise_MageLog_Helper_Data
74
     * @param EbayEnterprise_MageLog_Helper_Context
75
     * @param Mage_Core_Model_App
76
     * @return array
77
     */
78
    protected function checkTypes(
0 ignored issues
show
Unused Code introduced by
The method parameter $quantityService is never used
Loading history...
Unused Code introduced by
The method parameter $detailsService is never used
Loading history...
Unused Code introduced by
The method parameter $allocationService is never used
Loading history...
Unused Code introduced by
The method parameter $logger is never used
Loading history...
Unused Code introduced by
The method parameter $logContext is never used
Loading history...
Unused Code introduced by
The method parameter $app is never used
Loading history...
79
        EbayEnterprise_Inventory_Model_Quantity_Service $quantityService,
0 ignored issues
show
Unused Code introduced by
The parameter $quantityService is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
        EbayEnterprise_Inventory_Model_Details_Service $detailsService,
0 ignored issues
show
Unused Code introduced by
The parameter $detailsService is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
81
        EbayEnterprise_Inventory_Model_Allocation_Service $allocationService,
0 ignored issues
show
Unused Code introduced by
The parameter $allocationService is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
        EbayEnterprise_MageLog_Helper_Data $logger,
0 ignored issues
show
Unused Code introduced by
The parameter $logger is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
83
        EbayEnterprise_MageLog_Helper_Context $logContext,
0 ignored issues
show
Unused Code introduced by
The parameter $logContext is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
84
        Mage_Core_Model_App $app
0 ignored issues
show
Unused Code introduced by
The parameter $app is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
    ) {
86
        return func_get_args();
87
    }
88
89
    /**
90
     * Fill in default values.
91
     *
92
     * @param string
93
     * @param array
94
     * @param mixed
95
     * @return mixed
96
     */
97
    protected function nullCoalesce(array $arr, $key, $default)
98
    {
99
        return isset($arr[$key]) ? $arr[$key] : $default;
100
    }
101
102
    /**
103
     * Before collecting item totals, check that all items
104
     * in the quote are available to be fulfilled.
105
     *
106
     * @param Varien_Event_Observer
107
     * @return self
108
     */
109
    public function handleBeforeCollectTotals(Varien_Event_Observer $observer)
110
    {
111
        try {
112
            $quote = $observer->getEvent()->getQuote();
113
            $this->quantityService
114
                ->checkQuoteInventory($quote);
115
        } catch (EbayEnterprise_Inventory_Exception_Quantity_Collector_Exception $e) {
116
            $this->logger->logException($e, $this->logContext->getMetaData(__CLASS__, [], $e));
117
            $this->logger->warning(
118
                'Caught the following exception: {exception_message}.',
119
                $this->logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])
120
            );
121
        } catch (EbayEnterprise_Inventory_Exception_Quantity_Unavailable_Exception $e) {
122
            $this->logger->logException($e, $this->logContext->getMetaData(__CLASS__, [], $e));
123
            $this->logger->warning(
124
                'Caught the following exception: {exception_message}.',
125
                $this->logContext->getMetaData(__CLASS__, ['exception_message' => $e->getMessage()])
126
            );
127
            if ($this->app->getStore()->isAdmin()) {
128
                // Handling admin exception here because it is not
129
                // being caught in the admin controller causing it
130
                // to display exception traces in the page.
131
                return $this->handleAdminOrderException($e, $quote);
132
            }
133
            // Continue to throw the exception in the frontend and let
134
            // the frontend controller handle it.
135
            throw $e;
136
        }
137
        return $this;
138
    }
139
140
    /**
141
     * Handle admin out of stock Exception thrown from ROM Inventory Service.
142
     *
143
     * @param  EbayEnterprise_Inventory_Exception_Quantity_Unavailable_Exception
144
     * @param  Mage_Sales_Model_Quote
145
     * @return self
146
     */
147
    protected function handleAdminOrderException(
148
        EbayEnterprise_Inventory_Exception_Quantity_Unavailable_Exception $e,
149
        Mage_Sales_Model_Quote $quote
150
    )
151
    {
152
        $this->getAdminQuoteSession()->addError($e->getMessage());
153
        foreach ($quote->getAllVisibleItems() as $quoteItem) {
154
            if (!$this->quantityService->isItemAvailable($quoteItem)) {
155
                $quote->deleteItem($quoteItem);
156
            }
157
        }
158
        return $this;
159
    }
160
161
    /**
162
     * Stash the adminhtml quote session to a class property.
163
     * @return Mage_Adminhtml_Model_Session_Quote
164
     */
165
    protected function getAdminQuoteSession()
166
    {
167
        if (!$this->adminQuoteSession) {
168
            $this->adminQuoteSession = Mage::getSingleton('adminhtml/session_quote');
169
        }
170
        return $this->adminQuoteSession;
171
    }
172
173
    /**
174
     * add estimated shipping information to the item payload
175
     * @param  Varien_Event_Observer $observer
176
     * @return self
177
     */
178
    public function handleEbayEnterpriseOrderCreateItem(Varien_Event_Observer $observer)
179
    {
180
        $event = $observer->getEvent();
181
        $itemPayload = $event->getItemPayload();
182
        $item = $this->getItemForLookup($event->getItem());
183
        Mage::getModel('ebayenterprise_inventory/order_create_item_details')
184
            ->injectShippingEstimates($itemPayload, $item);
185
        Mage::getModel('ebayenterprise_inventory/order_create_item_allocation')
186
            ->injectReservationInfo($itemPayload, $item);
187
        return $this;
188
    }
189
190
    /**
191
     * get the correct order item
192
     * @param Mage_Sales_Model_Order_Item
193
     * @return Mage_Sales_Model_Order_Item
194
     */
195
    protected function getItemForLookup(Mage_Sales_Model_Order_Item $item)
196
    {
197
        if ($item->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
198
            $item = reset($item->getChildrenItems());
0 ignored issues
show
Bug introduced by
$item->getChildrenItems() cannot be passed to reset() as the parameter $array expects a reference.
Loading history...
199
        }
200
        return $item;
201
    }
202
203
    /**
204
     * Inject the ship from address into the tax item payload
205
     *
206
     * @param Varien_Event_Observer
207
     * @return self
208
     */
209
    public function handleEbayEnterpriseTaxItemShipOrigin(Varien_Event_Observer $observer)
210
    {
211
        $item = $observer->getEvent()->getItem();
212
        $address = $observer->getEvent()->getAddress();
213
        Mage::getModel('ebayenterprise_inventory/tax_shipping_origin')
214
            ->injectShippingOriginForItem($item, $address);
215
        return $this;
216
    }
217
218
    /**
219
     * trigger inventory allocation for the quote
220
     *
221
     * @param Varien_Event_Observer
222
     * @return self
223
     */
224
    public function handleSalesOrderPlaceBefore(Varien_Event_Observer $observer)
225
    {
226
        $order = $observer->getEvent()->getOrder();
227
        $quote = $order->getQuote();
228
        $this->allocationService->allocateInventoryForQuote($quote);
229
        return $this;
230
    }
231
232
    /**
233
     * trigger a rollback if the order fails
234
     *
235
     * @return self
236
     */
237
    public function handleSalesModelServiceQuoteSubmitFailure()
238
    {
239
        $this->allocationService->undoAllocation();
240
        return $this;
241
    }
242
243
    /**
244
     * Clean-up quantity session after an order was successfully created.
245
     *
246
     * @return self
247
     */
248
    public function handleEbayEnterpriseOrderCreateSuccessful()
249
    {
250
        /** @var EbayEnterprise_Inventory_Model_Session $inventorySession */
251
        $inventorySession = Mage::getSingleton('ebayenterprise_inventory/session');
252
        $inventorySession->unsetData('quantity_results');
253
        return $this;
254
    }
255
}
256