Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cookie.php
Go to the documentation of this file.
1 <?php
7 
14 {
18  const IS_USER_ALLOWED_SAVE_COOKIE = 'user_allowed_save_cookie';
19 
23  const XML_PATH_COOKIE_RESTRICTION = 'web/cookie/cookie_restriction';
24 
28  const XML_PATH_COOKIE_RESTRICTION_LIFETIME = 'web/cookie/cookie_restriction_lifetime';
29 
33  protected $_currentStore;
34 
38  protected $_website;
39 
47  public function __construct(
48  \Magento\Framework\App\Helper\Context $context,
49  \Magento\Store\Model\StoreManagerInterface $storeManager,
50  array $data = []
51  ) {
52  parent::__construct($context);
53  $this->_currentStore = isset($data['current_store']) ? $data['current_store'] : $storeManager->getStore();
54 
55  if (!$this->_currentStore instanceof \Magento\Store\Model\Store) {
56  throw new \InvalidArgumentException('Required store object is invalid');
57  }
58 
59  $this->_website = isset($data['website']) ? $data['website'] : $storeManager->getWebsite();
60 
61  if (!$this->_website instanceof \Magento\Store\Model\Website) {
62  throw new \InvalidArgumentException('Required website object is invalid');
63  }
64  }
65 
71  public function isUserNotAllowSaveCookie()
72  {
73  $acceptedSaveCookiesWebsites = $this->_getAcceptedSaveCookiesWebsites();
74  return $this->isCookieRestrictionModeEnabled() &&
75  empty($acceptedSaveCookiesWebsites[$this->_website->getId()]);
76  }
77 
85  {
86  return $this->scopeConfig->getValue(
87  self::XML_PATH_COOKIE_RESTRICTION,
88  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
89  $this->_currentStore
90  );
91  }
92 
99  {
100  $acceptedSaveCookiesWebsites = $this->_getAcceptedSaveCookiesWebsites();
101  $acceptedSaveCookiesWebsites[(int)$this->_website->getId()] = 1;
102  return json_encode($acceptedSaveCookiesWebsites);
103  }
104 
110  protected function _getAcceptedSaveCookiesWebsites()
111  {
112  $unSerializedList = null;
113  $serializedList = $this->_request->getCookie(self::IS_USER_ALLOWED_SAVE_COOKIE, false);
114  if ($serializedList) {
115  $unSerializedList = json_decode($serializedList, true);
116  }
117  return is_array($unSerializedList) ? $unSerializedList : [];
118  }
119 
126  {
127  return (int)$this->scopeConfig->getValue(
128  self::XML_PATH_COOKIE_RESTRICTION_LIFETIME,
129  \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
130  $this->_currentStore
131  );
132  }
133 }
$storeManager