Cookie.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cookie\Helper;
  7. /**
  8. * Cookie helper
  9. * @api
  10. * @since 100.0.2
  11. */
  12. class Cookie extends \Magento\Framework\App\Helper\AbstractHelper
  13. {
  14. /**
  15. * Cookie name for users who allowed cookie save
  16. */
  17. const IS_USER_ALLOWED_SAVE_COOKIE = 'user_allowed_save_cookie';
  18. /**
  19. * Path to configuration, check is enable cookie restriction mode
  20. */
  21. const XML_PATH_COOKIE_RESTRICTION = 'web/cookie/cookie_restriction';
  22. /**
  23. * Cookie restriction lifetime configuration path
  24. */
  25. const XML_PATH_COOKIE_RESTRICTION_LIFETIME = 'web/cookie/cookie_restriction_lifetime';
  26. /**
  27. * @var \Magento\Store\Model\Store
  28. */
  29. protected $_currentStore;
  30. /**
  31. * @var \Magento\Store\Model\Website
  32. */
  33. protected $_website;
  34. /**
  35. * @param \Magento\Framework\App\Helper\Context $context
  36. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  37. * @param array $data
  38. *
  39. * @throws \Magento\Framework\Exception\LocalizedException
  40. * @throws \Magento\Framework\Exception\NoSuchEntityException
  41. */
  42. public function __construct(
  43. \Magento\Framework\App\Helper\Context $context,
  44. \Magento\Store\Model\StoreManagerInterface $storeManager,
  45. array $data = []
  46. ) {
  47. parent::__construct($context);
  48. $this->_currentStore = isset($data['current_store']) ? $data['current_store'] : $storeManager->getStore();
  49. if (!$this->_currentStore instanceof \Magento\Store\Model\Store) {
  50. throw new \InvalidArgumentException('Required store object is invalid');
  51. }
  52. $this->_website = isset($data['website']) ? $data['website'] : $storeManager->getWebsite();
  53. if (!$this->_website instanceof \Magento\Store\Model\Website) {
  54. throw new \InvalidArgumentException('Required website object is invalid');
  55. }
  56. }
  57. /**
  58. * Check if cookie restriction notice should be displayed
  59. *
  60. * @return bool
  61. */
  62. public function isUserNotAllowSaveCookie()
  63. {
  64. $acceptedSaveCookiesWebsites = $this->_getAcceptedSaveCookiesWebsites();
  65. return $this->isCookieRestrictionModeEnabled() &&
  66. empty($acceptedSaveCookiesWebsites[$this->_website->getId()]);
  67. }
  68. /**
  69. * Check if cookie restriction mode is enabled for this store
  70. *
  71. * @return bool
  72. * @since 100.1.3
  73. */
  74. public function isCookieRestrictionModeEnabled()
  75. {
  76. return $this->scopeConfig->getValue(
  77. self::XML_PATH_COOKIE_RESTRICTION,
  78. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  79. $this->_currentStore
  80. );
  81. }
  82. /**
  83. * Return serialized list of accepted save cookie website
  84. *
  85. * @return string
  86. */
  87. public function getAcceptedSaveCookiesWebsiteIds()
  88. {
  89. $acceptedSaveCookiesWebsites = $this->_getAcceptedSaveCookiesWebsites();
  90. $acceptedSaveCookiesWebsites[(int)$this->_website->getId()] = 1;
  91. return json_encode($acceptedSaveCookiesWebsites);
  92. }
  93. /**
  94. * Get accepted save cookies websites
  95. *
  96. * @return array
  97. */
  98. protected function _getAcceptedSaveCookiesWebsites()
  99. {
  100. $unSerializedList = null;
  101. $serializedList = $this->_request->getCookie(self::IS_USER_ALLOWED_SAVE_COOKIE, false);
  102. if ($serializedList) {
  103. $unSerializedList = json_decode($serializedList, true);
  104. }
  105. return is_array($unSerializedList) ? $unSerializedList : [];
  106. }
  107. /**
  108. * Get cookie restriction lifetime (in seconds)
  109. *
  110. * @return int
  111. */
  112. public function getCookieRestrictionLifetime()
  113. {
  114. return (int)$this->scopeConfig->getValue(
  115. self::XML_PATH_COOKIE_RESTRICTION_LIFETIME,
  116. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  117. $this->_currentStore
  118. );
  119. }
  120. }