1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jellyfish\QueueRabbitMq; |
4
|
|
|
|
5
|
|
|
use Jellyfish\Config\ConfigInterface; |
6
|
|
|
use Jellyfish\Queue\MessageMapperInterface; |
7
|
|
|
use Jellyfish\Queue\QueueClientInterface; |
8
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
9
|
|
|
use PhpAmqpLib\Connection\AMQPLazyConnection; |
10
|
|
|
use Pimple\Container; |
11
|
|
|
use Pimple\ServiceProviderInterface; |
12
|
|
|
|
13
|
|
|
class QueueRabbitMqServiceProvider implements ServiceProviderInterface |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @param \Pimple\Container $pimple |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function register(Container $pimple): void |
21
|
|
|
{ |
22
|
|
|
$this->registerQueueClient($pimple); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function registerQueueClient(Container $container): QueueRabbitMqServiceProvider |
26
|
|
|
{ |
27
|
|
|
$self = $this; |
28
|
|
|
$container->offsetSet('queue_client', function (Container $container) use ($self) { |
29
|
|
|
return new QueueClient( |
30
|
|
|
$self->createConnection($container), |
31
|
|
|
$container->offsetGet('message_mapper') |
32
|
|
|
); |
33
|
|
|
}); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param \Pimple\Container $container |
38
|
|
|
* |
39
|
|
|
* @return \PhpAmqpLib\Connection\AbstractConnection |
40
|
|
|
*/ |
41
|
|
|
protected function createConnection(Container $container): AbstractConnection |
42
|
|
|
{ |
43
|
|
|
$config = $container->offsetGet('config'); |
44
|
|
|
|
45
|
|
|
$rabbitMqHost = $config->get( |
46
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_HOST, |
47
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_HOST |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
$rabbitMqPort = $config->get( |
51
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_PORT, |
52
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_PORT |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$rabbitMqUser = $config->get( |
56
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_USER, |
57
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_USER |
58
|
|
|
); |
59
|
|
|
|
60
|
|
|
$rabbitMqPassword = $config->get( |
61
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_PASSWORD, |
62
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_PASSWORD |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
$rabbitMqVhost = $config->get( |
66
|
|
|
QueueRabbitMqConstants::RABBIT_MQ_VHOST, |
67
|
|
|
QueueRabbitMqConstants::DEFAULT_RABBIT_MQ_VHOST |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$connection = new AMQPLazyConnection( |
71
|
|
|
$rabbitMqHost, |
72
|
|
|
$rabbitMqPort, |
73
|
|
|
$rabbitMqUser, |
74
|
|
|
$rabbitMqPassword, |
75
|
|
|
$rabbitMqVhost |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
return $connection; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example: