Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Session.php
Go to the documentation of this file.
1 <?php
7 
18 {
22  const KEY_LENGTH = 50;
23 
27  const COOKIE_NAME = 'persistent_shopping_cart';
28 
34  protected $_unserializableFields = [
35  'persistent_id',
36  'key',
37  'customer_id',
38  'website_id',
39  'info',
40  'updated_at',
41  ];
42 
48  protected $_loadExpired = false;
49 
55  protected $_persistentData;
56 
62  protected $jsonHelper;
63 
67  protected $_coreConfig;
68 
74  protected $_storeManager;
75 
81  protected $_cookieManager;
82 
89 
93  protected $mathRandom;
94 
98  protected $sessionConfig;
99 
105  private $request;
106 
125  public function __construct(
126  \Magento\Framework\Model\Context $context,
127  \Magento\Framework\Registry $registry,
128  \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig,
129  \Magento\Framework\Json\Helper\Data $jsonHelper,
130  \Magento\Persistent\Helper\Data $persistentData,
131  \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
132  \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory,
133  \Magento\Store\Model\StoreManagerInterface $storeManager,
134  \Magento\Framework\Math\Random $mathRandom,
135  \Magento\Framework\Session\Config\ConfigInterface $sessionConfig,
136  \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
137  \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
138  array $data = []
139  ) {
140  $this->jsonHelper = $jsonHelper;
141  $this->_persistentData = $persistentData;
142  $this->_coreConfig = $coreConfig;
143  $this->_cookieManager = $cookieManager;
144  $this->_cookieMetadataFactory = $cookieMetadataFactory;
145  $this->_storeManager = $storeManager;
146  $this->sessionConfig = $sessionConfig;
147  $this->mathRandom = $mathRandom;
148  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
149  }
150 
157  protected function _construct()
158  {
159  $this->_init(\Magento\Persistent\Model\ResourceModel\Session::class);
160  }
161 
169  public function setLoadExpired($loadExpired = true)
170  {
171  $this->_loadExpired = $loadExpired;
172  return $this;
173  }
174 
181  public function getLoadExpired()
182  {
183  return $this->_loadExpired;
184  }
185 
193  public function getExpiredBefore($store = null)
194  {
195  return gmdate('Y-m-d H:i:s', time() - $this->_persistentData->getLifeTime($store));
196  }
197 
204  public function beforeSave()
205  {
206  parent::beforeSave();
207 
208  // Setting info
209  $info = [];
210  foreach ($this->getData() as $index => $value) {
211  if (!in_array($index, $this->_unserializableFields)) {
212  $info[$index] = $value;
213  }
214  }
215  $this->setInfo($this->jsonHelper->jsonEncode($info));
216 
217  if ($this->isObjectNew()) {
218  $this->setWebsiteId($this->_storeManager->getStore()->getWebsiteId());
219  // Setting cookie key
220  do {
221  $this->setKey($this->mathRandom->getRandomString(self::KEY_LENGTH));
222  } while (!$this->getResource()->isKeyAllowed($this->getKey()));
223  }
224 
225  return $this;
226  }
227 
233  protected function _afterLoad()
234  {
235  parent::_afterLoad();
236  $info = null;
237  if ($this->getInfo()) {
238  $info = $this->jsonHelper->jsonDecode($this->getInfo());
239  }
240  if (is_array($info)) {
241  foreach ($info as $key => $value) {
242  $this->setData($key, $value);
243  }
244  }
245  return $this;
246  }
247 
254  public function loadByCookieKey($key = null)
255  {
256  if (null === $key) {
257  $key = $this->_cookieManager->getCookie(self::COOKIE_NAME);
258  }
259  if ($key) {
260  $this->load($key, 'key');
261  }
262 
263  return $this;
264  }
265 
273  public function loadByCustomerId($id)
274  {
275  return $this->load($id, 'customer_id');
276  }
277 
285  public function deleteByCustomerId($customerId, $clearCookie = true)
286  {
287  if ($clearCookie) {
288  $this->removePersistentCookie();
289  }
290  $this->getResource()->deleteByCustomerId($customerId);
291  return $this;
292  }
293 
300  public function removePersistentCookie()
301  {
302  $cookieMetadata = $this->_cookieMetadataFactory->createSensitiveCookieMetadata()
303  ->setPath($this->sessionConfig->getCookiePath());
304  $this->_cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
305  return $this;
306  }
307 
316  public function setPersistentCookie($duration, $path)
317  {
318  $value = $this->getKey();
319  $this->setCookie($value, $duration, $path);
320  return $this;
321  }
322 
330  public function renewPersistentCookie($duration, $path)
331  {
332  if ($duration === null) {
333  return $this;
334  }
335  $value = $this->_cookieManager->getCookie(self::COOKIE_NAME);
336  if (null !== $value) {
337  $this->setCookie($value, $duration, $path);
338  }
339  return $this;
340  }
341 
348  public function deleteExpired($websiteId = null)
349  {
350  if ($websiteId === null) {
351  $websiteId = $this->_storeManager->getStore()->getWebsiteId();
352  }
353 
354  $lifetime = $this->_coreConfig->getValue(
355  \Magento\Persistent\Helper\Data::XML_PATH_LIFE_TIME,
356  'website',
357  intval($websiteId)
358  );
359 
360  if ($lifetime) {
361  $this->getResource()->deleteExpired($websiteId, gmdate('Y-m-d H:i:s', time() - $lifetime));
362  }
363 
364  return $this;
365  }
366 
373  public function afterDeleteCommit()
374  {
375  $this->removePersistentCookie();
376  return parent::afterDeleteCommit();
377  }
378 
387  private function setCookie($value, $duration, $path)
388  {
389  $publicCookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()
390  ->setDuration($duration)
391  ->setPath($path)
392  ->setSecure($this->getRequest()->isSecure())
393  ->setHttpOnly(true);
394  $this->_cookieManager->setPublicCookie(
395  self::COOKIE_NAME,
396  $value,
397  $publicCookieMetadata
398  );
399  }
400 
407  private function getRequest()
408  {
409  if ($this->request == null) {
411  ->get(\Magento\Framework\App\Request\Http::class);
412  }
413  return $this->request;
414  }
415 
422  public function save()
423  {
424  $this->setUpdatedAt(gmdate('Y-m-d H:i:s'));
425  return parent::save();
426  }
427 }
getData($key='', $index=null)
Definition: DataObject.php:119
$id
Definition: fieldset.phtml:14
$storeManager
$resource
Definition: bulk.php:12
deleteByCustomerId($customerId, $clearCookie=true)
Definition: Session.php:285
$value
Definition: gender.phtml:16
deleteExpired($websiteId=null)
Definition: Session.php:348
setLoadExpired($loadExpired=true)
Definition: Session.php:169
renewPersistentCookie($duration, $path)
Definition: Session.php:330
__construct(\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig, \Magento\Framework\Json\Helper\Data $jsonHelper, \Magento\Persistent\Helper\Data $persistentData, \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager, \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Framework\Math\Random $mathRandom, \Magento\Framework\Session\Config\ConfigInterface $sessionConfig, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
Definition: Session.php:125
setPersistentCookie($duration, $path)
Definition: Session.php:316
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
$index
Definition: list.phtml:44