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

Public Member Functions

 __construct (\Magento\Framework\Model\Context $context, \Magento\Framework\Registry $registry, \Magento\Framework\Session\SessionManagerInterface $session, \Magento\Framework\HTTP\Header $httpHeader, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Framework\Indexer\IndexerRegistry $indexerRegistry, \Magento\Framework\Model\ResourceModel\AbstractResource $resource=null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection=null, array $ignoredUserAgents=[], array $ignores=[], array $data=[], RequestSafetyInterface $requestSafety=null)
 
 setSkipRequestLogging ($skipRequestLogging)
 
 initByRequest ($observer)
 
 saveByRequest ($observer)
 
 isModuleIgnored ($observer)
 
 bindCustomerLogout ($observer)
 
 bindQuoteCreate ($observer)
 
 bindQuoteDestroy ($observer)
 
 getCleanTime ()
 
 clean ()
 
 getOnlineInterval ()
 
- 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 VISITOR_TYPE_CUSTOMER = 'c'
 
const VISITOR_TYPE_VISITOR = 'v'
 
const DEFAULT_ONLINE_MINUTES_INTERVAL = 15
 
const XML_PATH_ONLINE_INTERVAL = 'customer/online_customers/online_minutes_interval'
 

Protected Member Functions

 _construct ()
 
- 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

 $ignoredUserAgents
 
 $session
 
 $httpHeader
 
 $skipRequestLogging = false
 
 $ignores
 
 $scopeConfig
 
 $dateTime
 
 $indexerRegistry
 
- 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

Definition at line 17 of file Visitor.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( \Magento\Framework\Model\Context  $context,
\Magento\Framework\Registry  $registry,
\Magento\Framework\Session\SessionManagerInterface  $session,
\Magento\Framework\HTTP\Header  $httpHeader,
\Magento\Framework\App\Config\ScopeConfigInterface  $scopeConfig,
\Magento\Framework\Stdlib\DateTime  $dateTime,
\Magento\Framework\Indexer\IndexerRegistry  $indexerRegistry,
\Magento\Framework\Model\ResourceModel\AbstractResource  $resource = null,
\Magento\Framework\Data\Collection\AbstractDb  $resourceCollection = null,
array  $ignoredUserAgents = [],
array  $ignores = [],
array  $data = [],
RequestSafetyInterface  $requestSafety = null 
)
Parameters
\Magento\Framework\Model\Context$context
\Magento\Framework\Registry$registry
\Magento\Framework\Session\SessionManagerInterface$session
\Magento\Framework\HTTP\Header$httpHeader
\Magento\Framework\App\Config\ScopeConfigInterface$scopeConfig
\Magento\Framework\Stdlib\DateTime$dateTime
\Magento\Framework\Indexer\IndexerRegistry$indexerRegistry
\Magento\Framework\Model\ResourceModel\AbstractResource | null$resource
\Magento\Framework\Data\Collection\AbstractDb | null$resourceCollection
array$ignoredUserAgents
array$ignores
array$data@SuppressWarnings(PHPMD.ExcessiveParameterList)

Definition at line 92 of file Visitor.php.

106  {
107  $this->session = $session;
108  $this->httpHeader = $httpHeader;
109  $this->ignoredUserAgents = $ignoredUserAgents;
110  parent::__construct($context, $registry, $resource, $resourceCollection, $data);
111  $this->ignores = $ignores;
112  $this->scopeConfig = $scopeConfig;
113  $this->dateTime = $dateTime;
114  $this->indexerRegistry = $indexerRegistry;
115  $this->requestSafety = $requestSafety ?? ObjectManager::getInstance()->get(RequestSafetyInterface::class);
116  }
$resource
Definition: bulk.php:12

Member Function Documentation

◆ _construct()

_construct ( )
protected

Object initialization

Returns
void

Definition at line 123 of file Visitor.php.

124  {
125  $this->_init(\Magento\Customer\Model\ResourceModel\Visitor::class);
126  $userAgent = $this->httpHeader->getHttpUserAgent();
127  if ($this->ignoredUserAgents) {
128  if (in_array($userAgent, $this->ignoredUserAgents)) {
129  $this->skipRequestLogging = true;
130  }
131  }
132  }

◆ bindCustomerLogout()

bindCustomerLogout (   $observer)

Bind customer data when customer logout

Used in event "customer_logout"

Parameters
\Magento\Framework\Event\Observer$observer
Returns
\Magento\Customer\Model\Visitor @SuppressWarnings(PHPMD.UnusedFormalParameter)

Definition at line 252 of file Visitor.php.

253  {
254  if ($this->getCustomerId()) {
255  $this->setDoCustomerLogout(true);
256  }
257  return $this;
258  }

◆ bindQuoteCreate()

bindQuoteCreate (   $observer)

Create binding of checkout quote

Parameters
\Magento\Framework\Event\Observer$observer
Returns
\Magento\Customer\Model\Visitor

Definition at line 266 of file Visitor.php.

267  {
268  $quote = $observer->getEvent()->getQuote();
269  if ($quote) {
270  if ($quote->getIsCheckoutCart()) {
271  $this->setQuoteId($quote->getId());
272  $this->setDoQuoteCreate(true);
273  }
274  }
275  return $this;
276  }
$quote

◆ bindQuoteDestroy()

bindQuoteDestroy (   $observer)

Destroy binding of checkout quote

Parameters
\Magento\Framework\Event\Observer$observer
Returns
\Magento\Customer\Model\Visitor

Definition at line 283 of file Visitor.php.

