Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Member Functions | Protected Attributes
Session Class Reference
Inheritance diagram for Session:
AbstractModel DataObject

Public Member Functions

 __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=[])
 
 setLoadExpired ($loadExpired=true)
 
 getLoadExpired ()
 
 getExpiredBefore ($store=null)
 
 beforeSave ()
 
 loadByCookieKey ($key=null)
 
 loadByCustomerId ($id)
 
 deleteByCustomerId ($customerId, $clearCookie=true)
 
 removePersistentCookie ()
 
 setPersistentCookie ($duration, $path)
 
 renewPersistentCookie ($duration, $path)
 
 deleteExpired ($websiteId=null)
 
 afterDeleteCommit ()
 
 save ()
 
- Public Member Functions inherited from AbstractModel
 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $data=[])
 
 __sleep ()
 
 __wakeup ()
 
 setIdFieldName ($name)
 
 getIdFieldName ()
 
 getId ()
 
 setId ($value)
 
 isDeleted ($isDeleted=null)
 
 hasDataChanges ()
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 setDataChanges ($value)
 
 getOrigData ($key=null)
 
 setOrigData ($key=null, $data=null)
 
 dataHasChangedFor ($field)
 
 getResourceName ()
 
 getResourceCollection ()
 
 getCollection ()
 
 load ($modelId, $field=null)
 
 beforeLoad ($identifier, $field=null)
 
 afterLoad ()
 
 isSaveAllowed ()
 
 setHasDataChanges ($flag)
 
 save ()
 
 afterCommitCallback ()
 
 isObjectNew ($flag=null)
 
 beforeSave ()
 
 validateBeforeSave ()
 
 getCacheTags ()
 
 cleanModelCache ()
 
 afterSave ()
 
 delete ()
 
 beforeDelete ()
 
 afterDelete ()
 
 afterDeleteCommit ()
 
 getResource ()
 
 getEntityId ()
 
 setEntityId ($entityId)
 
 clearInstance ()
 
 getStoredData ()
 
 getEventPrefix ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 

Data Fields

const KEY_LENGTH = 50
 
const COOKIE_NAME = 'persistent_shopping_cart'
 

Protected Member Functions

 _construct ()
 
 _afterLoad ()
 
- Protected Member Functions inherited from AbstractModel
 _construct ()
 
 _init ($resourceModel)
 
 _setResourceModel ($resourceName, $collectionName=null)
 
 _getResource ()
 
 _getEventData ()
 
 _beforeLoad ($modelId, $field=null)
 
 _afterLoad ()
 
 _hasModelChanged ()
 
 _getValidatorBeforeSave ()
 
 _createValidatorBeforeSave ()
 
 _getValidationRulesBeforeSave ()
 
 _clearReferences ()
 
 _clearData ()
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $_unserializableFields
 
 $_loadExpired = false
 
 $_persistentData
 
 $jsonHelper
 
 $_coreConfig
 
 $_storeManager
 
 $_cookieManager
 
 $_cookieMetadataFactory
 
 $mathRandom
 
 $sessionConfig
 
- Protected Attributes inherited from AbstractModel
 $_eventPrefix = 'core_abstract'
 
 $_eventObject = 'object'
 
 $_idFieldName = 'id'
 
 $_hasDataChanges = false
 
 $_origData
 
 $_isDeleted = false
 
 $_resource
 
 $_resourceCollection
 
 $_resourceName
 
 $_collectionName
 
 $_cacheTag = false
 
 $_dataSaveAllowed = true
 
 $_isObjectNew = null
 
 $_validatorBeforeSave = null
 
 $_eventManager
 
 $_cacheManager
 
 $_registry
 
 $_logger
 
 $_appState
 
 $_actionValidator
 
 $storedData = []
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Persistent Session Model

@api @method int getCustomerId() @method Session setCustomerId() @SuppressWarnings(PHPMD.CouplingBetweenObjects)

Since
100.0.2

Definition at line 17 of file Session.php.

Constructor & Destructor Documentation

◆ __construct()

Constructor

Parameters
\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
\Magento\Framework\Data\Collection\AbstractDb$resourceCollection
array$data@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 125 of file Session.php.

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  }
$storeManager
$resource
Definition: bulk.php:12

Member Function Documentation

◆ _afterLoad()

_afterLoad ( )
protected

Set model data from info field

Returns
$this

Definition at line 233 of file Session.php.

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  }
$value
Definition: gender.phtml:16
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52

◆ _construct()

_construct ( )
protected

Define resource model

Returns
void @codeCoverageIgnore

Definition at line 157 of file Session.php.

158  {
159  $this->_init(\Magento\Persistent\Model\ResourceModel\Session::class);
160  }

◆ afterDeleteCommit()

afterDeleteCommit ( )

Delete 'persistent' cookie

Returns
$this @codeCoverageIgnore

Definition at line 373 of file Session.php.

374  {
375  $this->removePersistentCookie();
376  return parent::afterDeleteCommit();
377  }

◆ beforeSave()

beforeSave ( )

