CronEvent.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\NewRelicReporting\Model;
  7. use \Magento\Framework\HTTP\ZendClient;
  8. class CronEvent
  9. {
  10. /**
  11. * @var \Magento\Framework\HTTP\ZendClient
  12. */
  13. protected $request;
  14. /**
  15. * URL for Insights API, generated via method getEventsUrl
  16. *
  17. * @var string
  18. */
  19. protected $eventsUrl = '';
  20. /**
  21. * Parameters to be sent to New Relic for a job run
  22. *
  23. * @var array
  24. */
  25. protected $customParameters = [];
  26. /**
  27. * @var \Magento\NewRelicReporting\Model\Config
  28. */
  29. protected $config;
  30. /**
  31. * @var \Magento\Framework\Json\Helper\Data
  32. */
  33. protected $jsonEncoder;
  34. /**
  35. * @var \Magento\Framework\HTTP\ZendClientFactory $clientFactory
  36. */
  37. protected $clientFactory;
  38. /**
  39. * Constructor
  40. *
  41. * @param \Magento\NewRelicReporting\Model\Config $config
  42. * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder
  43. * @param \Magento\Framework\HTTP\ZendClientFactory $clientFactory
  44. */
  45. public function __construct(
  46. \Magento\NewRelicReporting\Model\Config $config,
  47. \Magento\Framework\Json\EncoderInterface $jsonEncoder,
  48. \Magento\Framework\HTTP\ZendClientFactory $clientFactory
  49. ) {
  50. $this->config = $config;
  51. $this->jsonEncoder = $jsonEncoder;
  52. $this->clientFactory = $clientFactory;
  53. }
  54. /**
  55. * Returns Insights API url with account id
  56. *
  57. * @return string
  58. * @throws \Magento\Framework\Exception\LocalizedException
  59. */
  60. protected function getEventsUrl()
  61. {
  62. if (empty($this->eventsUrl)) {
  63. $accountId = $this->config->getNewRelicAccountId();
  64. if (empty($accountId)) {
  65. throw new \Magento\Framework\Exception\LocalizedException(__(
  66. 'No New Relic Application ID configured, cannot continue with Cron Event reporting'
  67. ));
  68. }
  69. $this->eventsUrl = sprintf(
  70. $this->config->getInsightsApiUrl(),
  71. $accountId
  72. );
  73. }
  74. return $this->eventsUrl;
  75. }
  76. /**
  77. * Returns HTTP request to events url
  78. *
  79. * @return \Magento\Framework\HTTP\ZendClient
  80. */
  81. protected function getRequest()
  82. {
  83. if (!isset($this->request)) {
  84. $this->request = $this->clientFactory->create();
  85. $this->request->setUri($this->getEventsUrl());
  86. $insertKey = $this->config->getInsightsInsertKey();
  87. $this->request->setMethod(ZendClient::POST);
  88. $this->request->setHeaders(
  89. [
  90. 'X-Insert-Key' => $insertKey,
  91. 'Content-Type' => 'application/json',
  92. ]
  93. );
  94. }
  95. return $this->request;
  96. }
  97. /**
  98. * Returns all set custom parameters as JSON string
  99. *
  100. * @return string
  101. */
  102. protected function getJsonForResponse()
  103. {
  104. $json = [
  105. 'eventType' => 'Cron',
  106. 'appName' => $this->config->getNewRelicAppName(),
  107. 'appId' => $this->config->getNewRelicAppId(),
  108. ];
  109. $jsonArrayKeys = array_keys($json);
  110. foreach ($jsonArrayKeys as $jsonKey) {
  111. if (array_key_exists($jsonKey, $this->customParameters)) {
  112. unset($this->customParameters[$jsonKey]);
  113. }
  114. }
  115. $json = array_merge($json, $this->customParameters);
  116. return $this->jsonEncoder->encode($json);
  117. }
  118. /**
  119. * Add custom parameters to send with API request
  120. *
  121. * @param array $data
  122. * @return \Magento\NewRelicReporting\Model\CronEvent $this
  123. */
  124. public function addData(array $data)
  125. {
  126. $this->customParameters = array_merge($this->customParameters, $data);
  127. return $this;
  128. }
  129. /**
  130. * Instantiates request if necessary and sends off with collected data
  131. *
  132. * @return bool
  133. */
  134. public function sendRequest()
  135. {
  136. $response = $this->getRequest()
  137. ->setRawData($this->getJsonForResponse())
  138. ->request();
  139. if ($response->getStatus() >= 200 && $response->getStatus() < 300) {
  140. return true;
  141. }
  142. return false;
  143. }
  144. }