Data.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Persistent Shopping Cart Data Helper
  8. */
  9. namespace Magento\Persistent\Helper;
  10. use Magento\Framework\Module\Dir;
  11. use Magento\Store\Model\ScopeInterface;
  12. /**
  13. * Helper for persistence
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Data extends \Magento\Framework\App\Helper\AbstractHelper
  19. {
  20. const XML_PATH_ENABLED = 'persistent/options/enabled';
  21. const XML_PATH_LIFE_TIME = 'persistent/options/lifetime';
  22. const XML_PATH_LOGOUT_CLEAR = 'persistent/options/logout_clear';
  23. const XML_PATH_REMEMBER_ME_ENABLED = 'persistent/options/remember_enabled';
  24. const XML_PATH_REMEMBER_ME_DEFAULT = 'persistent/options/remember_default';
  25. const XML_PATH_PERSIST_SHOPPING_CART = 'persistent/options/shopping_cart';
  26. /**
  27. * Name of config file
  28. *
  29. * @var string
  30. */
  31. protected $_configFileName = 'persistent.xml';
  32. /**
  33. * @var \Magento\Framework\Escaper
  34. */
  35. protected $_escaper;
  36. /**
  37. * @var \Magento\Framework\Module\Dir\Reader
  38. */
  39. protected $_modulesReader;
  40. /**
  41. * @param \Magento\Framework\App\Helper\Context $context
  42. * @param \Magento\Framework\Module\Dir\Reader $modulesReader
  43. * @param \Magento\Framework\Escaper $escaper
  44. */
  45. public function __construct(
  46. \Magento\Framework\App\Helper\Context $context,
  47. \Magento\Framework\Module\Dir\Reader $modulesReader,
  48. \Magento\Framework\Escaper $escaper
  49. ) {
  50. $this->_modulesReader = $modulesReader;
  51. $this->_escaper = $escaper;
  52. parent::__construct(
  53. $context
  54. );
  55. }
  56. /**
  57. * Checks whether Persistence Functionality is enabled
  58. *
  59. * @param int|string|\Magento\Store\Model\Store $store
  60. * @return bool
  61. * @codeCoverageIgnore
  62. */
  63. public function isEnabled($store = null)
  64. {
  65. return $this->scopeConfig->isSetFlag(
  66. self::XML_PATH_ENABLED,
  67. ScopeInterface::SCOPE_STORE,
  68. $store
  69. );
  70. }
  71. /**
  72. * Checks whether "Remember Me" enabled
  73. *
  74. * @param int|string|\Magento\Store\Model\Store $store
  75. * @return bool
  76. * @codeCoverageIgnore
  77. */
  78. public function isRememberMeEnabled($store = null)
  79. {
  80. return $this->scopeConfig->isSetFlag(
  81. self::XML_PATH_REMEMBER_ME_ENABLED,
  82. ScopeInterface::SCOPE_STORE,
  83. $store
  84. );
  85. }
  86. /**
  87. * Is "Remember Me" checked by default
  88. *
  89. * @param int|string|\Magento\Store\Model\Store $store
  90. * @return bool
  91. * @codeCoverageIgnore
  92. */
  93. public function isRememberMeCheckedDefault($store = null)
  94. {
  95. return $this->scopeConfig->isSetFlag(
  96. self::XML_PATH_REMEMBER_ME_DEFAULT,
  97. ScopeInterface::SCOPE_STORE,
  98. $store
  99. );
  100. }
  101. /**
  102. * Is shopping cart persist
  103. *
  104. * @param int|string|\Magento\Store\Model\Store $store
  105. * @return bool
  106. * @codeCoverageIgnore
  107. */
  108. public function isShoppingCartPersist($store = null)
  109. {
  110. return $this->scopeConfig->isSetFlag(
  111. self::XML_PATH_PERSIST_SHOPPING_CART,
  112. ScopeInterface::SCOPE_STORE,
  113. $store
  114. );
  115. }
  116. /**
  117. * Get Persistence Lifetime
  118. *
  119. * @param int|string|\Magento\Store\Model\Store $store
  120. * @return int
  121. */
  122. public function getLifeTime($store = null)
  123. {
  124. $lifeTime = (int)$this->scopeConfig->getValue(
  125. self::XML_PATH_LIFE_TIME,
  126. ScopeInterface::SCOPE_STORE,
  127. $store
  128. );
  129. return $lifeTime < 0 ? 0 : $lifeTime;
  130. }
  131. /**
  132. * Check if set `Clear on Logout` in config settings
  133. *
  134. * @return bool
  135. * @SuppressWarnings(PHPMD.BooleanGetMethodName)
  136. * @codeCoverageIgnore
  137. */
  138. public function getClearOnLogout()
  139. {
  140. return $this->scopeConfig->isSetFlag(
  141. self::XML_PATH_LOGOUT_CLEAR,
  142. ScopeInterface::SCOPE_STORE
  143. );
  144. }
  145. /**
  146. * Retrieve url for unset long-term cookie
  147. *
  148. * @return string
  149. * @codeCoverageIgnore
  150. */
  151. public function getUnsetCookieUrl()
  152. {
  153. return $this->_getUrl('persistent/index/unsetCookie');
  154. }
  155. /**
  156. * Retrieve path for config file
  157. *
  158. * @return string
  159. */
  160. public function getPersistentConfigFilePath()
  161. {
  162. return $this->_modulesReader->getModuleDir(Dir::MODULE_ETC_DIR, $this->_getModuleName())
  163. . '/' . $this->_configFileName;
  164. }
  165. /**
  166. * Check whether specified action should be processed
  167. *
  168. * @param \Magento\Framework\Event\Observer $observer
  169. * @return bool
  170. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  171. * @codeCoverageIgnore
  172. */
  173. public function canProcess($observer)
  174. {
  175. return true;
  176. }
  177. }