Skip to content

Commit 8a11086

Browse files
committed
refactor
1 parent 5df4969 commit 8a11086

File tree

7 files changed

+165
-166
lines changed

7 files changed

+165
-166
lines changed

rector.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
6+
use Rector\Config\RectorConfig;
7+
use Rector\Set\ValueObject\LevelSetList;
8+
use Rector\Set\ValueObject\SetList;
9+
10+
return static function (RectorConfig $rectorConfig): void {
11+
$rectorConfig->paths([
12+
__DIR__ . '/Config',
13+
__DIR__ . '/src',
14+
]);
15+
16+
// register a single rule
17+
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
18+
19+
// define sets of rules
20+
$rectorConfig->sets([
21+
LevelSetList::UP_TO_PHP_81,
22+
SetList::CODE_QUALITY,
23+
SetList::CODING_STYLE,
24+
SetList::NAMING,
25+
SetList::PRIVATIZATION,
26+
SetList::TYPE_DECLARATION
27+
]);
28+
};

src/Connectors/RabbitMQConnector.php

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,45 @@
1010
use PhpAmqpLib\Connection\AMQPConnectionConfig;
1111
use PhpAmqpLib\Connection\AMQPConnectionFactory;
1212

13-
class RabbitMQConnector implements ConnectorInterface
13+
final class RabbitMQConnector implements ConnectorInterface
1414
{
15-
private readonly Dispatcher $dispatcher;
16-
17-
public function __construct(Dispatcher $dispatcher)
15+
public function __construct(private readonly Dispatcher $dispatcher)
1816
{
19-
$this->dispatcher = $dispatcher;
2017
}
2118

2219
public function connect(array $config = []): Queue
2320
{
24-
$configs = new AMQPConnectionConfig();
21+
$amqpConnectionConfig = new AMQPConnectionConfig();
2522

2623
// set AMQP account
27-
$configs->setHost(config('queue.connections.rabbitmq.hosts.host'));
28-
$configs->setPort(config('queue.connections.rabbitmq.hosts.port'));
29-
$configs->setUser(config('queue.connections.rabbitmq.hosts.user'));
30-
$configs->setPassword(config('queue.connections.rabbitmq.hosts.password'));
31-
$configs->setVhost(config('queue.connections.rabbitmq.hosts.vhost'));
24+
$amqpConnectionConfig->setHost(config('queue.connections.rabbitmq.hosts.host'));
25+
$amqpConnectionConfig->setPort(config('queue.connections.rabbitmq.hosts.port'));
26+
$amqpConnectionConfig->setUser(config('queue.connections.rabbitmq.hosts.user'));
27+
$amqpConnectionConfig->setPassword(config('queue.connections.rabbitmq.hosts.password'));
28+
$amqpConnectionConfig->setVhost(config('queue.connections.rabbitmq.hosts.vhost'));
3229

33-
$configs->setIsLazy(config('queue.connections.rabbitmq.hosts.lazy'));
34-
$configs->setKeepalive(config('queue.connections.rabbitmq.hosts.keepalive'));
35-
$configs->setHeartbeat(config('queue.connections.rabbitmq.hosts.heartbeat'));
30+
$amqpConnectionConfig->setIsLazy(config('queue.connections.rabbitmq.hosts.lazy'));
31+
$amqpConnectionConfig->setKeepalive(config('queue.connections.rabbitmq.hosts.keepalive'));
32+
$amqpConnectionConfig->setHeartbeat(config('queue.connections.rabbitmq.hosts.heartbeat'));
3633

3734

3835
// set SSL Options
39-
$configs->setSslCaCert(config('queue.connections.rabbitmq.options.ssl_options.cafile'));
40-
$configs->setSslCert(config('queue.connections.rabbitmq.options.ssl_options.local_cert'));
41-
$configs->setSslKey(config('queue.connections.rabbitmq.options.ssl_options.local_key'));
42-
$configs->setSslVerify(config('queue.connections.rabbitmq.options.ssl_options.verify_peer'));
43-
$configs->setSslPassPhrase(config('queue.connections.rabbitmq.options.ssl_options.passphrase'));
36+
$amqpConnectionConfig->setSslCaCert(config('queue.connections.rabbitmq.options.ssl_options.cafile'));
37+
$amqpConnectionConfig->setSslCert(config('queue.connections.rabbitmq.options.ssl_options.local_cert'));
38+
$amqpConnectionConfig->setSslKey(config('queue.connections.rabbitmq.options.ssl_options.local_key'));
39+
$amqpConnectionConfig->setSslVerify(config('queue.connections.rabbitmq.options.ssl_options.verify_peer'));
40+
$amqpConnectionConfig->setSslPassPhrase(config('queue.connections.rabbitmq.options.ssl_options.passphrase'));
4441

4542
// Create AMQP Connection
46-
$connection = AMQPConnectionFactory::create($configs);
43+
$connection = AMQPConnectionFactory::create($amqpConnectionConfig);
4744
$defaultQueue = config('queue.connections.rabbitmq.queue');
4845

49-
$queueConnection = new RabbitQueue($connection, $defaultQueue);
46+
$rabbitQueue = new RabbitQueue($connection, $defaultQueue);
5047

51-
$this->dispatcher->listen(WorkerStopping::class, static function () use ($queueConnection): void {
52-
$queueConnection->close();
48+
$this->dispatcher->listen(WorkerStopping::class, static function () use ($rabbitQueue): void {
49+
$rabbitQueue->close();
5350
});
5451

55-
return $queueConnection;
52+
return $rabbitQueue;
5653
}
5754
}

src/Console/ConsumeCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use Symfony\Component\Console\Attribute\AsCommand;
99

1010
#[AsCommand(name: 'rabbitmq:consume')]
11-
class ConsumeCommand extends WorkCommand
11+
final class ConsumeCommand extends WorkCommand
1212
{
1313
protected $signature = 'rabbitmq:consume
1414
{connection? : The name of the queue connection to work}
@@ -49,7 +49,7 @@ public function handle(): void
4949
parent::handle();
5050
}
5151

52-
protected function consumerTag(): string
52+
private function consumerTag(): string
5353
{
5454
if ($consumerTag = $this->option('consumer-tag')) {
5555
return $consumerTag;

src/Consumer.php

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@
1212
use Throwable;
1313
use iamfarhad\LaravelRabbitMQ\RabbitQueue;
1414

15-
class Consumer extends Worker
15+
final class Consumer extends Worker
1616
{
17-
protected Container $container;
17+
private Container $container;
1818

19-
protected string $consumerTag;
19+
private string $consumerTag;
2020

21-
protected int $prefetchSize;
21+
private int $prefetchSize;
2222

23-
protected int $maxPriority;
23+
private int $maxPriority;
2424

25-
protected int $prefetchCount;
25+
private int $prefetchCount;
2626

27-
protected AMQPChannel $channel;
27+
private AMQPChannel $amqpChannel;
2828

29-
private $currentJob;
29+
private ?object $currentJob = null;
3030

31-
public function setContainer(Container $value): void
31+
public function setContainer(Container $container): void
3232
{
33-
$this->container = $value;
33+
$this->container = $container;
3434
}
3535

3636
public function setConsumerTag(string $value): void
@@ -58,62 +58,60 @@ public function setPrefetchCount(int $value): void
5858
*
5959
* @param string $connectionName
6060
* @param string $queue
61-
* @param WorkerOptions $options
6261
* @return int
63-
*
6462
* @throws Throwable
6563
*/
66-
public function daemon($connectionName, $queue, WorkerOptions $options)
64+
public function daemon($connectionName, $queue, WorkerOptions $workerOptions)
6765
{
6866
if ($this->supportsAsyncSignals()) {
6967
$this->listenForSignals();
7068
}
7169

72-
$lastRestart = $this->getTimestampOfLastQueueRestart();
73-
74-
[$startTime, $jobsProcessed] = [hrtime(true) / 1e9, 0];
70+
$timestampOfLastQueueRestart = $this->getTimestampOfLastQueueRestart();
71+
$startTime = hrtime(true) / 1e9;
72+
$jobsProcessed = 0;
7573

7674
$connection = $this->manager->connection($connectionName);
7775

78-
$this->channel = $connection->getChannel();
76+
$this->amqpChannel = $connection->getChannel();
7977

80-
$this->channel->basic_qos(
78+
$this->amqpChannel->basic_qos(
8179
$this->prefetchSize,
8280
$this->prefetchCount,
8381
null
8482
);
8583

8684
$jobClass = $connection->getJobClass();
8785
$arguments = [];
88-
if ($this->maxPriority) {
86+
if ($this->maxPriority !== 0) {
8987
$arguments['priority'] = ['I', $this->maxPriority];
9088
}
9189

92-
$this->channel->basic_consume(
90+
$this->amqpChannel->basic_consume(
9391
$queue,
9492
$this->consumerTag,
9593
false,
9694
false,
9795
false,
9896
false,
99-
function (AMQPMessage $message) use ($connection, $options, $connectionName, $queue, $jobClass, &$jobsProcessed): void {
97+
function (AMQPMessage $amqpMessage) use ($connection, $workerOptions, $connectionName, $queue, $jobClass, &$jobsProcessed): void {
10098
$job = new $jobClass(
10199
$this->container,
102100
$connection,
103-
$message,
101+
$amqpMessage,
104102
$connectionName,
105103
$queue
106104
);
107105

108106
$this->currentJob = $job;
109107

110108
if ($this->supportsAsyncSignals()) {
111-
$this->registerTimeoutHandler($job, $options);
109+
$this->registerTimeoutHandler($job, $workerOptions);
112110
}
113111

114-
$jobsProcessed++;
112+
++$jobsProcessed;
115113

116-
$this->runJob($job, $connectionName, $options);
114+
$this->runJob($job, $connectionName, $workerOptions);
117115

118116
if ($this->supportsAsyncSignals()) {
119117
$this->resetTimeoutHandler();
@@ -123,21 +121,21 @@ function (AMQPMessage $message) use ($connection, $options, $connectionName, $qu
123121
$arguments
124122
);
125123

126-
while ($this->channel->is_consuming()) {
124+
while ($this->amqpChannel->is_consuming()) {
127125
// Before reserving any jobs, we will make sure this queue is not paused and
128126
// if it is we will just pause this worker for a given amount of time and
129127
// make sure we do not need to kill this worker process off completely.
130-
if (! $this->daemonShouldRun($options, $connectionName, $queue)) {
131-
$this->pauseWorker($options, $lastRestart);
128+
if (! $this->daemonShouldRun($workerOptions, $connectionName, $queue)) {
129+
$this->pauseWorker($workerOptions, $timestampOfLastQueueRestart);
132130

133131
continue;
134132
}
135133

136134
// If the daemon should run (not in maintenance mode, etc.), then we can wait for a job.
137135
try {
138-
$this->channel->wait(null, true, (int) $options->timeout);
139-
} catch (AMQPRuntimeException $exception) {
140-
$this->exceptions->report($exception);
136+
$this->amqpChannel->wait(null, true, (int) $workerOptions->timeout);
137+
} catch (AMQPRuntimeException $amqpRuntimeException) {
138+
$this->exceptions->report($amqpRuntimeException);
141139

142140
$this->kill(1);
143141
} catch (Exception | Throwable $exception) {
@@ -148,15 +146,15 @@ function (AMQPMessage $message) use ($connection, $options, $connectionName, $qu
148146

149147
// If no job is got off the queue, we will need to sleep the worker.
150148
if ($this->currentJob === null) {
151-
$this->sleep($options->sleep);
149+
$this->sleep($workerOptions->sleep);
152150
}
153151

154152
// Finally, we will check to see if we have exceeded our memory limits or if
155153
// the queue should restart based on other indications. If so, we'll stop
156154
// this worker and let whatever is "monitoring" it restart the process.
157155
$status = $this->stopIfNecessary(
158-
$options,
159-
$lastRestart,
156+
$workerOptions,
157+
$timestampOfLastQueueRestart,
160158
$startTime,
161159
$jobsProcessed,
162160
$this->currentJob
@@ -173,21 +171,19 @@ function (AMQPMessage $message) use ($connection, $options, $connectionName, $qu
173171
/**
174172
* Determine if the daemon should process on this iteration.
175173
*
176-
* @param WorkerOptions $options
177174
* @param string $connectionName
178175
* @param string $queue
179-
* @return bool
180176
*/
181-
protected function daemonShouldRun(WorkerOptions $options, $connectionName, $queue): bool
177+
protected function daemonShouldRun(WorkerOptions $workerOptions, $connectionName, $queue): bool
182178
{
183-
return ! ((($this->isDownForMaintenance)() && ! $options->force) || $this->paused);
179+
return !(($this->isDownForMaintenance)() && ! $workerOptions->force) && !$this->paused;
184180
}
185181

186182
public function stop($status = 0, $options = []): int
187183
{
188184
// Tell the server you are going to stop consuming.
189185
// It will finish up the last message and not send you anymore.
190-
$this->channel->basic_cancel($this->consumerTag, false, true);
186+
$this->amqpChannel->basic_cancel($this->consumerTag, false, true);
191187

192188
return parent::stop($status);
193189
}

0 commit comments

Comments
 (0)