Amqp.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\TestFramework\Helper;
  8. /**
  9. * Helper class to access RabbitMQ server configuration
  10. */
  11. class Amqp
  12. {
  13. const CONFIG_PATH_HOST = 'queue/amqp/host';
  14. const CONFIG_PATH_USER = 'queue/amqp/user';
  15. const CONFIG_PATH_PASSWORD = 'queue/amqp/password';
  16. const DEFAULT_MANAGEMENT_PORT = '15672';
  17. /**
  18. * @var Curl
  19. */
  20. private $curl;
  21. /**
  22. * @var \Magento\Framework\App\DeploymentConfig
  23. */
  24. private $deploymentConfig;
  25. /**
  26. * RabbitMQ API host
  27. *
  28. * @var string
  29. */
  30. private $host;
  31. /**
  32. * Initialize dependencies.
  33. * @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
  34. */
  35. public function __construct(
  36. \Magento\Framework\App\DeploymentConfig $deploymentConfig = null
  37. ) {
  38. $this->deploymentConfig = $deploymentConfig ?? \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
  39. ->get(\Magento\Framework\App\DeploymentConfig::class);
  40. $this->curl = new Curl();
  41. $this->curl->setCredentials(
  42. $this->deploymentConfig->get(self::CONFIG_PATH_USER),
  43. $this->deploymentConfig->get(self::CONFIG_PATH_PASSWORD)
  44. );
  45. $this->curl->addHeader('content-type', 'application/json');
  46. $this->host = sprintf(
  47. 'http://%s:%s/api/',
  48. $this->deploymentConfig->get(self::CONFIG_PATH_HOST),
  49. defined('RABBITMQ_MANAGEMENT_PORT') ? RABBITMQ_MANAGEMENT_PORT : self::DEFAULT_MANAGEMENT_PORT
  50. );
  51. }
  52. /**
  53. * Check that the RabbitMQ instance has the management plugin installed and the api is available.
  54. *
  55. * @return bool
  56. */
  57. public function isAvailable(): bool
  58. {
  59. $this->curl->get($this->host . 'overview');
  60. $data = $this->curl->getBody();
  61. $data = json_decode($data, true);
  62. return isset($data['management_version']);
  63. }
  64. /**
  65. * Get declared exchanges.
  66. *
  67. * @return array
  68. */
  69. public function getExchanges()
  70. {
  71. $this->curl->get($this->host . 'exchanges');
  72. $data = $this->curl->getBody();
  73. $data = json_decode($data, true);
  74. $output = [];
  75. foreach ($data as $value) {
  76. $output[$value['name']] = $value;
  77. }
  78. return $output;
  79. }
  80. /**
  81. * Get declared exchange bindings.
  82. *
  83. * @param string $name
  84. * @return array
  85. */
  86. public function getExchangeBindings($name)
  87. {
  88. $this->curl->get($this->host . 'exchanges/%2f/' . $name . '/bindings/source');
  89. $data = $this->curl->getBody();
  90. return json_decode($data, true);
  91. }
  92. /**
  93. * Get All available connections
  94. *
  95. * @return array
  96. */
  97. public function getConnections()
  98. {
  99. $this->curl->get($this->host . 'connections');
  100. $data = $this->curl->getBody();
  101. $data = json_decode($data, true);
  102. $output = [];
  103. foreach ($data as $value) {
  104. $output[$value['name']] = $value;
  105. }
  106. return $output;
  107. }
  108. /**
  109. * Clear Queue
  110. *
  111. * @param string $name
  112. * @param int $numMessages
  113. * @return string
  114. */
  115. public function clearQueue(string $name, int $numMessages = 50)
  116. {
  117. $body = [
  118. "count" => $numMessages,
  119. "ackmode" => "ack_requeue_false",
  120. "encoding" => "auto",
  121. "truncate" => 50000
  122. ];
  123. $this->curl->post($this->host . 'queue/%2f/' . $name . '/get', json_encode($body));
  124. return $this->curl->getBody();
  125. }
  126. /**
  127. * Delete connection
  128. *
  129. * @param string $name
  130. * @return string $data
  131. */
  132. public function deleteConnection($name)
  133. {
  134. $this->curl->delete($this->host . 'conections/' . urlencode($name));
  135. $data = $this->curl->getBody();
  136. return $data;
  137. }
  138. }