Communication.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\WebapiAsync\Code\Generator\Config\RemoteServiceReader;
  8. use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
  9. use Magento\AsynchronousOperations\Model\ConfigInterface as WebApiAsyncConfig;
  10. use Magento\Framework\Communication\Config\ReflectionGenerator;
  11. /**
  12. * Remote service reader with auto generated configuration for communication.xml
  13. */
  14. class Communication implements \Magento\Framework\Config\ReaderInterface
  15. {
  16. /**
  17. * @var WebApiAsyncConfig
  18. */
  19. private $webapiAsyncConfig;
  20. /**
  21. * @var \Magento\Framework\Communication\Config\ReflectionGenerator
  22. */
  23. private $reflectionGenerator;
  24. /**
  25. * Initialize dependencies.
  26. *
  27. * @param WebApiAsyncConfig $webapiAsyncConfig
  28. * @param ReflectionGenerator $reflectionGenerator
  29. */
  30. public function __construct(
  31. WebApiAsyncConfig $webapiAsyncConfig,
  32. ReflectionGenerator $reflectionGenerator
  33. ) {
  34. $this->webapiAsyncConfig = $webapiAsyncConfig;
  35. $this->reflectionGenerator = $reflectionGenerator;
  36. }
  37. /**
  38. * Generate communication configuration based on remote services declarations
  39. *
  40. * @param string|null $scope
  41. * @return array
  42. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  43. */
  44. public function read($scope = null)
  45. {
  46. $asyncServicesData = $this->webapiAsyncConfig->getServices();
  47. $result = [];
  48. foreach ($asyncServicesData as $serviceData) {
  49. $topicName = $serviceData[WebApiAsyncConfig::SERVICE_PARAM_KEY_TOPIC];
  50. $serviceClass = $serviceData[WebApiAsyncConfig::SERVICE_PARAM_KEY_INTERFACE];
  51. $serviceMethod = $serviceData[WebApiAsyncConfig::SERVICE_PARAM_KEY_METHOD];
  52. $topicConfig = $this->reflectionGenerator->generateTopicConfigForServiceMethod(
  53. $topicName,
  54. $serviceClass,
  55. $serviceMethod,
  56. [
  57. WebApiAsyncConfig::DEFAULT_HANDLER_NAME => [
  58. CommunicationConfig::HANDLER_TYPE => $serviceClass,
  59. CommunicationConfig::HANDLER_METHOD => $serviceMethod,
  60. ],
  61. ],
  62. false
  63. );
  64. $rewriteTopicParams = [
  65. CommunicationConfig::TOPIC_IS_SYNCHRONOUS => false,
  66. CommunicationConfig::TOPIC_RESPONSE => null,
  67. ];
  68. $result[$topicName] = array_merge($topicConfig, $rewriteTopicParams);
  69. }
  70. $result[WebApiAsyncConfig::SYSTEM_TOPIC_NAME] = WebApiAsyncConfig::SYSTEM_TOPIC_CONFIGURATION;
  71. return [CommunicationConfig::TOPICS => $result];
  72. }
  73. }