Plugin.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Wishlist\Controller\Index;
  8. use Magento\Customer\Model\Session as CustomerSession;
  9. use Magento\Framework\Exception\NotFoundException;
  10. use Magento\Framework\App\Config\ScopeConfigInterface;
  11. use Magento\Framework\App\RequestInterface;
  12. use Magento\Framework\App\Response\RedirectInterface;
  13. class Plugin
  14. {
  15. /**
  16. * @var \Magento\Customer\Model\Session
  17. */
  18. protected $customerSession;
  19. /**
  20. * @var \Magento\Wishlist\Model\AuthenticationStateInterface
  21. */
  22. protected $authenticationState;
  23. /**
  24. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  25. */
  26. protected $config;
  27. /**
  28. * @var \Magento\Framework\App\Response\RedirectInterface
  29. */
  30. protected $redirector;
  31. /**
  32. * @var \Magento\Framework\Message\ManagerInterface
  33. */
  34. private $messageManager;
  35. /**
  36. * @param CustomerSession $customerSession
  37. * @param \Magento\Wishlist\Model\AuthenticationStateInterface $authenticationState
  38. * @param ScopeConfigInterface $config
  39. * @param RedirectInterface $redirector
  40. * @param \Magento\Framework\Message\ManagerInterface $messageManager
  41. */
  42. public function __construct(
  43. CustomerSession $customerSession,
  44. \Magento\Wishlist\Model\AuthenticationStateInterface $authenticationState,
  45. ScopeConfigInterface $config,
  46. RedirectInterface $redirector,
  47. \Magento\Framework\Message\ManagerInterface $messageManager
  48. ) {
  49. $this->customerSession = $customerSession;
  50. $this->authenticationState = $authenticationState;
  51. $this->config = $config;
  52. $this->redirector = $redirector;
  53. $this->messageManager = $messageManager;
  54. }
  55. /**
  56. * Perform customer authentication and wishlist feature state checks
  57. *
  58. * @param \Magento\Framework\App\ActionInterface $subject
  59. * @param RequestInterface $request
  60. * @return void
  61. * @throws \Magento\Framework\Exception\NotFoundException
  62. */
  63. public function beforeDispatch(\Magento\Framework\App\ActionInterface $subject, RequestInterface $request)
  64. {
  65. if ($this->authenticationState->isEnabled() && !$this->customerSession->authenticate()) {
  66. $subject->getActionFlag()->set('', 'no-dispatch', true);
  67. if (!$this->customerSession->getBeforeWishlistUrl()) {
  68. $this->customerSession->setBeforeWishlistUrl($this->redirector->getRefererUrl());
  69. }
  70. $this->customerSession->setBeforeWishlistRequest($request->getParams());
  71. $this->customerSession->setBeforeRequestParams($this->customerSession->getBeforeWishlistRequest());
  72. $this->customerSession->setBeforeModuleName('wishlist');
  73. $this->customerSession->setBeforeControllerName('index');
  74. $this->customerSession->setBeforeAction('add');
  75. if ($request->getActionName() == 'add') {
  76. $this->messageManager->addErrorMessage(__('You must login or register to add items to your wishlist.'));
  77. }
  78. }
  79. if (!$this->config->isSetFlag('wishlist/general/active')) {
  80. throw new NotFoundException(__('Page not found.'));
  81. }
  82. }
  83. }