Grid.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Model\ResourceModel\Customer;
  7. use Magento\Framework\App\ResourceConnection;
  8. use Magento\Framework\Indexer\IndexerRegistry;
  9. use Magento\Framework\Indexer\ScopeResolver\FlatScopeResolver;
  10. use Magento\Customer\Model\Customer;
  11. /**
  12. * @deprecated 100.1.0
  13. */
  14. class Grid
  15. {
  16. /**
  17. * @var resource
  18. */
  19. protected $resource;
  20. /**
  21. * @var \Magento\Framework\Indexer\IndexerRegistry
  22. */
  23. protected $indexerRegistry;
  24. /**
  25. * @var \Magento\Framework\Indexer\ScopeResolver\FlatScopeResolver
  26. */
  27. protected $flatScopeResolver;
  28. /**
  29. * @param ResourceConnection $resource
  30. * @param IndexerRegistry $indexerRegistry
  31. * @param FlatScopeResolver $flatScopeResolver
  32. */
  33. public function __construct(
  34. ResourceConnection $resource,
  35. IndexerRegistry $indexerRegistry,
  36. FlatScopeResolver $flatScopeResolver
  37. ) {
  38. $this->resource = $resource;
  39. $this->indexerRegistry = $indexerRegistry;
  40. $this->flatScopeResolver = $flatScopeResolver;
  41. }
  42. /**
  43. * Synchronize customer grid
  44. *
  45. * @return void
  46. *
  47. * @deprecated 100.1.0
  48. */
  49. public function syncCustomerGrid()
  50. {
  51. $indexer = $this->indexerRegistry->get(Customer::CUSTOMER_GRID_INDEXER_ID);
  52. $customerIds = $this->getCustomerIdsForReindex();
  53. if ($customerIds) {
  54. $indexer->reindexList($customerIds);
  55. }
  56. }
  57. /**
  58. * Retrieve customer IDs for reindex
  59. *
  60. * @return array
  61. *
  62. * @deprecated 100.1.0
  63. */
  64. protected function getCustomerIdsForReindex()
  65. {
  66. $connection = $this->resource->getConnection();
  67. $gridTableName = $this->flatScopeResolver->resolve(Customer::CUSTOMER_GRID_INDEXER_ID, []);
  68. $select = $connection->select()
  69. ->from($this->resource->getTableName($gridTableName), 'last_visit_at')
  70. ->order('last_visit_at DESC')
  71. ->limit(1);
  72. $lastVisitAt = $connection->query($select)->fetchColumn();
  73. $select = $connection->select()
  74. ->from($this->resource->getTableName('customer_log'), 'customer_id')
  75. ->where('last_login_at > ?', $lastVisitAt);
  76. $customerIds = [];
  77. foreach ($connection->query($select)->fetchAll() as $row) {
  78. $customerIds[] = $row['customer_id'];
  79. }
  80. return $customerIds;
  81. }
  82. }