Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Config.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework\Amqp;
7 
11 use PhpAmqpLib\Connection\AbstractConnection;
12 use PhpAmqpLib\Channel\AMQPChannel;
13 use Magento\Framework\Amqp\Connection\Factory as ConnectionFactory;
14 
21 class Config
22 {
26  const QUEUE_CONFIG = 'queue';
27 
31  const AMQP_CONFIG = 'amqp';
32 
33  const HOST = 'host';
34  const PORT = 'port';
35  const USERNAME = 'user';
36  const PASSWORD = 'password';
37  const VIRTUALHOST = 'virtualhost';
38  const SSL = 'ssl';
39  const SSL_OPTIONS = 'ssl_options';
40 
46  private $deploymentConfig;
47 
51  private $connection;
52 
56  private $channel;
57 
63  private $data;
64 
70  private $connectionName;
71 
75  private $connectionFactory;
76 
101  public function __construct(
103  $connectionName = 'amqp',
104  ConnectionFactory $connectionFactory = null
105  ) {
106  $this->deploymentConfig = $config;
107  $this->connectionName = $connectionName;
108  $this->connectionFactory = $connectionFactory
109  ?: ObjectManager::getInstance()->get(ConnectionFactory::class);
110  }
111 
118  public function __destruct()
119  {
120  $this->closeConnection();
121  }
122 
131  public function getValue($key)
132  {
133  $this->load();
134  return isset($this->data[$key]) ? $this->data[$key] : null;
135  }
136 
140  private function createConnection(): AbstractConnection
141  {
142  $sslEnabled = trim($this->getValue(self::SSL)) === 'true';
143  $options = new FactoryOptions();
144  $options->setHost($this->getValue(self::HOST));
145  $options->setPort($this->getValue(self::PORT));
146  $options->setUsername($this->getValue(self::USERNAME));
147  $options->setPassword($this->getValue(self::PASSWORD));
148  $options->setVirtualHost($this->getValue(self::VIRTUALHOST));
149  $options->setSslEnabled($sslEnabled);
151  if ($sslOptions = $this->getValue(self::SSL_OPTIONS)) {
152  $options->setSslOptions($sslOptions);
153  }
154 
155  return $this->connectionFactory->create($options);
156  }
157 
165  public function getChannel()
166  {
167  if (!isset($this->connection) || !isset($this->channel)) {
168  $this->connection = $this->createConnection();
169 
170  $this->channel = $this->connection->channel();
171  }
172  return $this->channel;
173  }
174 
181  private function load()
182  {
183  if (null === $this->data) {
184  $queueConfig = $this->deploymentConfig->getConfigData(self::QUEUE_CONFIG);
185  if ($this->connectionName == self::AMQP_CONFIG) {
186  $this->data = isset($queueConfig[self::AMQP_CONFIG]) ? $queueConfig[self::AMQP_CONFIG] : [];
187  } else {
188  $this->data = isset($queueConfig['connections'][$this->connectionName])
189  ? $queueConfig['connections'][$this->connectionName]
190  : [];
191  }
192  if (empty($this->data)) {
193  throw new \LogicException('Unknown connection name ' . $this->connectionName);
194  }
195  }
196  }
197 
203  private function closeConnection()
204  {
205  if (isset($this->channel)) {
206  $this->channel->close();
207  unset($this->channel);
208  }
209 
210  if (isset($this->connection)) {
211  $this->connection->close();
212  unset($this->connection);
213  }
214  }
215 }
$config
Definition: fraud_order.php:17
__construct(DeploymentConfig $config, $connectionName='amqp', ConnectionFactory $connectionFactory=null)
Definition: Config.php:101