CustomerFlushFormKey.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\Plugin;
  7. use Magento\Customer\Model\Session;
  8. use Magento\Framework\Data\Form\FormKey as DataFormKey;
  9. use Magento\PageCache\Observer\FlushFormKey;
  10. class CustomerFlushFormKey
  11. {
  12. /**
  13. * @var Session
  14. */
  15. private $session;
  16. /**
  17. * @var DataFormKey
  18. */
  19. private $dataFormKey;
  20. /**
  21. * Initialize dependencies.
  22. *
  23. * @param Session $session
  24. * @param DataFormKey $dataFormKey
  25. */
  26. public function __construct(Session $session, DataFormKey $dataFormKey)
  27. {
  28. $this->session = $session;
  29. $this->dataFormKey = $dataFormKey;
  30. }
  31. /**
  32. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  33. * @param FlushFormKey $subject
  34. * @param callable $proceed
  35. * @param $args
  36. */
  37. public function aroundExecute(FlushFormKey $subject, callable $proceed, ...$args)
  38. {
  39. $currentFormKey = $this->dataFormKey->getFormKey();
  40. $proceed(...$args);
  41. $beforeParams = $this->session->getBeforeRequestParams();
  42. if (isset($beforeParams['form_key']) && $beforeParams['form_key'] === $currentFormKey) {
  43. $beforeParams['form_key'] = $this->dataFormKey->getFormKey();
  44. $this->session->setBeforeRequestParams($beforeParams);
  45. }
  46. }
  47. }