ReportModulesInfo.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\NewRelicReporting\Model\Module\Collect;
  9. /**
  10. * Class ReportModulesInfo
  11. */
  12. class ReportModulesInfo
  13. {
  14. /**
  15. * @var Config
  16. */
  17. protected $config;
  18. /**
  19. * @var Collect
  20. */
  21. protected $collect;
  22. /**
  23. * @var \Magento\NewRelicReporting\Model\SystemFactory
  24. */
  25. protected $systemFactory;
  26. /**
  27. * @var \Magento\Framework\Json\EncoderInterface
  28. */
  29. protected $jsonEncoder;
  30. /**
  31. * Constructor
  32. *
  33. * @param Config $config
  34. * @param Collect $collect
  35. * @param \Magento\NewRelicReporting\Model\SystemFactory $systemFactory
  36. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  37. */
  38. public function __construct(
  39. Config $config,
  40. Collect $collect,
  41. \Magento\NewRelicReporting\Model\SystemFactory $systemFactory,
  42. \Magento\Framework\Json\EncoderInterface $jsonEncoder
  43. ) {
  44. $this->config = $config;
  45. $this->collect = $collect;
  46. $this->systemFactory = $systemFactory;
  47. $this->jsonEncoder = $jsonEncoder;
  48. }
  49. /**
  50. * Reports Modules and module changes to the database reporting_module_status table
  51. *
  52. * @return \Magento\NewRelicReporting\Model\Cron\ReportModulesInfo
  53. */
  54. public function report()
  55. {
  56. if ($this->config->isNewRelicEnabled()) {
  57. $moduleData = $this->collect->getModuleData();
  58. if (count($moduleData['changes']) > 0) {
  59. foreach ($moduleData['changes'] as $change) {
  60. $modelData = [];
  61. switch ($change['type']) {
  62. case Config::ENABLED:
  63. $modelData = [
  64. 'type' => Config::MODULE_ENABLED,
  65. 'action' => $this->jsonEncoder->encode($change),
  66. ];
  67. break;
  68. case Config::DISABLED:
  69. $modelData = [
  70. 'type' => Config::MODULE_DISABLED,
  71. 'action' => $this->jsonEncoder->encode($change),
  72. ];
  73. break;
  74. case Config::INSTALLED:
  75. $modelData = [
  76. 'type' => Config::MODULE_INSTALLED,
  77. 'action' => $this->jsonEncoder->encode($change),
  78. ];
  79. break;
  80. case Config::UNINSTALLED:
  81. $modelData = [
  82. 'type' => Config::MODULE_UNINSTALLED,
  83. 'action' => $this->jsonEncoder->encode($change),
  84. ];
  85. break;
  86. }
  87. /** @var \Magento\NewRelicReporting\Model\System $systemModel */
  88. $systemModel = $this->systemFactory->create();
  89. $systemModel->setData($modelData);
  90. $systemModel->save();
  91. }
  92. }
  93. }
  94. return $this;
  95. }
  96. }