Completed
Push — master ( 973541...7dbf12 )
by Dmitry
11:58
created

AmqpBindingsProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 0
dl 0
loc 24
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A bind() 0 6 2
1
<?php
2
declare(strict_types=1);
3
4
namespace hiapi\Provider;
5
6
use PhpAmqpLib\Channel\AMQPChannel;
7
8
/**
9
 * Class AmqpBindingsProvider
10
 * Usage:
11
 *
12
 * 1. Add your binding functions to {@see bindings}
13
 * 2. Call {@see bind()} when AMQP is initialized to create required queues and bindings for them.
14
 *
15
 * Example:
16
 * ```
17
 *  $connection = new \PhpAmqpLib\Connection\AMQPLazyConnection(
18
 *      $params['amqp.host'],
19
 *      $params['amqp.port'],
20
 *      $params['amqp.user'],
21
 *      $params['amqp.password']
22
 *  );
23
 *
24
 *  $bindings = $container->get(\hiapi\Provider\AmqpBindingsProvider::class);
25
 *  $bindings->bind($connection->channel());
26
 * ```
27
 *
28
 * @author Dmytro Naumenko <[email protected]>
29
 */
30
final class AmqpBindingsProvider
31
{
32
    /**
33
     * @var \Closure[] array of closures, that bind routing keys to a right queue.
34
     * Expected closure signature is:
35
     *
36
     * ```php
37
     * function (PhpAmqpLib\Channel\AMQPChannel $channel): void {
38
     *    // Bind here, e.g.
39
     *    $channel->queue_declare('recon.queue', false, true, false, false);
40
     *    $channel->exchange_declare('dbms.updates', 'topic', false, true, true, false, false);
41
     *    $channel->queue_bind('recon.queue', 'dbms.updates', 'bot.dns.*');
42
     * }
43
     * ```
44
     */
45
    public $bindings = [];
46
47
    public function bind(AMQPChannel $channel): void
48
    {
49
        foreach ($this->bindings as $bindTo) {
50
            $bindTo($channel);
51
        }
52
    }
53
}
54