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(); } }