RenewCookieObserver.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Persistent\Observer;
  7. use Magento\Framework\Event\Observer;
  8. use Magento\Framework\Event\ObserverInterface;
  9. /**
  10. * Persistent Session Observer
  11. */
  12. class RenewCookieObserver implements ObserverInterface
  13. {
  14. /**
  15. * Persistent session
  16. *
  17. * @var \Magento\Persistent\Helper\Session
  18. */
  19. protected $_persistentSession;
  20. /**
  21. * Persistent data
  22. *
  23. * @var \Magento\Persistent\Helper\Data
  24. */
  25. protected $_persistentData = null;
  26. /**
  27. * Session factory
  28. *
  29. * @var \Magento\Persistent\Model\SessionFactory
  30. */
  31. protected $_sessionFactory;
  32. /**
  33. * Customer session
  34. *
  35. * @var \Magento\Customer\Model\Session
  36. */
  37. protected $_customerSession;
  38. /**
  39. * Constructor
  40. *
  41. * @param \Magento\Persistent\Helper\Data $persistentData
  42. * @param \Magento\Persistent\Helper\Session $persistentSession
  43. * @param \Magento\Customer\Model\Session $customerSession
  44. * @param \Magento\Persistent\Model\SessionFactory $sessionFactory
  45. */
  46. public function __construct(
  47. \Magento\Persistent\Helper\Data $persistentData,
  48. \Magento\Persistent\Helper\Session $persistentSession,
  49. \Magento\Customer\Model\Session $customerSession,
  50. \Magento\Persistent\Model\SessionFactory $sessionFactory
  51. ) {
  52. $this->_persistentData = $persistentData;
  53. $this->_persistentSession = $persistentSession;
  54. $this->_customerSession = $customerSession;
  55. $this->_sessionFactory = $sessionFactory;
  56. }
  57. /**
  58. * Renew persistent cookie
  59. *
  60. * @param Observer $observer
  61. * @return void
  62. */
  63. public function execute(Observer $observer)
  64. {
  65. if (!$this->_persistentData->canProcess($observer) || !$this->_persistentData->isEnabled()
  66. || !$this->_persistentSession->isPersistent()
  67. ) {
  68. return;
  69. }
  70. /** @var $request \Magento\Framework\App\RequestInterface */
  71. $request = $observer->getEvent()->getRequest();
  72. if ($this->_customerSession->isLoggedIn() || $request->getFullActionName() == 'customer_account_logout') {
  73. $this->_sessionFactory->create()
  74. ->renewPersistentCookie(
  75. $this->_persistentData->getLifeTime(),
  76. $this->_customerSession->getCookiePath()
  77. );
  78. }
  79. }
  80. }