Completed
Push — develop ( 7ec6a7...e72b38 )
by Nicolas
11:34
created

Client::init()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
rs 8.5806
cc 4
eloc 16
nc 2
nop 0
1
<?php
2
3
namespace devtransition\rabbitmq;
4
5
use yii\base\Component;
6
use yii\base\InvalidParamException;
7
use yii\di\Container;
8
use devtransition\rabbitmq\base\Connection;
9
use devtransition\rabbitmq\base\Channel;
10
11
/**
12
 * Connection represents an advanced instance of PhpAmqpLibs AMQPStreamConnection.
13
 *
14
 * Connection works together with Channels .... explain..
15
 *
16
 * ```php
17
 * 'components' => [
18
 *     'rabbit' => [
19
 *         'class' => '\devtransition\rabbitmq\Client',
20
 *         'username' => 'guest',
21
 *         'password' => 'guest',
22
 *         'vhost' => '/',
23
 *     ],
24
 * ],
25
 * ```
26
 *
27
 * @property Connection $connection The PhpAmqpLib connection instance. This property is read-only.
28
 *
29
 * @author Nicolas Wild <[email protected]>
30
 */
31
class Client extends Component
32
{
33
    /**
34
     * @var string RabbitMQ server hostname or IP address. Defaults to `localhost`.
35
     */
36
    public $host = 'localhost';
37
    /**
38
     * @var integer TCP-Port where RabbitMQ is listening. Defaults to `5672`.
39
     */
40
    public $port = 5672;
41
    /**
42
     * @var string RabbitMQ server username on connect. Defaults to `guest`.
43
     */
44
    public $username = 'guest';
45
    /**
46
     * @var string RabbitMQ server password on connect. Defaults to `guest`.
47
     */
48
    public $password = '';
49
    /**
50
     * @var string RabbitMQ server virtual host to use. Defaults to `/`.
51
     */
52
    public $vhost = '/';
53
    /**
54
     * @var array
55
     */
56
    public $options = [];
57
    /**
58
     * @var Connection|array|string The Connection object or the application component ID of the Connection object.
59
     * It´s also possible to set an array with class and addition configuration - in that case a new objects gets generated an configured.
60
     */
61
    public $connectionClass;
62
    /**
63
     * @var array|string The Channel class name or an array with class and addition configuration - in that case a new objects gets generated an configured.
64
     */
65
    public $channelClass;
66
67
    /**
68
     * @var Connection
69
     */
70
    protected $connection;
71
72
    /**
73
     * @var Container DI container holding connection (singleton) and all generated channels
74
     */
75
    protected $container;
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function init()
81
    {
82
        parent::init();
83
84
        $this->container = new Container();
85
86
        // Add connection
87
        $config = [
88
            'host' => $this->host,
89
            'port' => $this->port,
90
            'username' => $this->username,
91
            'password' => $this->password,
92
            'vhost' => $this->vhost,
93
        ];
94
95
        if (is_array($this->connectionClass)) {
96
            $connection = array_merge($config, $this->connectionClass);
97
        } else {
98
            $connection = array_merge($config, [
99
                'class' => (!empty($this->connectionClass) ? $this->connectionClass : 'devtransition\rabbitmq\base\Connection'),
100
            ]);
101
        }
102
103
        $this->container->setSingleton('devtransition\rabbitmq\base\ConnectionInterface', $connection);
104
105
        // Define base channel defintion
106
        $this->container->set("channel", ($this->channelClass ? $this->channelClass : 'devtransition\rabbitmq\base\Channel'));
107
    }
108
109
    /**
110
     * @return Connection
111
     */
112
    public function getConnection()
113
    {
114
        return $this->container->get('devtransition\rabbitmq\base\ConnectionInterface');
115
    }
116
117
    /**
118
     * Returns channel object.
119
     *
120
     * All further actions like sending and receiving can be done in the context of that channel.
121
     *
122
     * @param string $channel_id user defined name to identify channel
123
     * @return Channel
124
     */
125
    public function getChannel($channel_id = 'default')
126
    {
127
        $index = "channel_" . $channel_id;
128
129
        if (!$this->container->has($index)) {
130
            $this->container->set($index, 'channel');
131
        }
132
133
        return $this->container->get($index);
134
    }
135
136
    /**
137
     * Close an existing channel
138
     *
139
     * @param string $channel_id user given name during getChanncel()
140
     */
141
    public function closeChannel($channel_id = 'default')
142
    {
143
        $index = "channel_" . $channel_id;
144
        if (!$this->container->has($index)) {
145
            throw new InvalidParamException('invalid channel id');
146
        }
147
148
        $this->container->clear($index);
149
    }
150
151
152
153
}
154