ReportCounts.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\NewRelicReporting\Model\Cron;
  7. use Magento\NewRelicReporting\Model\Config;
  8. use Magento\Catalog\Api\ProductManagementInterface;
  9. use Magento\Catalog\Model\Product\Attribute\Source\Status;
  10. use Magento\ConfigurableProduct\Api\ConfigurableProductManagementInterface;
  11. use Magento\Catalog\Api\CategoryManagementInterface;
  12. /**
  13. * Class ReportCounts
  14. */
  15. class ReportCounts
  16. {
  17. /**
  18. * @var Config
  19. */
  20. protected $config;
  21. /**
  22. * @var ProductManagementInterface
  23. */
  24. protected $productManagement;
  25. /**
  26. * @var ConfigurableProductManagementInterface
  27. */
  28. protected $configurableManagement;
  29. /**
  30. * @var CategoryManagementInterface
  31. */
  32. protected $categoryManagement;
  33. /**
  34. * @var \Magento\NewRelicReporting\Model\CountsFactory
  35. */
  36. protected $countsFactory;
  37. /**
  38. * @var \Magento\NewRelicReporting\Model\ResourceModel\Counts\CollectionFactory
  39. */
  40. protected $countsCollectionFactory;
  41. /**
  42. * Constructor
  43. *
  44. * @param Config $config
  45. * @param ProductManagementInterface $productManagement
  46. * @param ConfigurableProductManagementInterface $configurableManagement
  47. * @param CategoryManagementInterface $categoryManagement
  48. * @param \Magento\NewRelicReporting\Model\CountsFactory $countsFactory
  49. * @param \Magento\NewRelicReporting\Model\ResourceModel\Counts\CollectionFactory $countsCollectionFactory
  50. */
  51. public function __construct(
  52. Config $config,
  53. ProductManagementInterface $productManagement,
  54. ConfigurableProductManagementInterface $configurableManagement,
  55. CategoryManagementInterface $categoryManagement,
  56. \Magento\NewRelicReporting\Model\CountsFactory $countsFactory,
  57. \Magento\NewRelicReporting\Model\ResourceModel\Counts\CollectionFactory $countsCollectionFactory
  58. ) {
  59. $this->config = $config;
  60. $this->productManagement = $productManagement;
  61. $this->configurableManagement = $configurableManagement;
  62. $this->categoryManagement = $categoryManagement;
  63. $this->countsFactory = $countsFactory;
  64. $this->countsCollectionFactory = $countsCollectionFactory;
  65. }
  66. /**
  67. * Updates the count for a specific model in the database
  68. *
  69. * @param int $count
  70. * @param \Magento\NewRelicReporting\Model\Counts $model
  71. * @param string $type
  72. * @return void
  73. */
  74. protected function updateCount($count, \Magento\NewRelicReporting\Model\Counts $model, $type)
  75. {
  76. /** @var \Magento\NewRelicReporting\Model\ResourceModel\Counts\Collection $collection */
  77. $collection = $this->countsCollectionFactory->create()
  78. ->addFieldToFilter(
  79. 'type',
  80. ['eq' => $type]
  81. )->addOrder(
  82. 'updated_at',
  83. 'DESC'
  84. )->setPageSize(1);
  85. $latestUpdate = $collection->getFirstItem();
  86. if ((!$latestUpdate) || ($count != $latestUpdate->getCount())) {
  87. $model->setEntityId(null);
  88. $model->setType($type);
  89. $model->setCount($count);
  90. $model->save();
  91. }
  92. }
  93. /**
  94. * Reports product size to the database reporting_counts table
  95. *
  96. * @return void
  97. */
  98. protected function reportProductsSize()
  99. {
  100. $productCount = $this->productManagement->getCount();
  101. /** @var \Magento\NewRelicReporting\Model\Counts $model */
  102. $model = $this->countsFactory->create()->load(Config::PRODUCT_COUNT, 'type');
  103. $this->updateCount($productCount, $model, Config::PRODUCT_COUNT);
  104. }
  105. /**
  106. * Reports configurable product size to the database reporting_counts table
  107. *
  108. * @return void
  109. */
  110. protected function reportConfigurableProductsSize()
  111. {
  112. $configurableCount = $this->configurableManagement->getCount();
  113. /** @var \Magento\NewRelicReporting\Model\Counts $model */
  114. $model = $this->countsFactory->create()->load(Config::CONFIGURABLE_COUNT, 'type');
  115. $this->updateCount($configurableCount, $model, Config::CONFIGURABLE_COUNT);
  116. }
  117. /**
  118. * Reports number of active products to the database reporting_counts table
  119. *
  120. * @return void
  121. */
  122. protected function reportProductsActive()
  123. {
  124. $productsActiveCount = $this->productManagement->getCount(Status::STATUS_ENABLED);
  125. /** @var \Magento\NewRelicReporting\Model\Counts $model */
  126. $model = $this->countsFactory->create()->load(Config::ACTIVE_COUNT, 'type');
  127. $this->updateCount($productsActiveCount, $model, Config::ACTIVE_COUNT);
  128. }
  129. /**
  130. * Reports category size to the database reporting_counts table
  131. *
  132. * @return void
  133. */
  134. protected function reportCategorySize()
  135. {
  136. $categoryCount = $this->categoryManagement->getCount();
  137. /** @var \Magento\NewRelicReporting\Model\Counts $model */
  138. $model = $this->countsFactory->create()->load(Config::CATEGORY_SIZE, 'type');
  139. $this->updateCount($categoryCount, $model, Config::CATEGORY_SIZE);
  140. }
  141. /**
  142. * Reports Modules and module changes to the database reporting_module_status table
  143. *
  144. * @return \Magento\NewRelicReporting\Model\Cron\ReportCounts
  145. */
  146. public function report()
  147. {
  148. if ($this->config->isNewRelicEnabled()) {
  149. $this->reportProductsSize();
  150. $this->reportConfigurableProductsSize();
  151. $this->reportProductsActive();
  152. $this->reportCategorySize();
  153. }
  154. return $this;
  155. }
  156. }