123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Persistent\Model;
- /**
- * Persistent Session Model
- *
- * @api
- * @method int getCustomerId()
- * @method Session setCustomerId()
- * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
- * @since 100.0.2
- */
- class Session extends \Magento\Framework\Model\AbstractModel
- {
- /**
- * Persistent cookie key length
- */
- const KEY_LENGTH = 50;
- /**
- * Persistent cookie name
- */
- const COOKIE_NAME = 'persistent_shopping_cart';
- /**
- * Fields which model does not save into `info` db field
- *
- * @var string[]
- */
- protected $_unserializableFields = [
- 'persistent_id',
- 'key',
- 'customer_id',
- 'website_id',
- 'info',
- 'updated_at',
- ];
- /**
- * If model loads expired sessions
- *
- * @var bool
- */
- protected $_loadExpired = false;
- /**
- * Persistent data
- *
- * @var \Magento\Persistent\Helper\Data
- */
- protected $_persistentData;
- /**
- * Json Helper
- *
- * @var \Magento\Framework\Json\Helper\Data
- */
- protected $jsonHelper;
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
- */
- protected $_coreConfig;
- /**
- * Store manager
- *
- * @var \Magento\Store\Model\StoreManagerInterface
- */
- protected $_storeManager;
- /**
- * Cookie manager
- *
- * @var \Magento\Framework\Stdlib\CookieManagerInterface
- */
- protected $_cookieManager;
- /**
- * Cookie metadata factory
- *
- * @var \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory
- */
- protected $_cookieMetadataFactory;
- /**
- * @var \Magento\Framework\Math\Random
- */
- protected $mathRandom;
- /**
- * @var \Magento\Framework\Session\Config\ConfigInterface
- */
- protected $sessionConfig;
- /**
- * Request
- *
- * @var \Magento\Framework\App\Request\Http
- */
- private $request;
- /**
- * Constructor
- *
- * @param \Magento\Framework\Model\Context $context
- * @param \Magento\Framework\Registry $registry
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig
- * @param \Magento\Framework\Json\Helper\Data $jsonHelper
- * @param \Magento\Persistent\Helper\Data $persistentData
- * @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
- * @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
- * @param \Magento\Store\Model\StoreManagerInterface $storeManager
- * @param \Magento\Framework\Math\Random $mathRandom
- * @param \Magento\Framework\Session\Config\ConfigInterface $sessionConfig
- * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
- * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
- * @param array $data
- * @SuppressWarnings(PHPMD.ExcessiveParameterList)
- */
- public function __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 = []
- ) {
- $this->jsonHelper = $jsonHelper;
- $this->_persistentData = $persistentData;
- $this->_coreConfig = $coreConfig;
- $this->_cookieManager = $cookieManager;
- $this->_cookieMetadataFactory = $cookieMetadataFactory;
- $this->_storeManager = $storeManager;
- $this->sessionConfig = $sessionConfig;
- $this->mathRandom = $mathRandom;
- parent::__construct($context, $registry, $resource, $resourceCollection, $data);
- }
- /**
- * Define resource model
- *
- * @return void
- * @codeCoverageIgnore
- */
- protected function _construct()
- {
- $this->_init(\Magento\Persistent\Model\ResourceModel\Session::class);
- }
- /**
- * Set if load expired persistent session
- *
- * @param bool $loadExpired
- * @return $this
- * @codeCoverageIgnore
- */
- public function setLoadExpired($loadExpired = true)
- {
- $this->_loadExpired = $loadExpired;
- return $this;
- }
- /**
- * Get if model loads expired sessions
- *
- * @return bool
- * @SuppressWarnings(PHPMD.BooleanGetMethodName)
- */
- public function getLoadExpired()
- {
- return $this->_loadExpired;
- }
- /**
- * Get date-time before which persistent session is expired
- *
- * @param int|string|\Magento\Store\Model\Store $store
- * @return string
- * @codeCoverageIgnore
- */
- public function getExpiredBefore($store = null)
- {
- return gmdate('Y-m-d H:i:s', time() - $this->_persistentData->getLifeTime($store));
- }
- /**
- * Serialize info for Resource Model to save
- *
- * For new model check and set available cookie key
- *
- * @return $this
- */
- public function beforeSave()
- {
- parent::beforeSave();
- // Setting info
- $info = [];
- foreach ($this->getData() as $index => $value) {
- if (!in_array($index, $this->_unserializableFields)) {
- $info[$index] = $value;
- }
- }
- $this->setInfo($this->jsonHelper->jsonEncode($info));
- if ($this->isObjectNew()) {
- $this->setWebsiteId($this->_storeManager->getStore()->getWebsiteId());
- // Setting cookie key
- do {
- $this->setKey($this->mathRandom->getRandomString(self::KEY_LENGTH));
- } while (!$this->getResource()->isKeyAllowed($this->getKey()));
- }
- return $this;
- }
- /**
- * Set model data from info field
- *
- * @return $this
- */
- protected function _afterLoad()
- {
- parent::_afterLoad();
- $info = null;
- if ($this->getInfo()) {
- $info = $this->jsonHelper->jsonDecode($this->getInfo());
- }
- if (is_array($info)) {
- foreach ($info as $key => $value) {
- $this->setData($key, $value);
- }
- }
- return $this;
- }
- /**
- * Get persistent session by cookie key
- *
- * @param string $key
- * @return $this
- */
- public function loadByCookieKey($key = null)
- {
- if (null === $key) {
- $key = $this->_cookieManager->getCookie(self::COOKIE_NAME);
- }
- if ($key) {
- $this->load($key, 'key');
- }
- return $this;
- }
- /**
- * Load session model by specified customer id
- *
- * @param int $id
- * @return $this
- * @codeCoverageIgnore
- */
- public function loadByCustomerId($id)
- {
- return $this->load($id, 'customer_id');
- }
- /**
- * Delete customer persistent session by customer id
- *
- * @param int $customerId
- * @param bool $clearCookie
- * @return $this
- */
- public function deleteByCustomerId($customerId, $clearCookie = true)
- {
- if ($clearCookie) {
- $this->removePersistentCookie();
- }
- $this->getResource()->deleteByCustomerId($customerId);
- return $this;
- }
- /**
- * Remove persistent cookie
- *
- * @return $this
- * @api
- */
- public function removePersistentCookie()
- {
- $cookieMetadata = $this->_cookieMetadataFactory->createSensitiveCookieMetadata()
- ->setPath($this->sessionConfig->getCookiePath());
- $this->_cookieManager->deleteCookie(self::COOKIE_NAME, $cookieMetadata);
- return $this;
- }
- /**
- * Set persistent cookie
- *
- * @param int $duration Time in seconds.
- * @param string $path
- * @return $this
- * @api
- */
- public function setPersistentCookie($duration, $path)
- {
- $value = $this->getKey();
- $this->setCookie($value, $duration, $path);
- return $this;
- }
- /**
- * Postpone cookie expiration time if cookie value defined
- *
- * @param int $duration Time in seconds.
- * @param string $path
- * @return $this
- */
- public function renewPersistentCookie($duration, $path)
- {
- if ($duration === null) {
- return $this;
- }
- $value = $this->_cookieManager->getCookie(self::COOKIE_NAME);
- if (null !== $value) {
- $this->setCookie($value, $duration, $path);
- }
- return $this;
- }
- /**
- * Delete expired persistent sessions for the website
- *
- * @param null|int $websiteId
- * @return $this
- */
- public function deleteExpired($websiteId = null)
- {
- if ($websiteId === null) {
- $websiteId = $this->_storeManager->getStore()->getWebsiteId();
- }
- $lifetime = $this->_coreConfig->getValue(
- \Magento\Persistent\Helper\Data::XML_PATH_LIFE_TIME,
- 'website',
- (int)$websiteId
- );
- if ($lifetime) {
- $this->getResource()->deleteExpired($websiteId, gmdate('Y-m-d H:i:s', time() - $lifetime));
- }
- return $this;
- }
- /**
- * Delete 'persistent' cookie
- *
- * @return $this
- * @codeCoverageIgnore
- */
- public function afterDeleteCommit()
- {
- $this->removePersistentCookie();
- return parent::afterDeleteCommit();
- }
- /**
- * Set persistent shopping cart cookie.
- *
- * @param string $value
- * @param int $duration
- * @param string $path
- * @return void
- */
- private function setCookie($value, $duration, $path)
- {
- $publicCookieMetadata = $this->_cookieMetadataFactory->createPublicCookieMetadata()
- ->setDuration($duration)
- ->setPath($path)
- ->setSecure($this->getRequest()->isSecure())
- ->setHttpOnly(true);
- $this->_cookieManager->setPublicCookie(
- self::COOKIE_NAME,
- $value,
- $publicCookieMetadata
- );
- }
- /**
- * Get request object
- *
- * @return \Magento\Framework\App\Request\Http
- * @deprecated 100.1.0
- */
- private function getRequest()
- {
- if ($this->request == null) {
- $this->request = \Magento\Framework\App\ObjectManager::getInstance()
- ->get(\Magento\Framework\App\Request\Http::class);
- }
- return $this->request;
- }
- /**
- * Set `updated_at` to be always changed
- *
- * @return $this
- * @since 100.1.0
- */
- public function save()
- {
- $this->setUpdatedAt(gmdate('Y-m-d H:i:s'));
- return parent::save();
- }
- }
|