Skip to content

Commit

Permalink
Merge pull request #8 from olaurendeau/feature/one-define-to-rule-all…
Browse files Browse the repository at this point in the history
…-vhosts

Command define all configured vhost, not just default one but still specified one
  • Loading branch information
Olivier Laurendeau authored Apr 10, 2017
2 parents 20bd8e0 + c52dcff commit 513dcbb
Show file tree
Hide file tree
Showing 5 changed files with 270 additions and 537 deletions.
66 changes: 34 additions & 32 deletions Command/VhostDefineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,59 +29,61 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
try {
$this->comment($input, $output, sprintf(
'Define rabbitmq <info>%s</info> vhost configuration',
$this->getVhost($input)
));

$vhostConfiguration = $this->getVhostConfiguration($input);
$vhostHandler = $this->getContainer()->get('ola_rabbit_mq_admin_toolkit.handler.vhost');
$creation = !$vhostHandler->exists($vhostConfiguration);

$vhostHandler->define($vhostConfiguration);

$this->success($input, $output, sprintf(
'Rabbitmq "%s" vhost configuration successfully %s !',
$this->getVhost($input),
$creation ? 'created' : 'updated'
));
} catch (\Exception $e) {
if (!$this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.silent_failure')) {
throw $e;
$vhostList = $this->getVhostList($input);

foreach ($vhostList as $vhost) {
try {
$this->comment($input, $output, sprintf(
'Define rabbitmq <info>%s</info> vhost configuration',
$vhost
));

$vhostConfiguration = $this->getVhostConfiguration($vhost);
$vhostHandler = $this->getContainer()->get('ola_rabbit_mq_admin_toolkit.handler.vhost');
$creation = !$vhostHandler->exists($vhostConfiguration);

$vhostHandler->define($vhostConfiguration);

$this->success($input, $output, sprintf(
'Rabbitmq "%s" vhost configuration successfully %s !',
$vhost,
$creation ? 'created' : 'updated'
));
} catch (\Exception $e) {
if (!$this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.silent_failure')) {
throw $e;
}
}
}
}

/**
* Retrieve vhost's name to process
* Return Vhosts to process
*
* @param InputInterface $input
*
* @return string
* @return array
*/
private function getVhost(InputInterface $input)
private function getVhostList(InputInterface $input)
{
$vhost = $input->getArgument('vhost');
if (empty($vhost)) {
$vhost = $this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.default_vhost');
$inputVhost = $input->getArgument('vhost');
$vhostList = array($inputVhost);
if (empty($inputVhost)) {
$vhostList = $this->getContainer()->getParameter('ola_rabbit_mq_admin_toolkit.vhost_list');
}

return $vhost;
return $vhostList;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @param string $vhost
*
* @return VhostConfiguration
*
* @throws \InvalidArgumentException
*/
private function getVhostConfiguration(InputInterface $input)
private function getVhostConfiguration($vhost)
{
$vhost = $this->getVhost($input);

$serviceName = sprintf(
OlaRabbitMqAdminToolkitExtension::VHOST_MANAGER_SERVICE_TEMPLATE,
$vhost
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/OlaRabbitMqAdminToolkitExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function load(array $configs, ContainerBuilder $container)
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$container->setParameter(sprintf(self::PARAMETER_TEMPLATE, 'default_vhost'), $config['default_vhost']);
$container->setParameter(sprintf(self::PARAMETER_TEMPLATE, 'vhost_list'), array_keys($config['vhosts']));
$container->setParameter(sprintf(self::PARAMETER_TEMPLATE, 'silent_failure'), $config['silent_failure']);

$this->loadConnections($config['connections'], $container);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Command/VhostDefineCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function setUp()
$this->container = $this->prophesize('Symfony\Component\DependencyInjection\ContainerInterface');
$this->container->get('ola_rabbit_mq_admin_toolkit.handler.vhost')->willReturn($this->handler->reveal());
$this->container->getParameter('ola_rabbit_mq_admin_toolkit.silent_failure')->willReturn(false);
$this->container->getParameter('ola_rabbit_mq_admin_toolkit.default_vhost')->willReturn('foo');
$this->container->getParameter('ola_rabbit_mq_admin_toolkit.vhost_list')->willReturn(array('foo'));

$this->command = new VhostDefineCommand();
$this->command->setApplication($this->application);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function test_load_successfull()
));
$this->assertContainerBuilderHasService('ola_rabbit_mq_admin_toolkit.connection.default');
$this->assertContainerBuilderHasService('ola_rabbit_mq_admin_toolkit.configuration.test');
$this->assertContainerBuilderHasParameter('ola_rabbit_mq_admin_toolkit.default_vhost');
$this->assertContainerBuilderHasParameter('ola_rabbit_mq_admin_toolkit.vhost_list');
}

public function dataProvider_load_failBecauseModulusIsImproperlyDefined()
Expand Down
Loading

0 comments on commit 513dcbb

Please sign in to comment.