1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Queue\AMQP; |
6
|
|
|
|
7
|
|
|
use PhpAmqpLib\Channel\AMQPChannel; |
8
|
|
|
use PhpAmqpLib\Connection\AbstractConnection; |
9
|
|
|
use Yiisoft\Queue\AMQP\Exception\ExchangeDeclaredException; |
10
|
|
|
use Yiisoft\Queue\AMQP\Settings\Exchange; |
11
|
|
|
use Yiisoft\Queue\AMQP\Settings\ExchangeSettingsInterface; |
12
|
|
|
use Yiisoft\Queue\AMQP\Settings\QueueSettingsInterface; |
13
|
|
|
|
14
|
|
|
final class QueueProvider implements QueueProviderInterface |
15
|
|
|
{ |
16
|
|
|
public const EXCHANGE_NAME_DEFAULT = 'yii-queue'; |
17
|
|
|
|
18
|
|
|
private ?AMQPChannel $channel = null; |
19
|
|
|
|
20
|
|
|
public function __construct( |
21
|
|
|
private AbstractConnection $connection, |
22
|
|
|
private QueueSettingsInterface $queueSettings, |
23
|
|
|
private ?ExchangeSettingsInterface $exchangeSettings = null, |
24
|
|
|
private array $messageProperties = [], |
25
|
|
|
) { |
26
|
|
|
if ($this->exchangeSettings === null) { |
27
|
|
|
$this->exchangeSettings = new Exchange(self::EXCHANGE_NAME_DEFAULT); |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function __destruct() |
32
|
|
|
{ |
33
|
|
|
$this->channel?->close(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getChannel(): AMQPChannel |
37
|
|
|
{ |
38
|
|
|
if ($this->channel === null) { |
39
|
|
|
$this->channel = $this->connection->channel(); |
40
|
|
|
$this->channel->queue_declare(...$this->queueSettings->getPositionalSettings()); |
|
|
|
|
41
|
|
|
|
42
|
|
|
if ($this->exchangeSettings !== null) { |
43
|
|
|
$this->channel->exchange_declare(...$this->exchangeSettings->getPositionalSettings()); |
44
|
|
|
$this->channel->queue_bind($this->queueSettings->getName(), $this->exchangeSettings->getName()); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->channel; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getQueueSettings(): QueueSettingsInterface |
52
|
|
|
{ |
53
|
|
|
return $this->queueSettings; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getExchangeSettings(): ?ExchangeSettingsInterface |
57
|
|
|
{ |
58
|
|
|
return $this->exchangeSettings; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getMessageProperties(): array |
62
|
|
|
{ |
63
|
|
|
return $this->messageProperties; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function withChannelName(string $channel): self |
67
|
|
|
{ |
68
|
|
|
if ($channel === $this->queueSettings->getName()) { |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($this->exchangeSettings !== null) { |
73
|
|
|
throw new ExchangeDeclaredException(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$instance = clone $this; |
77
|
|
|
$instance->channel = null; |
78
|
|
|
$instance->queueSettings = $instance->queueSettings->withName($channel); |
79
|
|
|
|
80
|
|
|
return $instance; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return self |
85
|
|
|
*/ |
86
|
|
|
public function withQueueSettings(QueueSettingsInterface $queueSettings): QueueProviderInterface |
87
|
|
|
{ |
88
|
|
|
$new = clone $this; |
89
|
|
|
$new->queueSettings = $queueSettings; |
90
|
|
|
|
91
|
|
|
return $new; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return self |
96
|
|
|
*/ |
97
|
|
|
public function withExchangeSettings(?ExchangeSettingsInterface $exchangeSettings): QueueProviderInterface |
98
|
|
|
{ |
99
|
|
|
$new = clone $this; |
100
|
|
|
$new->exchangeSettings = $exchangeSettings; |
101
|
|
|
|
102
|
|
|
return $new; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return self |
107
|
|
|
*/ |
108
|
|
|
public function withMessageProperties(array $properties): QueueProviderInterface |
109
|
|
|
{ |
110
|
|
|
$new = clone $this; |
111
|
|
|
$new->messageProperties = $properties; |
112
|
|
|
|
113
|
|
|
return $new; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.