284  {
285  $quote = $observer->getEvent()->getQuote();
286  if ($quote) {
287  $this->setDoQuoteDestroy(true);
288  }
289  return $this;
290  }
$quote

◆ clean()

clean ( )

Clean visitor's outdated records

Returns
$this

Definition at line 310 of file Visitor.php.

311  {
312  $this->getResource()->clean($this);
313  return $this;
314  }

◆ getCleanTime()

getCleanTime ( )

Return clean time in seconds for visitor's outdated records

Returns
string

Definition at line 297 of file Visitor.php.

298  {
299  return $this->scopeConfig->getValue(
300  \Magento\Framework\Session\Config::XML_PATH_COOKIE_LIFETIME,
301  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
302  ) + 86400;
303  }

◆ getOnlineInterval()

getOnlineInterval ( )

Retrieve Online Interval (in minutes)

Returns
int Minutes Interval

Definition at line 321 of file Visitor.php.

322  {
323  $configValue = intval(
324  $this->scopeConfig->getValue(
325  static::XML_PATH_ONLINE_INTERVAL,
326  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
327  )
328  );
329  return $configValue ?: static::DEFAULT_ONLINE_MINUTES_INTERVAL;
330  }

◆ initByRequest()

initByRequest (   $observer)

Initialization visitor by request

Used in event "controller_action_predispatch"

Parameters
\Magento\Framework\Event\Observer$observer
Returns
\Magento\Customer\Model\Visitor

Definition at line 154 of file Visitor.php.

155  {
156  if ($this->skipRequestLogging || $this->isModuleIgnored($observer)) {
157  return $this;
158  }
159 
160  if ($this->session->getVisitorData()) {
161  $this->setData($this->session->getVisitorData());
162  if ($this->getSessionId() != $this->session->getSessionId()) {
163  $this->setSessionId($this->session->getSessionId());
164  }
165  }
166 
167  $this->setLastVisitAt((new \DateTime())->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT));
168 
169  // prevent saving Visitor for safe methods, e.g. GET request
170  if ($this->requestSafety->isSafeMethod()) {
171  return $this;
172  }
173  if (!$this->getId()) {
174  $this->setSessionId($this->session->getSessionId());
175  $this->save();
176  $this->_eventManager->dispatch('visitor_init', ['visitor' => $this]);
177  $this->session->setVisitorData($this->getData());
178  }
179  return $this;
180  }
getData($key='', $index=null)
Definition: DataObject.php:119

◆ isModuleIgnored()

isModuleIgnored (   $observer)

Returns true if the module is required

Parameters
\Magento\Framework\Event\Observer$observer
Returns
bool

Definition at line 213 of file Visitor.php.

214  {
215  if (is_array($this->ignores) && $observer) {
216  $curModule = $observer->getEvent()->getControllerAction()->getRequest()->getRouteName();
217  if (isset($this->ignores[$curModule])) {
218  return true;
219  }
220  }
221  return false;
222  }

◆ saveByRequest()

saveByRequest (   $observer)

Save visitor by request

Used in event "controller_action_postdispatch"

Parameters
\Magento\Framework\Event\Observer$observer
Returns
\Magento\Customer\Model\Visitor

Definition at line 190 of file Visitor.php.

191  {
192  // prevent saving Visitor for safe methods, e.g. GET request
193  if ($this->skipRequestLogging || $this->requestSafety->isSafeMethod() || $this->isModuleIgnored($observer)) {
194  return $this;
195  }
196 
197  try {
198  $this->save();
199  $this->_eventManager->dispatch('visitor_activity_save', ['visitor' => $this]);
200  $this->session->setVisitorData($this->getData());
201  } catch (\Exception $e) {
202  $this->_logger->critical($e);
203  }
204  return $this;
205  }
getData($key='', $index=null)
Definition: DataObject.php:119

◆ setSkipRequestLogging()

setSkipRequestLogging (   $skipRequestLogging)

Skip request logging

Parameters
bool$skipRequestLogging
Returns
\Magento\Customer\Model\Visitor

Definition at line 140 of file Visitor.php.

141  {
142  $this->skipRequestLogging = (bool)$skipRequestLogging;
143  return $this;
144  }

Field Documentation

◆ $dateTime

$dateTime
protected

Definition at line 64 of file Visitor.php.

◆ $httpHeader

$httpHeader
protected

Definition at line 40 of file Visitor.php.

◆ $ignoredUserAgents

$ignoredUserAgents
protected

Definition at line 30 of file Visitor.php.

◆ $ignores

$ignores
protected

Definition at line 52 of file Visitor.php.

◆ $indexerRegistry

$indexerRegistry
protected

Definition at line 69 of file Visitor.php.

◆ $scopeConfig

$scopeConfig
protected

Definition at line 59 of file Visitor.php.

◆ $session

$session
protected

Definition at line 35 of file Visitor.php.

◆ $skipRequestLogging

$skipRequestLogging = false
protected

Definition at line 45 of file Visitor.php.

◆ DEFAULT_ONLINE_MINUTES_INTERVAL

const DEFAULT_ONLINE_MINUTES_INTERVAL = 15

Definition at line 23 of file Visitor.php.

◆ VISITOR_TYPE_CUSTOMER

const VISITOR_TYPE_CUSTOMER = 'c'

Definition at line 19 of file Visitor.php.

◆ VISITOR_TYPE_VISITOR

const VISITOR_TYPE_VISITOR = 'v'

Definition at line 21 of file Visitor.php.

◆ XML_PATH_ONLINE_INTERVAL

const XML_PATH_ONLINE_INTERVAL = 'customer/online_customers/online_minutes_interval'

Definition at line 25 of file Visitor.php.


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