|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Transport\Protocol; |
|
5
|
|
|
|
|
6
|
|
|
use Innmind\AMQP\{ |
|
7
|
|
|
Transport\Protocol, |
|
8
|
|
|
Transport\Frame\Method, |
|
9
|
|
|
Exception\VersionNotUsable, |
|
10
|
|
|
}; |
|
11
|
|
|
use Innmind\Stream\Readable; |
|
12
|
|
|
use Innmind\Immutable\{ |
|
13
|
|
|
Sequence, |
|
14
|
|
|
StreamInterface, |
|
15
|
|
|
}; |
|
16
|
|
|
|
|
17
|
|
|
final class Delegate implements Protocol |
|
18
|
|
|
{ |
|
19
|
|
|
private $protocols; |
|
20
|
|
|
private $inUse; |
|
21
|
|
|
|
|
22
|
5 |
|
public function __construct(Protocol $first, Protocol ...$protocols) |
|
23
|
|
|
{ |
|
24
|
|
|
$protocols = Sequence::of($first, ...$protocols)->sort(static function(Protocol $a, Protocol $b): bool { |
|
25
|
3 |
|
return $b->version()->higherThan($a->version()); |
|
26
|
5 |
|
}); |
|
27
|
5 |
|
$this->inUse = $protocols->first(); |
|
28
|
5 |
|
$this->protocols = $protocols; |
|
29
|
5 |
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
public function version(): Version |
|
32
|
|
|
{ |
|
33
|
3 |
|
return $this->inUse->version(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
public function use(Version $version): Protocol |
|
37
|
|
|
{ |
|
38
|
|
|
$protocols = $this |
|
39
|
2 |
|
->protocols |
|
40
|
|
|
->filter(static function(Protocol $protocol) use ($version): bool { |
|
41
|
|
|
try { |
|
42
|
2 |
|
$protocol->use($version); |
|
43
|
|
|
|
|
44
|
2 |
|
return true; |
|
45
|
2 |
|
} catch (VersionNotUsable $e) { |
|
46
|
2 |
|
return false; |
|
47
|
|
|
} |
|
48
|
2 |
|
}); |
|
49
|
|
|
|
|
50
|
2 |
|
if ($protocols->size() === 0) { |
|
51
|
|
|
throw new VersionNotUsable($version); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
$this->inUse = $protocols->first(); |
|
55
|
|
|
|
|
56
|
2 |
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* {@inheritdoc} |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function read(Method $method, Readable $arguments): StreamInterface |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->inUse->read($method, $arguments); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
|
|
public function readHeader(Readable $arguments): StreamInterface |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->inUse->readHeader($arguments); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function method(string $name): Method |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->inUse->method($name); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public function connection(): Connection |
|
81
|
|
|
{ |
|
82
|
1 |
|
return $this->inUse->connection(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public function channel(): Channel |
|
86
|
|
|
{ |
|
87
|
1 |
|
return $this->inUse->channel(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function exchange(): Exchange |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->inUse->exchange(); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function queue(): Queue |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->inUse->queue(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function basic(): Basic |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->inUse->basic(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function transaction(): Transaction |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->inUse->transaction(); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|