1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\AMQP\Transport\Connection; |
5
|
|
|
|
6
|
|
|
use Innmind\AMQP\{ |
7
|
|
|
Model\Connection\MaxFrameSize, |
8
|
|
|
Transport\Connection as ConnectionInterface, |
9
|
|
|
Transport\Protocol, |
10
|
|
|
Transport\Frame, |
11
|
|
|
Exception\ConnectionClosed |
12
|
|
|
}; |
13
|
|
|
use Innmind\Socket\Internet\Transport; |
14
|
|
|
use Innmind\Url\UrlInterface; |
15
|
|
|
use Innmind\TimeContinuum\{ |
16
|
|
|
TimeContinuumInterface, |
17
|
|
|
ElapsedPeriod |
18
|
|
|
}; |
19
|
|
|
|
20
|
|
|
final class Lazy implements ConnectionInterface |
21
|
|
|
{ |
22
|
|
|
private $transport; |
23
|
|
|
private $server; |
24
|
|
|
private $protocol; |
25
|
|
|
private $timeout; |
26
|
|
|
private $clock; |
27
|
|
|
private $connection; |
28
|
|
|
private $closed = false; |
29
|
|
|
|
30
|
16 |
|
public function __construct( |
31
|
|
|
Transport $transport, |
32
|
|
|
UrlInterface $server, |
33
|
|
|
Protocol $protocol, |
34
|
|
|
ElapsedPeriod $timeout, |
35
|
|
|
TimeContinuumInterface $clock |
36
|
|
|
) { |
37
|
16 |
|
$this->transport = $transport; |
38
|
16 |
|
$this->server = $server; |
39
|
16 |
|
$this->protocol = $protocol; |
40
|
16 |
|
$this->timeout = $timeout; |
41
|
16 |
|
$this->clock = $clock; |
42
|
16 |
|
} |
43
|
|
|
|
44
|
2 |
|
public function protocol(): Protocol |
45
|
|
|
{ |
46
|
2 |
|
return $this->connection()->protocol(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
4 |
|
public function send(Frame $frame): ConnectionInterface |
53
|
|
|
{ |
54
|
4 |
|
return $this->connection()->send($frame); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
2 |
|
public function wait(string ...$names): Frame |
61
|
|
|
{ |
62
|
2 |
|
return $this->connection()->wait(...$names); |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public function maxFrameSize(): MaxFrameSize |
66
|
|
|
{ |
67
|
2 |
|
return $this->connection()->maxFrameSize(); |
68
|
|
|
} |
69
|
4 |
|
public function close(): void |
70
|
|
|
{ |
71
|
4 |
|
if ($this->closed) { |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
4 |
|
if ($this->initialized()) { |
76
|
|
|
$this->connection()->close(); |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
$this->closed = true; |
80
|
4 |
|
} |
81
|
|
|
|
82
|
4 |
|
public function closed(): bool |
83
|
|
|
{ |
84
|
4 |
|
return $this->closed || $this->connection()->closed(); |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
private function initialized(): bool |
88
|
|
|
{ |
89
|
4 |
|
return $this->connection instanceof Connection; |
90
|
|
|
} |
91
|
|
|
|
92
|
12 |
|
private function connection(): Connection |
93
|
|
|
{ |
94
|
12 |
|
if ($this->closed) { |
95
|
2 |
|
throw new ConnectionClosed; |
96
|
|
|
} |
97
|
|
|
|
98
|
10 |
|
return $this->connection ?? $this->connection = new Connection( |
99
|
10 |
|
$this->transport, |
100
|
10 |
|
$this->server, |
101
|
10 |
|
$this->protocol, |
102
|
10 |
|
$this->timeout, |
103
|
10 |
|
$this->clock |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|