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
|
8 |
|
public function __construct(Protocol $first, Protocol ...$protocols) |
23
|
|
|
{ |
24
|
|
|
$protocols = Sequence::of($first, ...$protocols)->sort(static function(Protocol $a, Protocol $b): bool { |
25
|
6 |
|
return $b->version()->higherThan($a->version()); |
26
|
8 |
|
}); |
27
|
8 |
|
$this->inUse = $protocols->first(); |
28
|
8 |
|
$this->protocols = $protocols; |
29
|
8 |
|
} |
30
|
|
|
|
31
|
6 |
|
public function version(): Version |
32
|
|
|
{ |
33
|
6 |
|
return $this->inUse->version(); |
34
|
|
|
} |
35
|
|
|
|
36
|
4 |
|
public function use(Version $version): Protocol |
37
|
|
|
{ |
38
|
|
|
$protocols = $this |
39
|
4 |
|
->protocols |
40
|
|
|
->filter(static function(Protocol $protocol) use ($version): bool { |
41
|
|
|
try { |
42
|
4 |
|
$protocol->use($version); |
43
|
|
|
|
44
|
4 |
|
return true; |
45
|
4 |
|
} catch (VersionNotUsable $e) { |
46
|
4 |
|
return false; |
47
|
|
|
} |
48
|
4 |
|
}); |
49
|
|
|
|
50
|
4 |
|
if ($protocols->size() === 0) { |
51
|
|
|
throw new VersionNotUsable($version); |
52
|
|
|
} |
53
|
|
|
|
54
|
4 |
|
$this->inUse = $protocols->first(); |
55
|
|
|
|
56
|
4 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
2 |
|
public function read(Method $method, Readable $arguments): StreamInterface |
63
|
|
|
{ |
64
|
2 |
|
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
|
2 |
|
public function method(string $name): Method |
76
|
|
|
{ |
77
|
2 |
|
return $this->inUse->method($name); |
78
|
|
|
} |
79
|
|
|
|
80
|
2 |
|
public function connection(): Connection |
81
|
|
|
{ |
82
|
2 |
|
return $this->inUse->connection(); |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
public function channel(): Channel |
86
|
|
|
{ |
87
|
2 |
|
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
|
|
|
|