Skip to content

[Forwardport] Improvement: Magento\Sales\Helper\Guest refactoring and bugfix #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 14 additions & 27 deletions app/code/Magento/Sales/Helper/Guest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class Guest extends \Magento\Framework\App\Helper\AbstractHelper
/**
* @var \Magento\Store\Model\StoreManagerInterface
*/
private $_storeManager;
private $storeManager;

/**
* @var string
Expand Down Expand Up @@ -119,7 +119,7 @@ public function __construct(
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteria = null
) {
$this->coreRegistry = $coreRegistry;
$this->_storeManager = $storeManager;
$this->storeManager = $storeManager;
$this->customerSession = $customerSession;
$this->cookieManager = $cookieManager;
$this->cookieMetadataFactory = $cookieMetadataFactory;
Expand Down Expand Up @@ -158,9 +158,10 @@ public function loadValidOrder(App\RequestInterface $request)
// It is unique place in the class that process exception and only InputException. It is need because by
// input data we found order and one more InputException could be throws deeper in stack trace
try {
$order = (!empty($post) && isset($post['oar_order_id'], $post['oar_type']))
$order = (!empty($post)
&& isset($post['oar_order_id'], $post['oar_type'])
&& !$this->hasPostDataEmptyFields($post))
? $this->loadFromPost($post) : $this->loadFromCookie($fromCookie);
$this->validateOrderStoreId($order->getStoreId());
$this->coreRegistry->register('current_order', $order);
return true;
} catch (InputException $e) {
Expand All @@ -186,7 +187,7 @@ public function getBreadcrumbs(\Magento\Framework\View\Result\Page $resultPage)
[
'label' => __('Home'),
'title' => __('Go to Home Page'),
'link' => $this->_storeManager->getStore()->getBaseUrl()
'link' => $this->storeManager->getStore()->getBaseUrl()
]
);
$breadcrumbs->addCrumb(
Expand Down Expand Up @@ -247,12 +248,9 @@ private function loadFromCookie($fromCookie)
*/
private function loadFromPost(array $postData)
{
if ($this->hasPostDataEmptyFields($postData)) {
throw new InputException();
}
/** @var $order \Magento\Sales\Model\Order */
$order = $this->getOrderRecord($postData['oar_order_id']);
if (!$this->compareSoredBillingDataWithInput($order, $postData)) {
if (!$this->compareStoredBillingDataWithInput($order, $postData)) {
throw new InputException(__('You entered incorrect data. Please try again.'));
}
$toCookie = base64_encode($order->getProtectCode() . ':' . $postData['oar_order_id']);
Expand All @@ -267,7 +265,7 @@ private function loadFromPost(array $postData)
* @param array $postData
* @return bool
*/
private function compareSoredBillingDataWithInput(Order $order, array $postData)
private function compareStoredBillingDataWithInput(Order $order, array $postData)
{
$type = $postData['oar_type'];
$email = $postData['oar_email'];
Expand All @@ -288,7 +286,7 @@ private function compareSoredBillingDataWithInput(Order $order, array $postData)
private function hasPostDataEmptyFields(array $postData)
{
return empty($postData['oar_order_id']) || empty($postData['oar_billing_lastname']) ||
empty($postData['oar_type']) || empty($this->_storeManager->getStore()->getId()) ||
empty($postData['oar_type']) || empty($this->storeManager->getStore()->getId()) ||
!in_array($postData['oar_type'], ['email', 'zip'], true) ||
('email' === $postData['oar_type'] && empty($postData['oar_email'])) ||
('zip' === $postData['oar_type'] && empty($postData['oar_zip']));
Expand All @@ -306,26 +304,15 @@ private function getOrderRecord($incrementId)
$records = $this->orderRepository->getList(
$this->searchCriteriaBuilder
->addFilter('increment_id', $incrementId)
->addFilter('store_id', $this->storeManager->getStore()->getId())
->create()
);
if ($records->getTotalCount() < 1) {
throw new InputException(__($this->inputExceptionMessage));
}
$items = $records->getItems();
return array_shift($items);
}

/**
* Check that store_id from order are equals with system
*
* @param int $orderStoreId
* @return void
* @throws InputException
*/
private function validateOrderStoreId($orderStoreId)
{
if ($orderStoreId != $this->_storeManager->getStore()->getId()) {
$items = $records->getItems();
if (empty($items)) {
throw new InputException(__($this->inputExceptionMessage));
}

return array_shift($items);
}
}