1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Portiny\RabbitMQ\Exchange; |
4
|
|
|
|
5
|
|
|
use Bunny\Channel; |
6
|
|
|
use Bunny\Exception\BunnyException; |
7
|
|
|
use Bunny\Protocol\MethodExchangeBindOkFrame; |
8
|
|
|
use Bunny\Protocol\MethodExchangeDeclareOkFrame; |
9
|
|
|
|
10
|
|
|
abstract class AbstractExchange |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
public const TYPE_DIRECT = 'direct'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
public const TYPE_HEADERS = 'headers'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
public const TYPE_FANOUT = 'fanout'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public const TYPE_TOPIC = 'topic'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
public const AVAILABLE_TYPES = [self::TYPE_DIRECT, self::TYPE_HEADERS, self::TYPE_FANOUT, self::TYPE_TOPIC]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @throws BunnyException |
39
|
|
|
*/ |
40
|
|
|
final public function declare(Channel $channel): void |
41
|
|
|
{ |
42
|
|
|
$frame = $channel->exchangeDeclare( |
43
|
|
|
$this->getName(), |
44
|
|
|
$this->getType(), |
45
|
|
|
$this->isPassive(), |
46
|
|
|
$this->isDurable(), |
47
|
|
|
$this->isAutoDelete(), |
48
|
|
|
$this->isInternal(), |
49
|
|
|
FALSE, |
50
|
|
|
$this->getArguments() |
51
|
|
|
); |
52
|
|
|
if (! $frame instanceof MethodExchangeDeclareOkFrame) { |
|
|
|
|
53
|
|
|
throw new BunnyException(sprintf('Could not declare exchange "%s".', $this->getName())); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @throws BunnyException |
59
|
|
|
*/ |
60
|
|
|
final public function declareBindings(Channel $channel): void |
61
|
|
|
{ |
62
|
|
|
foreach ($this->getBindings() as $exchangeBind) { |
63
|
|
|
$frame = $channel->exchangeBind( |
64
|
|
|
$exchangeBind->getDestination(), |
65
|
|
|
$this->getName(), |
66
|
|
|
$exchangeBind->getRoutingKey(), |
67
|
|
|
FALSE, |
68
|
|
|
$exchangeBind->getArguments() |
69
|
|
|
); |
70
|
|
|
if (! $frame instanceof MethodExchangeBindOkFrame) { |
|
|
|
|
71
|
|
|
throw new BunnyException( |
72
|
|
|
sprintf( |
73
|
|
|
'Could not bind exchange "%s" to "%s" with routing key "%s".', |
74
|
|
|
$exchangeBind->getDestination(), |
75
|
|
|
$this->getName(), |
76
|
|
|
$exchangeBind->getRoutingKey() |
77
|
|
|
) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
abstract protected function getName(): string; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return ExchangeBind[] |
87
|
|
|
*/ |
88
|
|
|
protected function getBindings(): array |
89
|
|
|
{ |
90
|
|
|
return []; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
protected function getType(): string |
94
|
|
|
{ |
95
|
|
|
return self::TYPE_DIRECT; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function isPassive(): bool |
99
|
|
|
{ |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function isDurable(): bool |
104
|
|
|
{ |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
protected function isAutoDelete(): bool |
109
|
|
|
{ |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
protected function isInternal(): bool |
114
|
|
|
{ |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
protected function isNoWait(): bool |
119
|
|
|
{ |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
protected function getArguments(): array |
124
|
|
|
{ |
125
|
|
|
return []; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.