Skip to content

Commit a429584

Browse files
author
Eugene Leonovich
committed
Refactor command classes
1 parent 8493212 commit a429584

6 files changed

Lines changed: 13 additions & 23 deletions

File tree

src/Console/Command/Command.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ class Command extends BaseCommand
1717
const ENV_USER = 'TNT_JOBQUEUE_USER';
1818
const ENV_PASSWORD = 'TNT_JOBQUEUE_PASSWORD';
1919

20-
private $configFactory;
21-
2220
protected function configure(): void
2321
{
2422
$this
@@ -30,20 +28,20 @@ protected function configure(): void
3028
;
3129
}
3230

33-
protected function initialize(InputInterface $input, OutputInterface $output): void
31+
protected function createConfigFactory(InputInterface $input, OutputInterface $output): DefaultConfigFactory
3432
{
3533
$customConfigPath = $input->getOption('config');
3634
if (null !== $customConfigPath && !is_readable($customConfigPath)) {
3735
throw new \RuntimeException("The given configuration file '$customConfigPath' does not exist or it's not readable.");
3836
}
3937

40-
$this->configFactory = $customConfigPath ? include $customConfigPath : new DefaultConfigFactory();
38+
$configFactory = $customConfigPath ? include $customConfigPath : new DefaultConfigFactory();
4139

4240
$uri = sprintf('tcp://%s:%s', $input->getOption('host'), $input->getOption('port'));
43-
$this->configFactory->setConnectionUri($uri);
41+
$configFactory->setConnectionUri($uri);
4442

4543
$queueName = $input->getArgument('queue');
46-
$this->configFactory->setQueueName($queueName);
44+
$configFactory->setQueueName($queueName);
4745

4846
$user = $input->getOption('user') ?: getenv(self::ENV_USER);
4947
if ($user) {
@@ -55,12 +53,9 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
5553
$password = $helper->ask($input, $output, $question);
5654
}
5755

58-
$this->configFactory->setCredentials($user, $password);
56+
$configFactory->setCredentials($user, $password);
5957
}
60-
}
6158

62-
protected function getConfigFactory(): DefaultConfigFactory
63-
{
64-
return $this->configFactory;
59+
return $configFactory;
6560
}
6661
}

src/Console/Command/KickCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ protected function configure(): void
2121

2222
protected function execute(InputInterface $input, OutputInterface $output): void
2323
{
24+
$queue = $this->createConfigFactory($input, $output)->createQueue();
2425
$count = $input->getArgument('count');
25-
$queue = $this->getConfigFactory()->createQueue();
2626

2727
$affected = $queue->kick($count);
2828

src/Console/Command/PutCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function execute(InputInterface $input, OutputInterface $output): void
2828
throw new \InvalidArgumentException('Invalid json data.');
2929
}
3030

31-
$queue = $this->getConfigFactory()->createQueue();
31+
$queue = $this->createConfigFactory($input, $output)->createQueue();
3232
$task = $queue->put($data);
3333

3434
$output->writeln(sprintf(

src/Console/Command/RunCommand.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@ protected function configure(): void
2525
;
2626
}
2727

28-
protected function initialize(InputInterface $input, OutputInterface $output): void
28+
protected function execute(InputInterface $input, OutputInterface $output): void
2929
{
30-
parent::initialize($input, $output);
31-
32-
$configFactory = $this->getConfigFactory();
30+
$configFactory = $this->createConfigFactory($input, $output);
3331

3432
if ($logFile = $input->getOption('log-file')) {
3533
$configFactory->setLogFile($logFile);
@@ -40,11 +38,8 @@ protected function initialize(InputInterface $input, OutputInterface $output): v
4038
if ($executorsConfigFile = $input->getOption('executors-config')) {
4139
$configFactory->setExecutorsConfigFile(realpath($executorsConfigFile));
4240
}
43-
}
4441

45-
protected function execute(InputInterface $input, OutputInterface $output): void
46-
{
47-
$runner = $this->getConfigFactory()->createRunner();
42+
$runner = $configFactory->createRunner();
4843
$runner->run($input->getOption('idle-timeout'));
4944
}
5045
}

src/Console/Command/StatsCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function configure(): void
1919

2020
protected function execute(InputInterface $input, OutputInterface $output): void
2121
{
22-
$queue = $this->getConfigFactory()->createQueue();
22+
$queue = $this->createConfigFactory($input, $output)->createQueue();
2323
$stats = $queue->stats();
2424

2525
$output->writeln(sprintf('Queue: <options=bold>%s</>', $queue->getName()));

src/Console/Command/TruncateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function configure(): void
1919

2020
protected function execute(InputInterface $input, OutputInterface $output): void
2121
{
22-
$queue = $this->getConfigFactory()->createQueue();
22+
$queue = $this->createConfigFactory($input, $output)->createQueue();
2323
$queue->truncate();
2424

2525
$output->writeln(sprintf('<info>%s</info> was successfully truncated.', $queue->getName()));

0 commit comments

Comments
 (0)