Action.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Checkout\Controller;
  7. use Magento\Customer\Api\AccountManagementInterface;
  8. use Magento\Customer\Api\CustomerRepositoryInterface;
  9. use Magento\Framework\Exception\NoSuchEntityException;
  10. /**
  11. * Controller for onepage checkouts
  12. */
  13. abstract class Action extends \Magento\Framework\App\Action\Action
  14. {
  15. /**
  16. * @var \Magento\Customer\Model\Session
  17. */
  18. protected $_customerSession;
  19. /**
  20. * @var CustomerRepositoryInterface
  21. */
  22. protected $customerRepository;
  23. /**
  24. * @var AccountManagementInterface
  25. */
  26. protected $accountManagement;
  27. /**
  28. * @param \Magento\Framework\App\Action\Context $context
  29. * @param \Magento\Customer\Model\Session $customerSession
  30. * @param CustomerRepositoryInterface $customerRepository
  31. * @param AccountManagementInterface $accountManagement
  32. * @codeCoverageIgnore
  33. */
  34. public function __construct(
  35. \Magento\Framework\App\Action\Context $context,
  36. \Magento\Customer\Model\Session $customerSession,
  37. CustomerRepositoryInterface $customerRepository,
  38. AccountManagementInterface $accountManagement
  39. ) {
  40. $this->_customerSession = $customerSession;
  41. $this->customerRepository = $customerRepository;
  42. $this->accountManagement = $accountManagement;
  43. parent::__construct($context);
  44. }
  45. /**
  46. * Make sure customer is valid, if logged in
  47. *
  48. * By default will add error messages and redirect to customer edit form
  49. *
  50. * @param bool $redirect - stop dispatch and redirect?
  51. * @param bool $addErrors - add error messages?
  52. * @return bool|\Magento\Framework\Controller\Result\Redirect
  53. */
  54. protected function _preDispatchValidateCustomer($redirect = true, $addErrors = true)
  55. {
  56. try {
  57. $customer = $this->customerRepository->getById($this->_customerSession->getCustomerId());
  58. } catch (NoSuchEntityException $e) {
  59. return true;
  60. }
  61. if (isset($customer)) {
  62. $validationResult = $this->accountManagement->validate($customer);
  63. if (!$validationResult->isValid()) {
  64. if ($addErrors) {
  65. foreach ($validationResult->getMessages() as $error) {
  66. $this->messageManager->addErrorMessage($error);
  67. }
  68. }
  69. if ($redirect) {
  70. $this->_actionFlag->set('', self::FLAG_NO_DISPATCH, true);
  71. return $this->resultRedirectFactory->create()->setPath('customer/account/edit');
  72. }
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. }