123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Multishipping\Controller;
- use Magento\Customer\Api\AccountManagementInterface;
- use Magento\Customer\Api\CustomerRepositoryInterface;
- use Magento\Framework\App\RequestInterface;
- use Magento\Framework\Exception\StateException;
- /**
- * Multishipping checkout controller
- * @SuppressWarnings(PHPMD.NumberOfChildren)
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- */
- abstract class Checkout extends \Magento\Checkout\Controller\Action implements
- \Magento\Checkout\Controller\Express\RedirectLoginInterface
- {
- /**
- * Constructor
- *
- * @param \Magento\Framework\App\Action\Context $context
- * @param \Magento\Customer\Model\Session $customerSession
- * @param CustomerRepositoryInterface $customerRepository
- * @param AccountManagementInterface $accountManagement
- */
- public function __construct(
- \Magento\Framework\App\Action\Context $context,
- \Magento\Customer\Model\Session $customerSession,
- CustomerRepositoryInterface $customerRepository,
- AccountManagementInterface $accountManagement
- ) {
- parent::__construct(
- $context,
- $customerSession,
- $customerRepository,
- $accountManagement
- );
- }
- /**
- * Retrieve checkout model
- *
- * @return \Magento\Multishipping\Model\Checkout\Type\Multishipping
- */
- protected function _getCheckout()
- {
- return $this->_objectManager->get(\Magento\Multishipping\Model\Checkout\Type\Multishipping::class);
- }
- /**
- * Retrieve checkout state model
- *
- * @return \Magento\Multishipping\Model\Checkout\Type\Multishipping\State
- */
- protected function _getState()
- {
- return $this->_objectManager->get(\Magento\Multishipping\Model\Checkout\Type\Multishipping\State::class);
- }
- /**
- * Retrieve checkout url helper
- *
- * @return \Magento\Multishipping\Helper\Url
- */
- protected function _getHelper()
- {
- return $this->_objectManager->get(\Magento\Multishipping\Helper\Url::class);
- }
- /**
- * Retrieve checkout session
- *
- * @return \Magento\Checkout\Model\Session
- */
- protected function _getCheckoutSession()
- {
- return $this->_objectManager->get(\Magento\Checkout\Model\Session::class);
- }
- /**
- * Dispatch request
- *
- * @param RequestInterface $request
- * @return \Magento\Framework\App\ResponseInterface
- * @throws \Magento\Framework\Exception\NotFoundException
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
- * @SuppressWarnings(PHPMD.NPathComplexity)
- */
- public function dispatch(RequestInterface $request)
- {
- $this->_request = $request;
- if ($this->_actionFlag->get('', 'redirectLogin')) {
- return parent::dispatch($request);
- }
- $action = $request->getActionName();
- $checkoutSessionQuote = $this->_getCheckoutSession()->getQuote();
- /**
- * Catch index action call to set some flags before checkout/type_multishipping model initialization
- */
- if ($action == 'index') {
- $checkoutSessionQuote->setIsMultiShipping(true);
- $this->_getCheckoutSession()->setCheckoutState(\Magento\Checkout\Model\Session::CHECKOUT_STATE_BEGIN);
- } elseif (!$checkoutSessionQuote->getIsMultiShipping() && !in_array(
- $action,
- ['login', 'register', 'success']
- )
- ) {
- $this->_redirect('*/*/index');
- $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
- return parent::dispatch($request);
- }
- if (!in_array($action, ['login', 'register'])) {
- $customerSession = $this->_objectManager->get(\Magento\Customer\Model\Session::class);
- if (!$customerSession->authenticate($this->_getHelper()->getMSLoginUrl())) {
- $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
- }
- if (!$this->_objectManager->get(
- \Magento\Multishipping\Helper\Data::class
- )->isMultishippingCheckoutAvailable()) {
- $error = $this->_getCheckout()->getMinimumAmountError();
- $this->messageManager->addError($error);
- $this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
- $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
- return parent::dispatch($request);
- }
- }
- $result = $this->_preDispatchValidateCustomer();
- if ($result instanceof \Magento\Framework\Controller\ResultInterface) {
- return $result;
- }
- if (!$result) {
- return $this->getResponse();
- }
- if ($this->_getCheckoutSession()->getCartWasUpdated(true)
- &&
- !in_array($action, ['index', 'login', 'register', 'addresses', 'success'])
- ) {
- $this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
- $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
- return parent::dispatch($request);
- }
- if ($action == 'success' && $this->_getCheckout()->getCheckoutSession()->getDisplaySuccess(true)) {
- return parent::dispatch($request);
- }
- try {
- $checkout = $this->_getCheckout();
- } catch (StateException $e) {
- $this->getResponse()->setRedirect($this->_getHelper()->getMSNewShippingUrl());
- $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
- return parent::dispatch($request);
- }
- $quote = $checkout->getQuote();
- if (!$quote->hasItems() || $quote->getHasError() || $quote->isVirtual()) {
- $this->getResponse()->setRedirect($this->_getHelper()->getCartUrl());
- $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
- }
- return parent::dispatch($request);
- }
- /**
- * Validate minimum amount
- *
- * @return bool
- */
- protected function _validateMinimumAmount()
- {
- if (!$this->_getCheckout()->validateMinimumAmount()) {
- $error = $this->_getCheckout()->getMinimumAmountError();
- $this->messageManager->addError($error);
- $this->_forward('backToAddresses');
- return false;
- }
- return true;
- }
- /**
- * Returns before_auth_url redirect parameter for customer session
- *
- * @return string
- */
- public function getCustomerBeforeAuthUrl()
- {
- return $this->_objectManager->create(
- \Magento\Framework\UrlInterface::class
- )->getUrl('*/*', ['_secure' => true]);
- }
- /**
- * Returns a list of action flags [flag_key] => boolean
- *
- * @return array
- */
- public function getActionFlagList()
- {
- return ['redirectLogin' => true];
- }
- /**
- * Returns login url parameter for redirect
- *
- * @return string
- */
- public function getLoginUrl()
- {
- return $this->_getHelper()->getMSLoginUrl();
- }
- /**
- * Returns action name which requires redirect
- *
- * @return string
- */
- public function getRedirectActionName()
- {
- return 'index';
- }
- }
|