Deployments.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\NewRelicReporting\Model\Apm;
  7. use \Magento\Framework\HTTP\ZendClient;
  8. class Deployments
  9. {
  10. /**
  11. * API URL for New Relic deployments
  12. */
  13. const API_URL = 'https://api.newrelic.com/deployments.xml';
  14. /**
  15. * @var \Magento\NewRelicReporting\Model\Config
  16. */
  17. protected $config;
  18. /**
  19. * @var \Psr\Log\LoggerInterface
  20. */
  21. protected $logger;
  22. /**
  23. * @var \Magento\Framework\HTTP\ZendClientFactory $clientFactory
  24. */
  25. protected $clientFactory;
  26. /**
  27. * Constructor
  28. *
  29. * @param \Magento\NewRelicReporting\Model\Config $config
  30. * @param \Psr\Log\LoggerInterface $logger
  31. * @param \Magento\Framework\HTTP\ZendClientFactory $clientFactory
  32. */
  33. public function __construct(
  34. \Magento\NewRelicReporting\Model\Config $config,
  35. \Psr\Log\LoggerInterface $logger,
  36. \Magento\Framework\HTTP\ZendClientFactory $clientFactory
  37. ) {
  38. $this->config = $config;
  39. $this->logger = $logger;
  40. $this->clientFactory = $clientFactory;
  41. }
  42. /**
  43. * Performs the request to make the deployment
  44. *
  45. * @param string $description
  46. * @param bool $change
  47. * @param bool $user
  48. *
  49. * @return bool|string
  50. */
  51. public function setDeployment($description, $change = false, $user = false)
  52. {
  53. $apiUrl = $this->config->getNewRelicApiUrl();
  54. if (empty($apiUrl)) {
  55. $this->logger->notice('New Relic API URL is blank, using fallback URL');
  56. $apiUrl = self::API_URL;
  57. }
  58. /** @var \Magento\Framework\HTTP\ZendClient $client */
  59. $client = $this->clientFactory->create();
  60. $client->setUri($apiUrl);
  61. $client->setMethod(ZendClient::POST);
  62. $client->setHeaders(['x-api-key' => $this->config->getNewRelicApiKey()]);
  63. $params = [
  64. 'deployment[app_name]' => $this->config->getNewRelicAppName(),
  65. 'deployment[application_id]' => $this->config->getNewRelicAppId(),
  66. 'deployment[description]' => $description,
  67. 'deployment[changelog]' => $change,
  68. 'deployment[user]' => $user
  69. ];
  70. $client->setParameterPost($params);
  71. try {
  72. $response = $client->request();
  73. } catch (\Zend_Http_Client_Exception $e) {
  74. $this->logger->critical($e);
  75. return false;
  76. }
  77. if (($response->getStatus() < 200 || $response->getStatus() > 210)) {
  78. $this->logger->warning('Deployment marker request did not send a 200 status code.');
  79. return false;
  80. }
  81. return $response->getBody();
  82. }
  83. }