Skip to content

Commit 726e896

Browse files
authored
Merge pull request #5 from ReputationVIP/adopt-symfony-4
Adopt Symfony 4
2 parents 2643db2 + b989531 commit 726e896

15 files changed

+189
-318
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Queue Client Bundle Changelog
22

3+
## v2.0.0
4+
5+
- Use autowire
6+
- Explicitly declare Commands
7+
- [BR] Symfony 3.3 is now required
8+
39
## v1.1.1
410

511
- Remove debug code

Command/AddMessagesCommand.php

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,24 @@
22

33
namespace ReputationVIP\Bundle\QueueClientBundle\Command;
44

5-
use Psr\Log\LoggerInterface;
6-
use ReputationVIP\Bundle\QueueClientBundle\Utils\Output;
75
use ReputationVIP\QueueClient\QueueClientInterface;
8-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6+
use Symfony\Component\Console\Command\Command;
97
use Symfony\Component\Console\Input\InputArgument;
108
use Symfony\Component\Console\Input\InputInterface;
119
use Symfony\Component\Console\Input\InputOption;
1210
use Symfony\Component\Console\Output\OutputInterface;
13-
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1411

15-
class AddMessagesCommand extends ContainerAwareCommand
12+
class AddMessagesCommand extends Command
1613
{
17-
/**
18-
* @var Output $output
19-
*/
20-
private $output;
14+
/** @var QueueClientInterface */
15+
private $queueClient;
16+
17+
public function __construct(QueueClientInterface $queueClient)
18+
{
19+
parent::__construct();
20+
21+
$this->queueClient = $queueClient;
22+
}
2123

2224
protected function configure()
2325
{
@@ -27,7 +29,8 @@ protected function configure()
2729
->addOption('priority', 'p', InputOption::VALUE_OPTIONAL, 'Add in queue with specific priority')
2830
->addArgument('queueName', InputArgument::REQUIRED, 'queue')
2931
->addArgument('messages', InputArgument::IS_ARRAY, 'messages to add')
30-
->setHelp('This command add messages in queue.');
32+
->setHelp('This command add messages in queue.')
33+
;
3134
}
3235

3336
/**
@@ -37,28 +40,18 @@ protected function configure()
3740
*/
3841
protected function execute(InputInterface $input, OutputInterface $output)
3942
{
40-
try {
41-
/** @var LoggerInterface $logger */
42-
$logger = $this->getContainer()->get('logger');
43-
} catch (ServiceNotFoundException $e) {
44-
$logger = null;
45-
}
46-
$this->output = new Output($logger, $output);
47-
/** @var QueueClientInterface $queueClient */
48-
$queueClient = $this->getContainer()->get('queue_client');
49-
5043
$priority = null;
5144
if ($input->getOption('priority')) {
5245
$priority = $input->getOption('priority');
53-
if (!in_array($priority, $queueClient->getPriorityHandler()->getAll())) {
46+
if (!in_array($priority, $this->queueClient->getPriorityHandler()->getAll())) {
5447
throw new \InvalidArgumentException('Priority "' . $priority . '" not found.');
5548
}
5649
}
5750

5851
$queueName = $input->getArgument('queueName');
5952
$messages = $input->getArgument('messages');
6053

61-
$queueClient->addMessages($queueName, $messages, $priority);
54+
$this->queueClient->addMessages($queueName, $messages, $priority);
6255

6356
return 0;
6457
}

Command/CreateQueuesCommand.php

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
namespace ReputationVIP\Bundle\QueueClientBundle\Command;
44

55
use InvalidArgumentException;
6-
use Psr\Log\LoggerInterface;
7-
use ReputationVIP\Bundle\QueueClientBundle\Utils\Output;
86
use ReputationVIP\Bundle\QueueClientBundle\Configuration\QueuesConfiguration;
97
use ReputationVIP\QueueClient\QueueClientInterface;
108
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
@@ -13,15 +11,19 @@
1311
use Symfony\Component\Console\Input\InputInterface;
1412
use Symfony\Component\Console\Input\InputOption;
1513
use Symfony\Component\Console\Output\OutputInterface;
16-
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1714
use Symfony\Component\Yaml\Yaml;
1815

1916
class CreateQueuesCommand extends ContainerAwareCommand
2017
{
21-
/**
22-
* @var Output $output
23-
*/
24-
private $output;
18+
/** @var QueueClientInterface */
19+
private $queueClient;
20+
21+
public function __construct(QueueClientInterface $queueClient)
22+
{
23+
parent::__construct();
24+
25+
$this->queueClient = $queueClient;
26+
}
2527

2628
protected function configure()
2729
{
@@ -47,42 +49,44 @@ protected function configure()
4749
}
4850

4951
/**
52+
* @param OutputInterface $output
5053
* @param QueueClientInterface $queueClient
5154
* @param string $fileName
55+
*
5256
* @return int
5357
*/
54-
private function createFromFile($queueClient, $fileName)
58+
private function createFromFile(OutputInterface $output, QueueClientInterface $queueClient, $fileName)
5559
{
5660
try {
5761
$processor = new Processor();
5862
$configuration = new QueuesConfiguration();
5963
$processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($fileName)));
6064

6165
} catch (\Exception $e) {
62-
$this->output->write($e->getMessage(), Output::CRITICAL);
66+
$output->writeln($e->getMessage());
6367

6468
return 1;
6569
}
6670
array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $this->getContainer());
67-
$this->output->write('Start create queue.', Output::INFO);
71+
$output->writeln('Start create queue.');
6872
foreach ($processedConfiguration[QueuesConfiguration::QUEUES_NODE] as $queue) {
6973
$queueName = $queue[QueuesConfiguration::QUEUE_NAME_NODE];
7074
try {
7175
$queueClient->createQueue($queueName);
72-
$this->output->write('Queue ' . $queueName . ' created.', Output::INFO);
76+
$output->writeln('Queue ' . $queueName . ' created.');
7377
} catch (\Exception $e) {
74-
$this->output->write($e->getMessage(), Output::WARNING);
78+
$output->writeln($e->getMessage());
7579
}
7680
foreach ($queue[QueuesConfiguration::QUEUE_ALIASES_NODE] as $alias) {
7781
try {
7882
$queueClient->addAlias($queueName, $alias);
79-
$this->output->write('Queue alias ' . $alias . ' -> ' . $queueName . ' found.', Output::INFO);
83+
$output->writeln('Queue alias ' . $alias . ' -> ' . $queueName . ' found.');
8084
} catch (\Exception $e) {
81-
$this->output->write($e->getMessage(), Output::WARNING);
85+
$output->writeln($e->getMessage());
8286
}
8387
}
8488
}
85-
$this->output->write('End create queue.', Output::INFO);
89+
$output->writeln('End create queue.');
8690

8791
return 0;
8892
}
@@ -94,34 +98,19 @@ private function createFromFile($queueClient, $fileName)
9498
*/
9599
protected function execute(InputInterface $input, OutputInterface $output)
96100
{
97-
try {
98-
/** @var LoggerInterface $logger */
99-
$logger = $this->getContainer()->get('logger');
100-
} catch (ServiceNotFoundException $e) {
101-
$logger = null;
102-
}
103-
$this->output = new Output($logger, $output);
104-
try {
105-
/** @var QueueClientInterface $queueClient */
106-
$queueClient = $this->getContainer()->get('queue_client');
107-
} catch (ServiceNotFoundException $e) {
108-
$this->output->write('No queue client service found.', Output::CRITICAL);
109-
110-
return 1;
111-
}
112101
if ($input->getOption('file')) {
113102
$fileName = $input->getOption('file');
114103

115-
return $this->createFromFile($queueClient, $fileName);
104+
return $this->createFromFile($output, $this->queueClient, $fileName);
116105
} else {
117106
$queues = $input->getArgument('queues');
118107
if (count($queues)) {
119108
foreach ($queues as $queue) {
120109
try {
121-
$queueClient->createQueue($queue);
122-
$this->output->write('Queue ' . $queue . ' created.', Output::INFO);
110+
$this->queueClient->createQueue($queue);
111+
$output->writeln('Queue ' . $queue . ' created.');
123112
} catch (\Exception $e) {
124-
$this->output->write($e->getMessage(), Output::WARNING);
113+
$output->writeln($e->getMessage());
125114
}
126115
}
127116

@@ -130,9 +119,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
130119
try {
131120
$fileName = $this->getContainer()->getParameter('queue_client.queues_file');
132121

133-
return $this->createFromFile($queueClient, $fileName);
122+
return $this->createFromFile($output, $this->queueClient, $fileName);
134123
} catch (InvalidArgumentException $e) {
135-
$this->output->write('No queue_client.queues_file parameter found.', Output::CRITICAL);
124+
$output->writeln('No queue_client.queues_file parameter found.');
136125

137126
return 1;
138127
}

Command/DeleteQueuesCommand.php

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace ReputationVIP\Bundle\QueueClientBundle\Command;
44

55
use InvalidArgumentException;
6-
use Psr\Log\LoggerInterface;
76
use ReputationVIP\Bundle\QueueClientBundle\Configuration\QueuesConfiguration;
8-
use ReputationVIP\Bundle\QueueClientBundle\Utils\Output;
97
use ReputationVIP\QueueClient\QueueClientInterface;
108
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
119
use Symfony\Component\Config\Definition\Processor;
@@ -14,15 +12,19 @@
1412
use Symfony\Component\Console\Input\InputOption;
1513
use Symfony\Component\Console\Output\OutputInterface;
1614
use Symfony\Component\Console\Question\ConfirmationQuestion;
17-
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1815
use Symfony\Component\Yaml\Yaml;
1916

2017
class DeleteQueuesCommand extends ContainerAwareCommand
2118
{
22-
/**
23-
* @var Output $output
24-
*/
25-
private $output;
19+
/** @var QueueClientInterface */
20+
private $queueClient;
21+
22+
public function __construct(QueueClientInterface $queueClient)
23+
{
24+
parent::__construct();
25+
26+
$this->queueClient = $queueClient;
27+
}
2628

2729
protected function configure()
2830
{
@@ -49,34 +51,35 @@ protected function configure()
4951
}
5052

5153
/**
52-
* @param QueueClientInterface $queueClient
54+
* @param OutputInterface $output
5355
* @param string $fileName
56+
*
5457
* @return int
5558
*/
56-
private function deleteFromFile($queueClient, $fileName)
59+
private function deleteFromFile(OutputInterface $output, $fileName)
5760
{
5861
try {
5962
$processor = new Processor();
6063
$configuration = new QueuesConfiguration();
6164
$processedConfiguration = $processor->processConfiguration($configuration, Yaml::parse(file_get_contents($fileName)));
6265

6366
} catch (\Exception $e) {
64-
$this->output->write($e->getMessage(), Output::CRITICAL);
67+
$output->write($e->getMessage());
6568

6669
return 1;
6770
}
6871
array_walk_recursive($processedConfiguration, 'ReputationVIP\Bundle\QueueClientBundle\QueueClientFactory::resolveParameters', $this->getContainer());
69-
$this->output->write('Start delete queue.', Output::INFO);
72+
$output->write('Start delete queue.');
7073
foreach ($processedConfiguration[QueuesConfiguration::QUEUES_NODE] as $queue) {
7174
$queueName = $queue[QueuesConfiguration::QUEUE_NAME_NODE];
7275
try {
73-
$queueClient->deleteQueue($queueName);
74-
$this->output->write('Queue ' . $queueName . ' deleted.', Output::INFO);
76+
$this->queueClient->deleteQueue($queueName);
77+
$output->write('Queue ' . $queueName . ' deleted.');
7578
} catch (\Exception $e) {
76-
$this->output->write($e->getMessage(), Output::WARNING);
79+
$output->write($e->getMessage());
7780
}
7881
}
79-
$this->output->write('End delete queue.', Output::INFO);
82+
$output->write('End delete queue.');
8083

8184
return 0;
8285
}
@@ -90,29 +93,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
9093
{
9194
$helper = $this->getHelper('question');
9295
$force = $input->getOption('force') ? true : false;
93-
try {
94-
/** @var LoggerInterface $logger */
95-
$logger = $this->getContainer()->get('logger');
96-
} catch (ServiceNotFoundException $e) {
97-
$logger = null;
98-
}
99-
$this->output = new Output($logger, $output);
100-
try {
101-
/** @var QueueClientInterface $queueClient */
102-
$queueClient = $this->getContainer()->get('queue_client');
103-
} catch (ServiceNotFoundException $e) {
104-
$this->output->write('No queue client service found.', Output::CRITICAL);
10596

106-
return 1;
107-
}
10897
if ($input->getOption('file')) {
10998
$fileName = $input->getOption('file');
11099
if (!($force || $helper->ask($input, $output, new ConfirmationQuestion('Delete queues in file "' . $fileName . '"?', false)))) {
111100

112101
return 0;
113102
}
114103

115-
return $this->deleteFromFile($queueClient, $fileName);
104+
return $this->deleteFromFile($output, $fileName);
116105
} else {
117106
$queues = $input->getArgument('queues');
118107
if (count($queues)) {
@@ -122,10 +111,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
122111
}
123112
foreach ($queues as $queue) {
124113
try {
125-
$queueClient->deleteQueue($queue);
126-
$this->output->write('Queue ' . $queue . ' deleted.', Output::INFO);
114+
$this->queueClient->deleteQueue($queue);
115+
$output->write('Queue ' . $queue . ' deleted.');
127116
} catch (\Exception $e) {
128-
$this->output->write($e->getMessage(), Output::WARNING);
117+
$output->write($e->getMessage());
129118
}
130119
}
131120

@@ -138,9 +127,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
138127
return 0;
139128
}
140129

141-
return $this->deleteFromFile($queueClient, $fileName);
130+
return $this->deleteFromFile($output, $fileName);
142131
} catch (InvalidArgumentException $e) {
143-
$this->output->write('No queue_client.queues_file parameter found.', Output::CRITICAL);
132+
$output->write('No queue_client.queues_file parameter found.');
144133

145134
return 1;
146135
}

0 commit comments

Comments
 (0)