Serialize info for Resource Model to save For new model check and set available cookie key

Returns
$this

Definition at line 204 of file Session.php.

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  }
getData($key='', $index=null)
Definition: DataObject.php:119
$value
Definition: gender.phtml:16
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
$index
Definition: list.phtml:44

◆ deleteByCustomerId()

deleteByCustomerId (   $customerId,
  $clearCookie = true 
)

Delete customer persistent session by customer id

Parameters
int$customerId
bool$clearCookie
Returns
$this

Definition at line 285 of file Session.php.

286  {
287  if ($clearCookie) {
288  $this->removePersistentCookie();
289  }
290  $this->getResource()->deleteByCustomerId($customerId);
291  return $this;
292  }

◆ deleteExpired()

deleteExpired (   $websiteId = null)

Delete expired persistent sessions for the website

Parameters
null | int$websiteId
Returns
$this

Definition at line 348 of file Session.php.

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  }

◆ getExpiredBefore()

getExpiredBefore (   $store = null)

Get date-time before which persistent session is expired

Parameters
int | string | \Magento\Store\Model\Store$store
Returns
string @codeCoverageIgnore

Definition at line 193 of file Session.php.

194  {
195  return gmdate('Y-m-d H:i:s', time() - $this->_persistentData->getLifeTime($store));
196  }

◆ getLoadExpired()

getLoadExpired ( )

Get if model loads expired sessions

Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 181 of file Session.php.

182  {
183  return $this->_loadExpired;
184  }

◆ loadByCookieKey()

loadByCookieKey (   $key = null)

Get persistent session by cookie key

Parameters
string$key
Returns
$this

Definition at line 254 of file Session.php.

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  }

◆ loadByCustomerId()

loadByCustomerId (   $id)

Load session model by specified customer id

Parameters
int$id
Returns
$this @codeCoverageIgnore

Definition at line 273 of file Session.php.

274  {
275  return $this->load($id, 'customer_id');
276  }
$id
Definition: fieldset.phtml:14

◆ removePersistentCookie()

removePersistentCookie ( )

Remove persistent cookie

Returns
$this @api

Definition at line 300 of file Session.php.

301  {
302  $cookieMetadata = $this->_cookieMetadataFactory->createSensitiveCookieMetadata()
303  ->setPath($this->sessionConfig->getCookiePath());
304  $this->_cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
305  return $this;
306  }

◆ renewPersistentCookie()

renewPersistentCookie (   $duration,
  $path 
)

Postpone cookie expiration time if cookie value defined

Parameters
int$durationTime in seconds.
string$path
Returns
$this

Definition at line 330 of file Session.php.

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  }
$value
Definition: gender.phtml:16

◆ save()

save ( )

Set updated_at to be always changed

Returns
$this
Since
100.1.0

Definition at line 422 of file Session.php.

423  {
424  $this->setUpdatedAt(gmdate('Y-m-d H:i:s'));
425  return parent::save();
426  }

◆ setLoadExpired()

setLoadExpired (   $loadExpired = true)

Set if load expired persistent session

Parameters
bool$loadExpired
Returns
$this @codeCoverageIgnore

Definition at line 169 of file Session.php.

170  {
171  $this->_loadExpired = $loadExpired;
172  return $this;
173  }

◆ setPersistentCookie()

setPersistentCookie (   $duration,
  $path 
)

Set persistent cookie

Parameters
int$durationTime in seconds.
string$path
Returns
$this @api

Definition at line 316 of file Session.php.

317  {
318  $value = $this->getKey();
319  $this->setCookie($value, $duration, $path);
320  return $this;
321  }
$value
Definition: gender.phtml:16

Field Documentation

◆ $_cookieManager

$_cookieManager
protected

Definition at line 81 of file Session.php.

◆ $_cookieMetadataFactory

$_cookieMetadataFactory
protected

Definition at line 88 of file Session.php.

◆ $_coreConfig

$_coreConfig
protected

Definition at line 67 of file Session.php.

◆ $_loadExpired

$_loadExpired = false
protected

Definition at line 48 of file Session.php.

◆ $_persistentData

$_persistentData
protected

Definition at line 55 of file Session.php.

◆ $_storeManager

$_storeManager
protected

Definition at line 74 of file Session.php.

◆ $_unserializableFields

$_unserializableFields
protected
Initial value:
= [
'persistent_id',
'key',
'customer_id',
'website_id',
'info',
'updated_at',
]

Definition at line 34 of file Session.php.

◆ $jsonHelper

$jsonHelper
protected

Definition at line 62 of file Session.php.

◆ $mathRandom

$mathRandom
protected

Definition at line 93 of file Session.php.

◆ $sessionConfig

$sessionConfig
protected

Definition at line 98 of file Session.php.

◆ COOKIE_NAME

const COOKIE_NAME = 'persistent_shopping_cart'

Persistent cookie name

Definition at line 27 of file Session.php.

◆ KEY_LENGTH

const KEY_LENGTH = 50

Persistent cookie key length

Definition at line 22 of file Session.php.


The documentation for this class was generated from the following file: