-
Notifications
You must be signed in to change notification settings - Fork 55
/
AmqpContextManager.php
77 lines (64 loc) · 2 KB
/
AmqpContextManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Enqueue\MessengerAdapter;
use Interop\Amqp\AmqpContext;
use Interop\Amqp\AmqpQueue;
use Interop\Amqp\AmqpTopic;
use Interop\Amqp\Impl\AmqpBind;
use Interop\Queue\Context;
class AmqpContextManager implements ContextManager
{
private $context;
public function __construct(Context $context)
{
$this->context = $context;
}
/**
* {@inheritdoc}
*/
public function context(): Context
{
return $this->context;
}
/**
* {@inheritdoc}
*/
public function recoverException(\Exception $exception, array $destination): bool
{
if ($exception instanceof \AMQPQueueException) {
if (404 === $exception->getCode()) {
return $this->ensureExists($destination);
}
}
return false;
}
/**
* {@inheritdoc}
*/
public function ensureExists(array $destination): bool
{
if (!$this->context instanceof AmqpContext) {
return false;
}
$topic = $this->context->createTopic($destination['topic']);
$topic->setType($destination['topicOptions']['type'] ?? AmqpTopic::TYPE_FANOUT);
$topicFlags = $destination['topicOptions']['flags'] ?? ((int) $topic->getFlags() | AmqpTopic::FLAG_DURABLE);
$topic->setFlags($topicFlags);
$this->context->declareTopic($topic);
$queue = $this->context->createQueue($destination['queue']);
$queueFlags = $destination['queueOptions']['flags'] ?? ((int) $queue->getFlags() | AmqpQueue::FLAG_DURABLE);
$queue->setFlags($queueFlags);
$this->context->declareQueue($queue);
$this->context->bind(
new AmqpBind($queue, $topic, $destination['queueOptions']['bindingKey'] ?? null)
);
return true;
}
}