1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dazzle\Channel\Extra; |
4
|
|
|
|
5
|
|
|
use Dazzle\Promise\Promise; |
6
|
|
|
use Dazzle\Promise\PromiseInterface; |
7
|
|
|
use Dazzle\Channel\Protocol\ProtocolInterface; |
8
|
|
|
use Dazzle\Channel\Channel; |
9
|
|
|
use Dazzle\Channel\ChannelInterface; |
10
|
|
|
use Error; |
11
|
|
|
use Exception; |
12
|
|
|
|
13
|
|
|
class Response |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ChannelInterface |
17
|
|
|
*/ |
18
|
|
|
protected $channel; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ProtocolInterface |
22
|
|
|
*/ |
23
|
|
|
protected $protocol; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string|string[]|Error|Exception |
27
|
|
|
*/ |
28
|
|
|
protected $message; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var mixed[] |
32
|
|
|
*/ |
33
|
|
|
protected $params; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ChannelInterface $channel |
37
|
|
|
* @param ProtocolInterface $protocol |
38
|
|
|
* @param string|string[]|Error|Exception $message |
39
|
|
|
* @param mixed[] $params |
40
|
|
|
*/ |
41
|
6 |
|
public function __construct($channel, $protocol, $message, $params = []) |
42
|
|
|
{ |
43
|
6 |
|
$this->channel = $channel; |
44
|
6 |
|
$this->protocol = $protocol; |
45
|
6 |
|
$this->message = $message; |
46
|
6 |
|
$this->params = $params; |
47
|
6 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* |
51
|
|
|
*/ |
52
|
2 |
|
public function __destruct() |
53
|
|
|
{ |
54
|
2 |
|
unset($this->channel); |
55
|
2 |
|
unset($this->protocol); |
56
|
2 |
|
unset($this->message); |
57
|
2 |
|
unset($this->params); |
58
|
2 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Send the prepared response. |
62
|
|
|
* |
63
|
|
|
* @return PromiseInterface |
64
|
|
|
*/ |
65
|
1 |
|
public function __invoke() |
66
|
|
|
{ |
67
|
1 |
|
return $this->send(new Promise()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Send the prepared response. |
72
|
|
|
* |
73
|
|
|
* @return PromiseInterface |
74
|
|
|
* @resolves mixed |
75
|
|
|
* @rejects Error|Exception|string|null |
76
|
|
|
* @cancels Error|Exception|string|null |
77
|
|
|
*/ |
78
|
1 |
|
public function call() |
79
|
|
|
{ |
80
|
1 |
|
return $this->send(new Promise()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Send the request using passed Promise. |
85
|
|
|
* |
86
|
|
|
* @param PromiseInterface $promise |
87
|
|
|
* @return PromiseInterface |
88
|
|
|
* @resolves mixed |
89
|
|
|
* @rejects Error|Exception|string|null |
90
|
|
|
* @cancels Error|Exception|string|null |
91
|
|
|
*/ |
92
|
2 |
|
protected function send(PromiseInterface $promise) |
93
|
|
|
{ |
94
|
2 |
|
$pid = $this->protocol->getPid(); |
95
|
2 |
|
$origin = $this->protocol->getOrigin(); |
96
|
2 |
|
$message = $this->message; |
97
|
2 |
|
$channel = $this->channel; |
98
|
|
|
|
99
|
2 |
|
if ($message instanceof Error || $message instanceof Exception) |
100
|
|
|
{ |
101
|
|
|
$answer = $channel |
102
|
1 |
|
->createProtocol($message->getMessage()) |
103
|
1 |
|
->setPid($pid, true) |
104
|
1 |
|
->setException(get_class($message), true) |
105
|
|
|
; |
106
|
|
|
} |
107
|
|
|
else |
108
|
|
|
{ |
109
|
|
|
$answer = $channel |
110
|
1 |
|
->createProtocol($message) |
111
|
1 |
|
->setPid($pid, true) |
112
|
|
|
; |
113
|
|
|
} |
114
|
|
|
|
115
|
2 |
|
$this->channel->send( |
116
|
2 |
|
$origin, |
117
|
2 |
|
$answer, |
118
|
2 |
|
Channel::MODE_BUFFER_ONLINE |
119
|
|
|
); |
120
|
|
|
|
121
|
2 |
|
return $promise->resolve(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|