Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataMapper.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Framework\Communication\ConfigInterface as CommunicationConfig;
13 
18 {
24  private $mappedData;
25 
29  private $configData;
30 
34  private $communicationConfig;
35 
39  private $queueNameBuilder;
40 
48  public function __construct(
49  Data $configData,
50  CommunicationConfig $communicationConfig,
51  ResponseQueueNameBuilder $queueNameBuilder
52  ) {
53  $this->configData = $configData;
54  $this->communicationConfig = $communicationConfig;
55  $this->queueNameBuilder = $queueNameBuilder;
56  }
57 
63  public function getMappedData()
64  {
65  if (null === $this->mappedData) {
66  $this->mappedData = [];
67  foreach ($this->configData->get() as $exchange) {
68  $connection = $exchange['connection'];
69  foreach ($exchange['bindings'] as $binding) {
70  if ($binding['destinationType'] === 'queue') {
71  $queueItems = $this->createQueueItems($binding['destination'], $binding['topic'], $connection);
72  $this->mappedData = array_merge($this->mappedData, $queueItems);
73  }
74  }
75  }
76  }
77  return $this->mappedData;
78  }
79 
88  private function createQueueItems($name, $topic, $connection)
89  {
90  $output = [];
91  $synchronousTopics = [];
92 
93  if (strpos($topic, '*') !== false || strpos($topic, '#') !== false) {
94  $synchronousTopics = $this->matchSynchronousTopics($topic);
95  } elseif ($this->isSynchronousTopic($topic)) {
96  $synchronousTopics[$topic] = $topic;
97  }
98 
99  foreach ($synchronousTopics as $topicName) {
100  $callbackQueueName = $this->queueNameBuilder->getQueueName($topicName);
101  $output[$callbackQueueName . '--' . $connection] = [
102  'name' => $callbackQueueName,
103  'connection' => $connection,
104  'durable' => true,
105  'autoDelete' => false,
106  'arguments' => [],
107  ];
108  }
109 
110  $output[$name . '--' . $connection] = [
111  'name' => $name,
112  'connection' => $connection,
113  'durable' => true,
114  'autoDelete' => false,
115  'arguments' => [],
116  ];
117  return $output;
118  }
119 
127  private function isSynchronousTopic($topicName)
128  {
129  try {
130  $topic = $this->communicationConfig->getTopic($topicName);
131  $isSync = (bool)$topic[CommunicationConfig::TOPIC_IS_SYNCHRONOUS];
132  } catch (LocalizedException $e) {
133  throw new LocalizedException(new Phrase('Error while checking if topic is synchronous'));
134  }
135  return $isSync;
136  }
137 
144  private function matchSynchronousTopics($wildcard)
145  {
146  $topicDefinitions = array_filter(
147  $this->communicationConfig->getTopics(),
148  function ($item) {
149  return (bool)$item[CommunicationConfig::TOPIC_IS_SYNCHRONOUS];
150  }
151  );
152 
153  $topics = [];
154  $pattern = $this->buildWildcardPattern($wildcard);
155  foreach (array_keys($topicDefinitions) as $topicName) {
156  if (preg_match($pattern, $topicName)) {
157  $topics[$topicName] = $topicName;
158  }
159  }
160  return $topics;
161  }
162 
169  private function buildWildcardPattern($wildcardKey)
170  {
171  $pattern = '/^' . str_replace('.', '\.', $wildcardKey);
172  $pattern = str_replace('#', '.+', $pattern);
173  $pattern = str_replace('*', '[^\.]+', $pattern);
174  $pattern .= strpos($wildcardKey, '#') === strlen($wildcardKey) ? '/' : '$/';
175  return $pattern;
176  }
177 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(Data $configData, CommunicationConfig $communicationConfig, ResponseQueueNameBuilder $queueNameBuilder)
Definition: DataMapper.php:48
$pattern
Definition: website.php:22
$connection
Definition: bulk.php:13
if(!isset($_GET['name'])) $name
Definition: log.php:14