Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Converter.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
12 
17 {
21  private static $defaultInstance = ConsumerInterface::class;
22 
26  private $configParser;
27 
33  private $defaultValueProvider;
34 
41  public function __construct(ConfigParser $configParser, DefaultValueProvider $defaultValueProvider)
42  {
43  $this->configParser = $configParser;
44  $this->defaultValueProvider = $defaultValueProvider;
45  }
46 
50  public function convert($source)
51  {
52  $result = [];
54  foreach ($source->getElementsByTagName('consumer') as $consumerNode) {
55  $consumerName = $this->getAttributeValue($consumerNode, 'name');
56  $handler = $this->getAttributeValue($consumerNode, 'handler');
57  $result[$consumerName] = [
58  'name' => $consumerName,
59  'queue' => $this->getAttributeValue($consumerNode, 'queue'),
60  'consumerInstance' => $this->getAttributeValue(
61  $consumerNode,
62  'consumerInstance',
63  self::$defaultInstance
64  ),
65  'handlers' => $handler ? [$this->parseHandler($handler)] : [],
66  'connection' => $this->getAttributeValue(
67  $consumerNode,
68  'connection',
69  $this->defaultValueProvider->getConnection()
70  ),
71  'maxMessages' => $this->getAttributeValue($consumerNode, 'maxMessages')
72  ];
73  }
74  return $result;
75  }
76 
85  private function getAttributeValue(\DOMNode $node, $attributeName, $default = null)
86  {
87  $item = $node->attributes->getNamedItem($attributeName);
88  return $item ? $item->nodeValue : $default;
89  }
90 
97  private function parseHandler($handler)
98  {
99  $parseServiceMethod = $this->configParser->parseServiceMethod($handler);
100  return [
101  CommunicationConfig::HANDLER_TYPE => $parseServiceMethod[ConfigParser::TYPE_NAME],
102  CommunicationConfig::HANDLER_METHOD => $parseServiceMethod[ConfigParser::METHOD_NAME]
103  ];
104  }
105 }
$source
Definition: source.php:23
__construct(ConfigParser $configParser, DefaultValueProvider $defaultValueProvider)
Definition: Converter.php:41
catch(\Exception $e) $handler
Definition: index.php:30