Skip to content

Commit ba16130

Browse files
committed
Improvement: Magento\Sales\Helper\Guest refactoring and bugfix
1 parent 89cf3a9 commit ba16130

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

app/code/Magento/Sales/Helper/Guest.php

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class Guest extends \Magento\Framework\App\Helper\AbstractHelper
8383
/**
8484
* @var \Magento\Store\Model\StoreManagerInterface
8585
*/
86-
private $_storeManager;
86+
private $storeManager;
8787

8888
/**
8989
* @var string
@@ -119,7 +119,7 @@ public function __construct(
119119
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteria = null
120120
) {
121121
$this->coreRegistry = $coreRegistry;
122-
$this->_storeManager = $storeManager;
122+
$this->storeManager = $storeManager;
123123
$this->customerSession = $customerSession;
124124
$this->cookieManager = $cookieManager;
125125
$this->cookieMetadataFactory = $cookieMetadataFactory;
@@ -158,9 +158,10 @@ public function loadValidOrder(App\RequestInterface $request)
158158
// It is unique place in the class that process exception and only InputException. It is need because by
159159
// input data we found order and one more InputException could be throws deeper in stack trace
160160
try {
161-
$order = (!empty($post) && isset($post['oar_order_id'], $post['oar_type']))
161+
$order = (!empty($post)
162+
&& isset($post['oar_order_id'], $post['oar_type'])
163+
&& !$this->hasPostDataEmptyFields($post))
162164
? $this->loadFromPost($post) : $this->loadFromCookie($fromCookie);
163-
$this->validateOrderStoreId($order->getStoreId());
164165
$this->coreRegistry->register('current_order', $order);
165166
return true;
166167
} catch (InputException $e) {
@@ -186,7 +187,7 @@ public function getBreadcrumbs(\Magento\Framework\View\Result\Page $resultPage)
186187
[
187188
'label' => __('Home'),
188189
'title' => __('Go to Home Page'),
189-
'link' => $this->_storeManager->getStore()->getBaseUrl()
190+
'link' => $this->storeManager->getStore()->getBaseUrl()
190191
]
191192
);
192193
$breadcrumbs->addCrumb(
@@ -216,7 +217,7 @@ private function setGuestViewCookie($cookieValue)
216217
* Load order from cookie
217218
*
218219
* @param string $fromCookie
219-
* @return Order
220+
* @return \Magento\Sales\Model\Order\Interceptor
220221
* @throws InputException
221222
* @throws CookieSizeLimitReachedException
222223
* @throws FailureToSendException
@@ -240,19 +241,16 @@ private function loadFromCookie($fromCookie)
240241
* Load order data from post
241242
*
242243
* @param array $postData
243-
* @return Order
244+
* @return \Magento\Sales\Model\Order\Interceptor
244245
* @throws InputException
245246
* @throws CookieSizeLimitReachedException
246247
* @throws FailureToSendException
247248
*/
248249
private function loadFromPost(array $postData)
249250
{
250-
if ($this->hasPostDataEmptyFields($postData)) {
251-
throw new InputException();
252-
}
253251
/** @var $order \Magento\Sales\Model\Order */
254252
$order = $this->getOrderRecord($postData['oar_order_id']);
255-
if (!$this->compareSoredBillingDataWithInput($order, $postData)) {
253+
if (!$this->compareStoredBillingDataWithInput($order, $postData)) {
256254
throw new InputException(__('You entered incorrect data. Please try again.'));
257255
}
258256
$toCookie = base64_encode($order->getProtectCode() . ':' . $postData['oar_order_id']);
@@ -267,7 +265,7 @@ private function loadFromPost(array $postData)
267265
* @param array $postData
268266
* @return bool
269267
*/
270-
private function compareSoredBillingDataWithInput(Order $order, array $postData)
268+
private function compareStoredBillingDataWithInput(Order $order, array $postData)
271269
{
272270
$type = $postData['oar_type'];
273271
$email = $postData['oar_email'];
@@ -288,7 +286,7 @@ private function compareSoredBillingDataWithInput(Order $order, array $postData)
288286
private function hasPostDataEmptyFields(array $postData)
289287
{
290288
return empty($postData['oar_order_id']) || empty($postData['oar_billing_lastname']) ||
291-
empty($postData['oar_type']) || empty($this->_storeManager->getStore()->getId()) ||
289+
empty($postData['oar_type']) || empty($this->storeManager->getStore()->getId()) ||
292290
!in_array($postData['oar_type'], ['email', 'zip'], true) ||
293291
('email' === $postData['oar_type'] && empty($postData['oar_email'])) ||
294292
('zip' === $postData['oar_type'] && empty($postData['oar_zip']));
@@ -298,34 +296,23 @@ private function hasPostDataEmptyFields(array $postData)
298296
* Get order by increment_id and store_id
299297
*
300298
* @param string $incrementId
301-
* @return \Magento\Sales\Api\Data\OrderInterface
299+
* @return array
302300
* @throws InputException
303301
*/
304302
private function getOrderRecord($incrementId)
305303
{
306304
$records = $this->orderRepository->getList(
307305
$this->searchCriteriaBuilder
308306
->addFilter('increment_id', $incrementId)
307+
->addFilter('store_id', $this->storeManager->getStore()->getId())
309308
->create()
310309
);
311-
if ($records->getTotalCount() < 1) {
312-
throw new InputException(__($this->inputExceptionMessage));
313-
}
314-
$items = $records->getItems();
315-
return array_shift($items);
316-
}
317310

318-
/**
319-
* Check that store_id from order are equals with system
320-
*
321-
* @param int $orderStoreId
322-
* @return void
323-
* @throws InputException
324-
*/
325-
private function validateOrderStoreId($orderStoreId)
326-
{
327-
if ($orderStoreId != $this->_storeManager->getStore()->getId()) {
311+
$items = $records->getItems();
312+
if (empty($items)) {
328313
throw new InputException(__($this->inputExceptionMessage));
329314
}
315+
316+
return array_shift($items);
330317
}
331318
}

0 commit comments

Comments
 (0)