PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/module-customer/Model/Logger.php

https://gitlab.com/daigiangaitu91/magento
PHP | 120 lines | 63 code | 14 blank | 43 comment | 1 complexity | 9553c8436d394a8bf1e0f88882ca7aca MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model;
  7. use Magento\Framework\App\ResourceConnection;
  8. /**
  9. * Customer log data logger.
  10. *
  11. * Saves and retrieves customer log data.
  12. */
  13. class Logger
  14. {
  15. /**
  16. * Resource instance.
  17. *
  18. * @var Resource
  19. */
  20. protected $resource;
  21. /**
  22. * @var \Magento\Customer\Model\LogFactory
  23. */
  24. protected $logFactory;
  25. /**
  26. * @param ResourceConnection $resource
  27. * @param \Magento\Customer\Model\LogFactory $logFactory
  28. */
  29. public function __construct(
  30. ResourceConnection $resource,
  31. \Magento\Customer\Model\LogFactory $logFactory
  32. ) {
  33. $this->resource = $resource;
  34. $this->logFactory = $logFactory;
  35. }
  36. /**
  37. * Save (insert new or update existing) log.
  38. *
  39. * @param int $customerId
  40. * @param array $data
  41. * @return $this
  42. * @throws \InvalidArgumentException
  43. */
  44. public function log($customerId, array $data)
  45. {
  46. $data = array_filter($data);
  47. if (!$data) {
  48. throw new \InvalidArgumentException("Log data is empty");
  49. }
  50. /** @var \Magento\Framework\DB\Adapter\AdapterInterface $connection */
  51. $connection = $this->resource->getConnection(ResourceConnection::DEFAULT_CONNECTION);
  52. $connection->insertOnDuplicate(
  53. $this->resource->getTableName('customer_log'),
  54. array_merge(['customer_id' => $customerId], $data),
  55. array_keys($data)
  56. );
  57. return $this;
  58. }
  59. /**
  60. * Load log by Customer Id.
  61. *
  62. * @param int $customerId
  63. * @return Log
  64. */
  65. public function get($customerId = null)
  66. {
  67. $data = (null !== $customerId) ? $this->loadLogData($customerId) : [];
  68. return $this->logFactory->create(
  69. [
  70. 'customerId' => isset($data['customer_id']) ? $data['customer_id'] : null,
  71. 'lastLoginAt' => isset($data['last_login_at']) ? $data['last_login_at'] : null,
  72. 'lastLogoutAt' => isset($data['last_logout_at']) ? $data['last_logout_at'] : null,
  73. 'lastVisitAt' => isset($data['last_visit_at']) ? $data['last_visit_at'] : null
  74. ]
  75. );
  76. }
  77. /**
  78. * Load customer log data by customer id
  79. *
  80. * @param int $customerId
  81. * @return array
  82. */
  83. protected function loadLogData($customerId)
  84. {
  85. /** @var \Magento\Framework\DB\Adapter\AdapterInterface $connection */
  86. $connection = $this->resource->getConnection();
  87. $select = $connection->select()
  88. ->from(
  89. ['cl' => $this->resource->getTableName('customer_log')]
  90. )
  91. ->joinLeft(
  92. ['cv' => $this->resource->getTableName('customer_visitor')],
  93. 'cv.customer_id = cl.customer_id',
  94. ['last_visit_at']
  95. )
  96. ->where(
  97. 'cl.customer_id = ?',
  98. $customerId
  99. )
  100. ->order(
  101. 'cv.visitor_id DESC'
  102. )
  103. ->limit(1);
  104. return $connection->fetchRow($select);
  105. }
  106. }