2014-12-04 00:04:47 +01:00
|
|
|
<?php
|
2014-12-09 23:48:16 +01:00
|
|
|
|
2014-12-04 00:04:47 +01:00
|
|
|
/**
|
|
|
|
* User: sourcefabric
|
2021-10-11 16:10:47 +02:00
|
|
|
* Date: 02/12/14.
|
2014-12-04 00:04:47 +01:00
|
|
|
*
|
|
|
|
* Class RabbitMQSetup
|
|
|
|
*
|
|
|
|
* Wrapper class for validating and setting up RabbitMQ during the installation process
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
class RabbitMQSetup extends Setup
|
|
|
|
{
|
2022-06-06 17:09:25 +02:00
|
|
|
// config.yml section header
|
2021-10-11 16:10:47 +02:00
|
|
|
protected static $_section = '[rabbitmq]';
|
2015-06-24 01:02:55 +02:00
|
|
|
|
2022-06-06 17:09:25 +02:00
|
|
|
// Array of key->value pairs for config.yml
|
2015-06-24 01:02:55 +02:00
|
|
|
protected static $_properties;
|
2014-12-09 23:48:16 +01:00
|
|
|
|
|
|
|
// Constant form field names for passing errors back to the front-end
|
2021-10-11 16:10:47 +02:00
|
|
|
public const RMQ_USER = 'rmqUser';
|
|
|
|
public const RMQ_PASS = 'rmqPass';
|
|
|
|
public const RMQ_PORT = 'rmqPort';
|
|
|
|
public const RMQ_HOST = 'rmqHost';
|
|
|
|
public const RMQ_VHOST = 'rmqVHost';
|
2014-12-04 00:04:47 +01:00
|
|
|
|
2014-12-09 23:48:16 +01:00
|
|
|
// Message and error fields to return to the front-end
|
2021-10-11 16:10:47 +02:00
|
|
|
public static $message;
|
|
|
|
public static $errors = [];
|
2014-12-04 00:04:47 +01:00
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
public function __construct($settings)
|
|
|
|
{
|
|
|
|
static::$_properties = [
|
|
|
|
'host' => $settings[self::RMQ_HOST],
|
|
|
|
'port' => $settings[self::RMQ_PORT],
|
|
|
|
'user' => $settings[self::RMQ_USER],
|
|
|
|
'password' => $settings[self::RMQ_PASS],
|
|
|
|
'vhost' => $settings[self::RMQ_VHOST],
|
|
|
|
];
|
2014-12-04 00:04:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array associative array containing a display message and fields with errors
|
|
|
|
*/
|
2021-10-11 16:10:47 +02:00
|
|
|
public function runSetup()
|
|
|
|
{
|
2014-12-09 23:48:16 +01:00
|
|
|
try {
|
|
|
|
if ($this->checkRMQConnection()) {
|
2021-10-11 16:10:47 +02:00
|
|
|
self::$message = 'Connection successful!';
|
2014-12-09 23:48:16 +01:00
|
|
|
} else {
|
|
|
|
$this->identifyRMQConnectionError();
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
} catch (Exception $e) {
|
2014-12-09 23:48:16 +01:00
|
|
|
$this->identifyRMQConnectionError();
|
|
|
|
}
|
2021-10-11 16:10:47 +02:00
|
|
|
|
|
|
|
return [
|
|
|
|
'message' => self::$message,
|
|
|
|
'errors' => self::$errors,
|
|
|
|
];
|
2014-12-04 00:04:47 +01:00
|
|
|
}
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
public function checkRMQConnection()
|
|
|
|
{
|
2021-10-17 21:55:12 +02:00
|
|
|
$conn = new \PhpAmqpLib\Connection\AMQPStreamConnection(
|
2021-10-11 16:10:47 +02:00
|
|
|
self::$_properties['host'],
|
|
|
|
self::$_properties['port'],
|
|
|
|
self::$_properties['user'],
|
|
|
|
self::$_properties['password'],
|
|
|
|
self::$_properties['vhost']
|
|
|
|
);
|
2017-11-13 17:39:11 +01:00
|
|
|
$this->writeToTemp();
|
2021-10-11 16:10:47 +02:00
|
|
|
|
2014-12-09 23:48:16 +01:00
|
|
|
return isset($conn);
|
|
|
|
}
|
|
|
|
|
2021-10-11 16:10:47 +02:00
|
|
|
public function identifyRMQConnectionError()
|
|
|
|
{
|
2014-12-09 23:48:16 +01:00
|
|
|
// It's impossible to identify errors coming out of amqp.inc without a major
|
|
|
|
// rewrite, so for now just tell the user ALL THE THINGS went wrong
|
2015-06-24 01:02:55 +02:00
|
|
|
self::$message = _("Couldn't connect to RabbitMQ server! Please check if the server "
|
2021-10-11 16:10:47 +02:00
|
|
|
. 'is running and your credentials are correct.');
|
2014-12-09 23:48:16 +01:00
|
|
|
self::$errors[] = self::RMQ_USER;
|
|
|
|
self::$errors[] = self::RMQ_PASS;
|
|
|
|
self::$errors[] = self::RMQ_HOST;
|
|
|
|
self::$errors[] = self::RMQ_PORT;
|
|
|
|
self::$errors[] = self::RMQ_VHOST;
|
|
|
|
}
|
2017-07-18 22:27:19 +02:00
|
|
|
}
|