HeadersConsumer::declareQueue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 10
1
<?php
2
3
/**
4
 * This file is part of amqp
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
 
10
declare(strict_types=1);
11
12
namespace Slick\Amqp\Consumer;
13
14
use PhpAmqpLib\Wire\AMQPTable;
15
use Slick\Amqp\Consumer;
16
use Slick\Amqp\Consumer\BasicConsumer;
17
use Slick\Amqp\Producer;
18
19
/**
20
 * HeadersConsumer
21
 *
22
 * @package Slick\Amqp\Consumer
23
 */
24
abstract class HeadersConsumer extends BasicConsumer implements Consumer
25
{
26
    const X_MATCH_ALL = 'all';
27
    const X_MATCH_ANY = 'any';
28
29
    /**
30
     * @inheritDoc
31
     */
32
    protected function declareQueue(): void
33
    {
34
        $this->mergeExchangeOptions();
35
        $args = array_values($this->exchangeOptions());
36
        array_unshift($args, Producer::TYPE_HEADERS);
37
        array_unshift($args, $this->exchange);
38
        call_user_func_array([$this->channel(), 'exchange_declare'], $args);
39
        parent::declareQueue();
40
    }
41
42
    /**
43
     * Binds the queue to the exchange based on headers.
44
     *
45
     * This method checks if the queue is already declared and declares it if not.
46
     *
47
     * @param array<string, mixed> $headers An array of header key-value pairs to bind the queue with
48
     * @return mixed The result of the queue bind operation
49
     */
50
    public function bindHeaders(array $headers, string $xMatch = self::X_MATCH_ALL): mixed
51
    {
52
        if (!$this->isDeclared()) {
53
            $this->declareQueue();
54
        }
55
56
        $headerParams = new AMQPTable(array_merge(['x-match' => $xMatch], $headers));
57
        return $this->channel()->queue_bind($this->queue, $this->exchange, '', false, $headerParams);
58
    }
59
}
60