ContextPlugin.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\App\Action;
  7. use Magento\Customer\Model\Context;
  8. use Magento\Customer\Model\GroupManagement;
  9. use Magento\Framework\App\Action\AbstractAction;
  10. use Magento\Framework\App\RequestInterface;
  11. use Magento\Customer\Model\Session;
  12. use Magento\Framework\App\Http\Context as HttpContext;
  13. /**
  14. * Class ContextPlugin
  15. */
  16. class ContextPlugin
  17. {
  18. /**
  19. * @var Session
  20. */
  21. protected $customerSession;
  22. /**
  23. * @var HttpContext
  24. */
  25. protected $httpContext;
  26. /**
  27. * @param Session $customerSession
  28. * @param HttpContext $httpContext
  29. */
  30. public function __construct(Session $customerSession, HttpContext $httpContext)
  31. {
  32. $this->customerSession = $customerSession;
  33. $this->httpContext = $httpContext;
  34. }
  35. /**
  36. * Set customer group and customer session id to HTTP context
  37. *
  38. * @param AbstractAction $subject
  39. * @param RequestInterface $request
  40. * @return void
  41. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  42. */
  43. public function beforeDispatch(AbstractAction $subject, RequestInterface $request)
  44. {
  45. $this->httpContext->setValue(
  46. Context::CONTEXT_GROUP,
  47. $this->customerSession->getCustomerGroupId(),
  48. GroupManagement::NOT_LOGGED_IN_ID
  49. );
  50. $this->httpContext->setValue(
  51. Context::CONTEXT_AUTH,
  52. $this->customerSession->isLoggedIn(),
  53. false
  54. );
  55. }
  56. }