Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReportCounts.php
Go to the documentation of this file.
1 <?php
7 
13 
18 {
22  protected $config;
23 
27  protected $productManagement;
28 
33 
38 
42  protected $countsFactory;
43 
48 
59  public function __construct(
64  \Magento\NewRelicReporting\Model\CountsFactory $countsFactory,
65  \Magento\NewRelicReporting\Model\ResourceModel\Counts\CollectionFactory $countsCollectionFactory
66  ) {
67  $this->config = $config;
68  $this->productManagement = $productManagement;
69  $this->configurableManagement = $configurableManagement;
70  $this->categoryManagement = $categoryManagement;
71  $this->countsFactory = $countsFactory;
72  $this->countsCollectionFactory = $countsCollectionFactory;
73  }
74 
83  protected function updateCount($count, \Magento\NewRelicReporting\Model\Counts $model, $type)
84  {
86  $collection = $this->countsCollectionFactory->create()
87  ->addFieldToFilter(
88  'type',
89  ['eq' => $type]
90  )->addOrder(
91  'updated_at',
92  'DESC'
93  )->setPageSize(1);
94  $latestUpdate = $collection->getFirstItem();
95 
96  if ((!$latestUpdate) || ($count != $latestUpdate->getCount())) {
97  $model->setEntityId(null);
98  $model->setType($type);
99  $model->setCount($count);
100  $model->save();
101  }
102  }
103 
109  protected function reportProductsSize()
110  {
111  $productCount = $this->productManagement->getCount();
113  $model = $this->countsFactory->create()->load(Config::PRODUCT_COUNT, 'type');
114  $this->updateCount($productCount, $model, Config::PRODUCT_COUNT);
115  }
116 
122  protected function reportConfigurableProductsSize()
123  {
124  $configurableCount = $this->configurableManagement->getCount();
126  $model = $this->countsFactory->create()->load(Config::CONFIGURABLE_COUNT, 'type');
127  $this->updateCount($configurableCount, $model, Config::CONFIGURABLE_COUNT);
128  }
129 
135  protected function reportProductsActive()
136  {
137  $productsActiveCount = $this->productManagement->getCount(Status::STATUS_ENABLED);
139  $model = $this->countsFactory->create()->load(Config::ACTIVE_COUNT, 'type');
140  $this->updateCount($productsActiveCount, $model, Config::ACTIVE_COUNT);
141  }
142 
148  protected function reportCategorySize()
149  {
150  $categoryCount = $this->categoryManagement->getCount();
152  $model = $this->countsFactory->create()->load(Config::CATEGORY_SIZE, 'type');
153  $this->updateCount($categoryCount, $model, Config::CATEGORY_SIZE);
154  }
155 
161  public function report()
162  {
163  if ($this->config->isNewRelicEnabled()) {
164  $this->reportProductsSize();
165  $this->reportConfigurableProductsSize();
166  $this->reportProductsActive();
167  $this->reportCategorySize();
168  }
169 
170  return $this;
171  }
172 }
$count
Definition: recent.phtml:13
$type
Definition: item.phtml:13
__construct(Config $config, ProductManagementInterface $productManagement, ConfigurableProductManagementInterface $configurableManagement, CategoryManagementInterface $categoryManagement, \Magento\NewRelicReporting\Model\CountsFactory $countsFactory, \Magento\NewRelicReporting\Model\ResourceModel\Counts\CollectionFactory $countsCollectionFactory)