Converter.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue\Publisher\Config\Xml;
  7. use Magento\Framework\Stdlib\BooleanUtils;
  8. use Magento\Framework\MessageQueue\DefaultValueProvider;
  9. /**
  10. * Converts MessageQueue publishers config from \DOMDocument to array
  11. */
  12. class Converter implements \Magento\Framework\Config\ConverterInterface
  13. {
  14. /**
  15. * Boolean value converter.
  16. *
  17. * @var BooleanUtils
  18. */
  19. private $booleanUtils;
  20. /**
  21. * Default value provider.
  22. *
  23. * @var DefaultValueProvider
  24. */
  25. private $defaultValueProvider;
  26. /**
  27. * Initialize dependencies.
  28. *
  29. * @param BooleanUtils $booleanUtils
  30. * @param DefaultValueProvider $defaultValueProvider
  31. */
  32. public function __construct(BooleanUtils $booleanUtils, DefaultValueProvider $defaultValueProvider)
  33. {
  34. $this->booleanUtils = $booleanUtils;
  35. $this->defaultValueProvider = $defaultValueProvider;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function convert($source)
  41. {
  42. $result = [];
  43. /** @var $publisherConfig \DOMElement */
  44. foreach ($source->getElementsByTagName('publisher') as $publisherConfig) {
  45. $topic = $this->getAttributeValue($publisherConfig, 'topic');
  46. $connections = [];
  47. /** @var \DOMNode $connectionConfig */
  48. foreach ($publisherConfig->childNodes as $connectionConfig) {
  49. if ($connectionConfig->nodeName != 'connection' || $connectionConfig->nodeType != XML_ELEMENT_NODE) {
  50. continue;
  51. }
  52. $connectionName = $this->getAttributeValue($connectionConfig, 'name');
  53. if (!$connectionName) {
  54. throw new \InvalidArgumentException('Connection name is missing');
  55. }
  56. $exchangeName = $this->getAttributeValue(
  57. $connectionConfig,
  58. 'exchange',
  59. $this->defaultValueProvider->getExchange()
  60. );
  61. $isDisabled = $this->getAttributeValue($connectionConfig, 'disabled', false);
  62. $connections[$connectionName] = [
  63. 'name' => $connectionName,
  64. 'exchange' => $exchangeName,
  65. 'disabled' => $this->booleanUtils->toBoolean($isDisabled),
  66. ];
  67. }
  68. $isDisabled = $this->getAttributeValue($publisherConfig, 'disabled', false);
  69. $result[$topic] = [
  70. 'topic' => $topic,
  71. 'disabled' => $this->booleanUtils->toBoolean($isDisabled),
  72. 'connections' => $connections,
  73. ];
  74. }
  75. return $result;
  76. }
  77. /**
  78. * Get attribute value of the given node
  79. *
  80. * @param \DOMNode $node
  81. * @param string $attributeName
  82. * @param mixed $default
  83. * @return string|null
  84. */
  85. private function getAttributeValue(\DOMNode $node, $attributeName, $default = null)
  86. {
  87. $item = $node->attributes->getNamedItem($attributeName);
  88. return $item ? $item->nodeValue : $default;
  89. }
  90. }