Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Plugin.php
Go to the documentation of this file.
1 <?php
8 
14 
15 class Plugin
16 {
20  protected $customerSession;
21 
26 
30  protected $config;
31 
35  protected $redirector;
36 
40  private $messageManager;
41 
49  public function __construct(
51  \Magento\Wishlist\Model\AuthenticationStateInterface $authenticationState,
54  \Magento\Framework\Message\ManagerInterface $messageManager
55  ) {
56  $this->customerSession = $customerSession;
57  $this->authenticationState = $authenticationState;
58  $this->config = $config;
59  $this->redirector = $redirector;
60  $this->messageManager = $messageManager;
61  }
62 
71  public function beforeDispatch(\Magento\Framework\App\ActionInterface $subject, RequestInterface $request)
72  {
73  if ($this->authenticationState->isEnabled() && !$this->customerSession->authenticate()) {
74  $subject->getActionFlag()->set('', 'no-dispatch', true);
75  if (!$this->customerSession->getBeforeWishlistUrl()) {
76  $this->customerSession->setBeforeWishlistUrl($this->redirector->getRefererUrl());
77  }
78  $this->customerSession->setBeforeWishlistRequest($request->getParams());
79  $this->customerSession->setBeforeRequestParams($this->customerSession->getBeforeWishlistRequest());
80  $this->customerSession->setBeforeModuleName('wishlist');
81  $this->customerSession->setBeforeControllerName('index');
82  $this->customerSession->setBeforeAction('add');
83 
84  if ($request->getActionName() == 'add') {
85  $this->messageManager->addErrorMessage(__('You must login or register to add items to your wishlist.'));
86  }
87  }
88  if (!$this->config->isSetFlag('wishlist/general/active')) {
89  throw new NotFoundException(__('Page not found.'));
90  }
91  }
92 }
__construct(CustomerSession $customerSession, \Magento\Wishlist\Model\AuthenticationStateInterface $authenticationState, ScopeConfigInterface $config, RedirectInterface $redirector, \Magento\Framework\Message\ManagerInterface $messageManager)
Definition: Plugin.php:49
__()
Definition: __.php:13
beforeDispatch(\Magento\Framework\App\ActionInterface $subject, RequestInterface $request)
Definition: Plugin.php:71