Skip to content

Commit bf18827

Browse files
committed
some extra cleanup
1 parent 9a900f7 commit bf18827

File tree

3 files changed

+114
-52
lines changed

3 files changed

+114
-52
lines changed

src/Queue/Connection/ConfigFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ public static function make(array $config = []): AMQPConnectionConfig
2020
// Set the connection to a Lazy by default
2121
$connectionConfig->setIsLazy(! in_array(
2222
Arr::get($config, 'lazy') ?? true,
23-
[false, 0, '0', 'false'],
23+
[false, 0, '0', 'false', 'no'],
2424
true)
2525
);
2626

2727
// Set the connection to unsecure by default
2828
$connectionConfig->setIsSecure(in_array(
2929
Arr::get($config, 'secure'),
30-
[true, 1, '1', 'true'],
30+
[true, 1, '1', 'true', 'yes'],
3131
true)
3232
);
3333

@@ -86,8 +86,8 @@ protected static function getHeartbeatFromConfig(AMQPConnectionConfig $connectio
8686
{
8787
$heartbeat = Arr::get($config, self::CONFIG_OPTIONS.'.heartbeat');
8888

89-
if (! empty($heartbeat) && is_numeric($heartbeat) && 0 < (int) $heartbeat) {
90-
$connectionConfig->setHeartbeat($heartbeat);
89+
if (is_numeric($heartbeat) && intval($heartbeat) > 0) {
90+
$connectionConfig->setHeartbeat((int) $heartbeat);
9191
}
9292
}
9393
}

src/Queue/QueueConfig.php

Lines changed: 103 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,72 +16,83 @@ class QueueConfig
1616

1717
protected int $queueMaxPriority = 2;
1818

19-
protected ?string $exchange = null;
19+
protected string $exchange = '';
2020

2121
protected string $exchangeType = 'direct';
2222

2323
protected string $exchangeRoutingKey = '%s';
2424

2525
protected bool $rerouteFailed = false;
2626

27-
protected ?string $failedExchange = null;
27+
protected string $failedExchange = '';
2828

2929
protected string $failedRoutingKey = '%s.failed';
3030

3131
protected bool $quorum = false;
3232

3333
protected array $options = [];
3434

35+
/**
36+
* Holds the default queue name
37+
*
38+
* When no queue name is provided by laravel queue / workers via the QueueApi method's,
39+
* this value is used to publish messages.
40+
*/
3541
public function getQueue(): string
3642
{
3743
return $this->queue;
3844
}
3945

40-
public function setQueue(?string $queue): QueueConfig
46+
public function setQueue(string $queue): QueueConfig
4147
{
42-
$this->queue = $queue ?: 'default';
48+
$this->queue = $queue;
4349

4450
return $this;
4551
}
4652

53+
/**
54+
* Returns &true; as indication that jobs should be dispatched after all database transactions
55+
* have been committed.
56+
*/
4757
public function isDispatchAfterCommit(): bool
4858
{
4959
return $this->dispatchAfterCommit;
5060
}
5161

5262
public function setDispatchAfterCommit($dispatchAfterCommit): QueueConfig
5363
{
54-
$this->dispatchAfterCommit = ! empty($dispatchAfterCommit);
55-
56-
return $this;
57-
}
58-
59-
public function getOptions(): array
60-
{
61-
return $this->options;
62-
}
63-
64-
public function setOptions(?array $options): QueueConfig
65-
{
66-
$this->options = $options ?: [];
64+
$this->dispatchAfterCommit = $this->toBoolean($dispatchAfterCommit);
6765

6866
return $this;
6967
}
7068

69+
/**
70+
* Get the Job::class to use when processing messages
71+
*/
7172
public function getAbstractJob(): string
7273
{
7374
return $this->abstractJob;
7475
}
7576

76-
public function setAbstractJob(?string $abstract): QueueConfig
77+
public function setAbstractJob(string $abstract): QueueConfig
7778
{
78-
$this->abstractJob = $abstract ?: RabbitMQJob::class;
79+
$this->abstractJob = $abstract;
7980

8081
return $this;
8182
}
8283

8384
/**
8485
* Returns &true;, if delayed messages should be prioritized.
86+
*
87+
* RabbitMQ queues work with the FIFO method. So when there are 10000 messages in the queue and
88+
* the delayed message is put back to the queue (at the end) for further processing the delayed message won´t
89+
* process before all 10000 messages are processed. The same is true for requeueing.
90+
*
91+
* This may not what you desire.
92+
* When you want the message to get processed immediately after the delayed time expires or when requeueing, we can
93+
* use prioritization.
94+
*
95+
* @see[https://www.rabbitmq.com/queues.html#basics]
8596
*/
8697
public function isPrioritizeDelayed(): bool
8798
{
@@ -90,7 +101,7 @@ public function isPrioritizeDelayed(): bool
90101

91102
public function setPrioritizeDelayed($prioritizeDelayed): QueueConfig
92103
{
93-
$this->prioritizeDelayed = ! empty($prioritizeDelayed);
104+
$this->prioritizeDelayed = $this->toBoolean($prioritizeDelayed);
94105

95106
return $this;
96107
}
@@ -109,51 +120,67 @@ public function getQueueMaxPriority(): int
109120

110121
public function setQueueMaxPriority($queueMaxPriority): QueueConfig
111122
{
112-
if (is_numeric($queueMaxPriority)) {
123+
if (is_numeric($queueMaxPriority) && intval($queueMaxPriority) > 1) {
113124
$this->queueMaxPriority = (int) $queueMaxPriority;
114125
}
115126

116127
return $this;
117128
}
118129

119130
/**
120-
* Get the exchange name, or &null; as default value.
131+
* Get the exchange name, or empty string; as default value.
132+
*
133+
* The default exchange is an unnamed pre-declared direct exchange. Usually, an empty string
134+
* is frequently used to indicate it. If you choose default exchange, your message will be delivered
135+
* to a queue with the same name as the routing key.
136+
* With a routing key that is the same as the queue name, every queue is immediately tied to the default exchange.
121137
*/
122-
public function getExchange(): ?string
138+
public function getExchange(): string
123139
{
124140
return $this->exchange;
125141
}
126142

127-
public function setExchange(?string $exchange): QueueConfig
143+
public function setExchange(string $exchange): QueueConfig
128144
{
129-
$this->exchange = $exchange ?: null;
145+
$this->exchange = $exchange;
130146

131147
return $this;
132148
}
133149

150+
/**
151+
* Get the exchange type
152+
*
153+
* There are four basic RabbitMQ exchange types in RabbitMQ, each of which uses different parameters
154+
* and bindings to route messages in various ways, These are: 'direct', 'topic', 'fanout', 'headers'
155+
*
156+
* The default type is set as 'direct'
157+
*/
134158
public function getExchangeType(): string
135159
{
136160
return $this->exchangeType;
137161
}
138162

139-
public function setExchangeType(?string $exchangeType): QueueConfig
163+
public function setExchangeType(string $exchangeType): QueueConfig
140164
{
141-
$this->exchangeType = $exchangeType ?: 'direct';
165+
$this->exchangeType = $exchangeType;
142166

143167
return $this;
144168
}
145169

146170
/**
147-
* @return string
171+
* Get the routing key when using an exchange other than the direct exchange.
172+
* The routing key is a message attribute taken into account by the exchange when deciding how to route a message.
173+
*
174+
* The default routing-key is the given destination: '%s'.
148175
*/
149-
public function getExchangeRoutingKey(): ?string
176+
public function getExchangeRoutingKey(): string
150177
{
151178
return $this->exchangeRoutingKey;
152179
}
153180

154-
public function setExchangeRoutingKey(?string $exchangeRoutingKey): QueueConfig
181+
public function setExchangeRoutingKey(string $exchangeRoutingKey): QueueConfig
155182
{
156-
$this->exchangeRoutingKey = $exchangeRoutingKey ?: '%s';
183+
$this->exchangeRoutingKey = $exchangeRoutingKey;
157184

158185
return $this;
159186
}
@@ -168,48 +195,84 @@ public function isRerouteFailed(): bool
168195

169196
public function setRerouteFailed($rerouteFailed): QueueConfig
170197
{
171-
$this->rerouteFailed = ! empty($rerouteFailed);
198+
$this->rerouteFailed = $this->toBoolean($rerouteFailed);
172199

173200
return $this;
174201
}
175202

176-
public function getFailedExchange(): ?string
203+
/**
204+
* Get the exchange name with messages are published against.
205+
* The default exchange is empty, so messages will be published directly to a queue.
206+
*/
207+
public function getFailedExchange(): string
177208
{
178209
return $this->failedExchange;
179210
}
180211

181-
public function setFailedExchange(?string $failedExchange): QueueConfig
212+
public function setFailedExchange(string $failedExchange): QueueConfig
182213
{
183-
$this->failedExchange = $failedExchange ?: null;
214+
$this->failedExchange = $failedExchange;
184215

185216
return $this;
186217
}
187218

188219
/**
189-
* Get the routing-key for failed messages
190-
* The default routing-key is the given destination substituted by '.failed'.
220+
* Get the substitution string for failed messages
221+
* The default routing-key is the given destination substituted by '%s.failed'.
191222
*/
192223
public function getFailedRoutingKey(): string
193224
{
194225
return $this->failedRoutingKey;
195226
}
196227

197-
public function setFailedRoutingKey(?string $failedRoutingKey): QueueConfig
228+
public function setFailedRoutingKey(string $failedRoutingKey): QueueConfig
198229
{
199-
$this->failedRoutingKey = $failedRoutingKey ?: '%s.failed';
230+
$this->failedRoutingKey = $failedRoutingKey;
200231

201232
return $this;
202233
}
203234

235+
/**
236+
* Returns &true;, if queue is marked or set as quorum queue.
237+
*/
204238
public function isQuorum(): bool
205239
{
206240
return $this->quorum;
207241
}
208242

209243
public function setQuorum($quorum): QueueConfig
210244
{
211-
$this->quorum = ! empty($quorum);
245+
$this->quorum = $this->toBoolean($quorum);
246+
247+
return $this;
248+
}
249+
250+
/**
251+
* Holds all unknown queue options provided in the connection config
252+
*/
253+
public function getOptions(): array
254+
{
255+
return $this->options;
256+
}
257+
258+
public function setOptions(array $options): QueueConfig
259+
{
260+
$this->options = $options;
212261

213262
return $this;
214263
}
264+
265+
/**
266+
* Filters $value to boolean value
267+
*
268+
* Returns: &true;
269+
* For values: 1, '1', true, 'true', 'yes'
270+
*
271+
* Returns: &false;
272+
* For values: 0, '0', false, 'false', '', null, [] , 'ok', 'no', 'no not a bool', 'yes a bool'
273+
*/
274+
protected function toBoolean($value): bool
275+
{
276+
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
277+
}
215278
}

src/Queue/RabbitMQQueue.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ protected function getQueueArguments(string $destination): array
594594
}
595595

596596
if ($this->getConfig()->isRerouteFailed()) {
597-
$arguments['x-dead-letter-exchange'] = $this->getFailedExchange() ?? '';
597+
$arguments['x-dead-letter-exchange'] = $this->getFailedExchange();
598598
$arguments['x-dead-letter-routing-key'] = $this->getFailedRoutingKey($destination);
599599
}
600600

@@ -611,19 +611,19 @@ protected function getQueueArguments(string $destination): array
611611
protected function getDelayQueueArguments(string $destination, int $ttl): array
612612
{
613613
return [
614-
'x-dead-letter-exchange' => $this->getExchange() ?? '',
614+
'x-dead-letter-exchange' => $this->getExchange(),
615615
'x-dead-letter-routing-key' => $this->getRoutingKey($destination),
616616
'x-message-ttl' => $ttl,
617617
'x-expires' => $ttl * 2,
618618
];
619619
}
620620

621621
/**
622-
* Get the exchange name, or &null; as default value.
622+
* Get the exchange name, or empty string; as default value.
623623
*/
624-
protected function getExchange(string $exchange = null): ?string
624+
protected function getExchange(?string $exchange = null): string
625625
{
626-
return $exchange ?: $this->getConfig()->getExchange();
626+
return $exchange ?? $this->getConfig()->getExchange();
627627
}
628628

629629
/**
@@ -648,9 +648,9 @@ protected function getExchangeType(?string $type = null): string
648648
/**
649649
* Get the exchange for failed messages.
650650
*/
651-
protected function getFailedExchange(string $exchange = null): ?string
651+
protected function getFailedExchange(?string $exchange = null): string
652652
{
653-
return $exchange ?: $this->getConfig()->getFailedExchange();
653+
return $exchange ?? $this->getConfig()->getFailedExchange();
654654
}
655655

656656
/**
@@ -681,7 +681,6 @@ protected function isQueueDeclared(string $name): bool
681681
/**
682682
* Declare the destination when necessary.
683683
*
684-
*
685684
* @throws AMQPProtocolChannelException
686685
*/
687686
protected function declareDestination(string $destination, ?string $exchange = null, string $exchangeType = AMQPExchangeType::DIRECT): void

0 commit comments

Comments
 (0)