Issues (19)

src/RabbitMQManager.php (4 issues)

Labels
Severity
1
<?php
2
3
namespace Kunnu\RabbitMQ;
4
5
use Illuminate\Config\Repository;
6
use Illuminate\Support\Collection;
7
use PhpAmqpLib\Channel\AMQPChannel;
8
use PhpAmqpLib\Connection\AMQPSSLConnection;
9
use Illuminate\Contracts\Container\Container;
10
use PhpAmqpLib\Connection\AbstractConnection;
11
12
class RabbitMQManager
13
{
14
    /**
15
     * Configuration key.
16
     *
17
     * @var string
18
     */
19
    const CONFIG_KEY = 'rabbitmq';
20
21
    /**
22
     * IoC Container/Application.
23
     *
24
     * @var Container
25
     */
26
    protected Container $app;
27
28
    /**
29
     * Configuration repository.
30
     *
31
     * @var Repository
32
     */
33
    protected Repository $config;
34
35
    /**
36
     * Connection pool.
37
     *
38
     * @var Collection
39
     */
40
    protected Collection $connections;
41
42
    /**
43
     * Channel pool.
44
     *
45
     * @var Collection
46
     */
47
    protected Collection $channels;
48
49
    /**
50
     * Create a new RabbitMQManager instance.
51
     *
52
     * @param  Container  $app
53
     */
54
    public function __construct(Container $app)
55
    {
56
        $this->app = $app;
57
        $this->config = $this->app->get('config');
58
        $this->connections = new Collection([]);
0 ignored issues
show
array() of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        $this->connections = new Collection(/** @scrutinizer ignore-type */ []);
Loading history...
59
        $this->channels = new Collection([]);
60
    }
61
62
    /**
63
     * Get Connections.
64
     *
65
     * @return Collection
66
     */
67
    public function getConnections(): Collection
68
    {
69
        return $this->connections;
70
    }
71
72
    /**
73
     * Get Config.
74
     *
75
     * @return Repository
76
     */
77
    public function getConfig(): Repository
78
    {
79
        return $this->config;
80
    }
81
82
    /**
83
     * Get Application Container.
84
     *
85
     * @return Container
86
     */
87
    public function getApp(): Container
88
    {
89
        return $this->app;
90
    }
91
92
    /**
93
     * Get Channels.
94
     *
95
     * @return Collection
96
     */
97
    public function getChannels(): Collection
98
    {
99
        return $this->channels;
100
    }
101
102
    /**
103
     * Resolve default connection name.
104
     *
105
     * @return string
106
     */
107
    public function resolveDefaultConfigName(): string
108
    {
109
        $configKey = self::CONFIG_KEY;
110
111
        return $this->config->get("{$configKey}.defaultConnection", 'rabbitmq');
112
    }
113
114
    /**
115
     * Resolve connection instance by name.
116
     *
117
     * @param  string|null  $name
118
     * @param  ConnectionConfig|null  $config
119
     * @return AbstractConnection
120
     */
121
    public function resolveConnection(?string $name = null, ?ConnectionConfig $config = null): AbstractConnection
122
    {
123
        $name = $name ?? $this->resolveDefaultConfigName();
124
125
        if (!$this->connections->has($name)) {
126
            $this->connections->put(
127
                $name,
0 ignored issues
show
$name of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

127
                /** @scrutinizer ignore-type */ $name,
Loading history...
128
                $this->makeConnection($config ?? $this->resolveConfig($name))
129
            );
130
        }
131
132
        return $this->connections->get($name);
133
    }
134
135
    /**
136
     * Resolve connection configuration.
137
     *
138
     * @return ConnectionConfig
139
     */
140
    public function resolveConfig(string $connectionName): ConnectionConfig
141
    {
142
        $configKey = self::CONFIG_KEY;
143
        $connectionKey = "{$configKey}.connections.{$connectionName}";
144
145
        return new ConnectionConfig($this->config->get($connectionKey, []));
146
    }
147
148
    /**
149
     * Get the publisher.
150
     *
151
     * @return RabbitMQPublisher
152
     */
153
    public function publisher(): RabbitMQPublisher
154
    {
155
        return new RabbitMQPublisher($this);
156
    }
157
158
    /**
159
     * Get the consumer.
160
     *
161
     * @return RabbitMQConsumer
162
     */
163
    public function consumer(): RabbitMQConsumer
164
    {
165
        return new RabbitMQConsumer($this);
166
    }
167
168
    /**
169
     * Resolve the channel ID.
170
     *
171
     * @param  int|null  $channelId
172
     * @return int|null
173
     */
174
    public function resolveChannelId(?int $channelId, ?string $connectionName = null): ?int
175
    {
176
        $configKey = self::CONFIG_KEY;
177
        $connectionName = $connectionName ?? $this->resolveDefaultConfigName();
178
        // Use channel ID from the connection config if channel ID is not given
179
        $channelId = $channelId ?? $this->config->get("{$configKey}.{$connectionName}.channel_id");
180
        // else, use the default channel ID
181
        return $channelId ?? $this->config->get("{$configKey}.defaults.channel_id", $channelId);
182
    }
183
184
    /**
185
     * Resolve channel for the given connection.
186
     *
187
     * @param  string|null  $connectionName
188
     * @param  int|null  $channelId
189
     * @param  AbstractConnection|null  $connection
190
     * @return AMQPChannel|null
191
     */
192
    public function resolveChannel(
193
        ?string $connectionName = null,
194
        ?int $channelId = null,
195
        ?AbstractConnection $connection = null
196
    ): AMQPChannel {
197
        if (!$connection) {
198
            $connection = $this->resolveConnection($connectionName);
199
        }
200
201
        $channelId = $channelId ?? $this->resolveChannelId($channelId);
202
203
        if (!$this->channels->has("{$connectionName}.{$channelId}")) {
204
            $this->channels->put("{$connectionName}.{$channelId}", $connection->channel($channelId));
0 ignored issues
show
$connectionName.'.'.$channelId of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

204
            $this->channels->put(/** @scrutinizer ignore-type */ "{$connectionName}.{$channelId}", $connection->channel($channelId));
Loading history...
205
        }
206
207
        return $this->channels->get("{$connectionName}.{$channelId}");
208
    }
209
210
    /**
211
     * Create a new connection.
212
     *
213
     * @param  ConnectionConfig  $config
214
     * @return AbstractConnection
215
     */
216
    protected function makeConnection(ConnectionConfig $config): AbstractConnection
217
    {
218
        return new AMQPSSLConnection(
219
            $config->getHost(),
220
            $config->getPort(),
221
            $config->getUser(),
222
            $config->getPassword(),
223
            $config->getVhost(),
224
            $config->getSSLOptions(),
225
            $config->getOptions(),
226
            $config->getSSLProtocol(),
0 ignored issues
show
It seems like $config->getSSLProtocol() can also be of type string; however, parameter $config of PhpAmqpLib\Connection\AM...nnection::__construct() does only seem to accept PhpAmqpLib\Connection\AMQPConnectionConfig|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

226
            /** @scrutinizer ignore-type */ $config->getSSLProtocol(),
Loading history...
227
        );
228
    }
229
}
230