Issues (7)

src/Command/ConsumeCommand.php (1 issue)

Labels
Severity
1
<?php declare(strict_types = 1);
2
3
namespace Portiny\RabbitMQ\Command;
4
5
use Bunny\Channel;
6
use Portiny\RabbitMQ\BunnyManager;
7
use Portiny\RabbitMQ\Consumer\AbstractConsumer;
8
use Symfony\Component\Console\Attribute\AsCommand;
0 ignored issues
show
The type Symfony\Component\Console\Attribute\AsCommand was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
#[AsCommand(
15
	name: 'rabbitmq:consume',
16
	description: 'Run a RabbitMQ consumer'
17
)]
18
final class ConsumeCommand extends Command
19
{
20
21
	/**
22
	 * @var BunnyManager
23
	 */
24
	private $bunnyManager;
25
26
27
	public function __construct(BunnyManager $bunnyManager)
28
	{
29
		parent::__construct();
30
31
		$this->bunnyManager = $bunnyManager;
32
	}
33
34
35
	/**
36
	 * {@inheritdoc}
37
	 */
38
	protected function configure(): void
39
	{
40
		$this->setName('rabbitmq:consume')
41
			->setDescription('Run a RabbitMQ consumer')
42
			->addArgument('consumer', InputArgument::REQUIRED, 'FQDN or alias of the consumer')
43
			->addOption('messages', 'm', InputArgument::OPTIONAL, 'Amount of messages to consume')
44
			->addOption('time', 't', InputArgument::OPTIONAL, 'Max seconds for consumer to run');
45
	}
46
47
48
	/**
49
	 * {@inheritdoc}
50
	 */
51
	protected function execute(InputInterface $input, OutputInterface $output): int
52
	{
53
		/** @var string $consumerName */
54
		$consumerName = $input->getArgument('consumer');
55
		$numberOfMessages = $this->getNumberOfMessages($input);
56
		$secondsToRun = $this->getSecondsToRun($input);
57
58
		$output->writeln(
59
			sprintf(
60
				'<comment>[%s]</comment> <info>Starting consumer "%s"...</info>',
61
				date('Y-m-d H:i:s'),
62
				$consumerName
63
			)
64
		);
65
66
		$consumer = $this->bunnyManager->getConsumerByAlias($consumerName);
67
		if ($consumer === null) {
68
			$output->writeln('<error>Consumer not found!</error>');
69
			return -1;
70
		}
71
72
		/** @var Channel $channel */
73
		$channel = $this->bunnyManager->getChannel();
74
75
		$output->writeln('<info>Consuming...</info>');
76
77
		/** @var AbstractConsumer $consumer */
78
		$consumer->consume($channel, $numberOfMessages);
79
80
		$this->bunnyManager->getClient()->run($secondsToRun);
81
82
		return 0;
83
	}
84
85
86
	protected function getNumberOfMessages(InputInterface $input): ?int
87
	{
88
		/** @var string|int $messages */
89
		$messages = $input->getOption('messages');
90
91
		return $messages ? (int) $messages : null;
92
	}
93
94
95
	protected function getSecondsToRun(InputInterface $input): ?int
96
	{
97
		/** @var string|int $seconds */
98
		$seconds = $input->getOption('time');
99
100
		return $seconds ? (int) $seconds : null;
101
	}
102
103
}
104