1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the AppleApnPush package |
7
|
|
|
* |
8
|
|
|
* (c) Vitaliy Zhuk <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Apple\ApnPush\Sender\Builder; |
15
|
|
|
|
16
|
|
|
use Apple\ApnPush\Encoder\PayloadEncoder; |
17
|
|
|
use Apple\ApnPush\Encoder\PayloadEncoderInterface; |
18
|
|
|
use Apple\ApnPush\Protocol\Http\Authenticator\AuthenticatorInterface; |
19
|
|
|
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactory; |
20
|
|
|
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactoryInterface; |
21
|
|
|
use Apple\ApnPush\Protocol\Http\Sender\CurlHttpSender; |
22
|
|
|
use Apple\ApnPush\Protocol\Http\Sender\HttpSenderInterface; |
23
|
|
|
use Apple\ApnPush\Protocol\Http\UriFactory\UriFactory; |
24
|
|
|
use Apple\ApnPush\Protocol\Http\UriFactory\UriFactoryInterface; |
25
|
|
|
use Apple\ApnPush\Protocol\Http\Visitor\AddApnIdHeaderVisitor; |
26
|
|
|
use Apple\ApnPush\Protocol\Http\Visitor\AddCollapseIdHeaderVisitor; |
27
|
|
|
use Apple\ApnPush\Protocol\Http\Visitor\AddExpirationHeaderVisitor; |
28
|
|
|
use Apple\ApnPush\Protocol\Http\Visitor\AddPriorityHeaderVisitor; |
29
|
|
|
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolChainVisitor; |
30
|
|
|
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolVisitorInterface; |
31
|
|
|
use Apple\ApnPush\Protocol\HttpProtocol; |
32
|
|
|
use Apple\ApnPush\Protocol\ProtocolInterface; |
33
|
|
|
use Apple\ApnPush\Sender\Sender; |
34
|
|
|
use Apple\ApnPush\Sender\SenderInterface; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Builder for create sender with HTTP/2 protocol |
38
|
|
|
*/ |
39
|
|
|
class Http20Builder implements BuilderInterface |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* @var \SplPriorityQueue|HttpProtocolVisitorInterface[] |
43
|
|
|
*/ |
44
|
|
|
private $visitors; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var UriFactoryInterface |
48
|
|
|
*/ |
49
|
|
|
private $uriFactory; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var PayloadEncoderInterface |
53
|
|
|
*/ |
54
|
|
|
private $payloadEncoder; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var AuthenticatorInterface |
58
|
|
|
*/ |
59
|
|
|
private $authenticator; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var HttpSenderInterface |
63
|
|
|
*/ |
64
|
|
|
private $httpSender; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var ExceptionFactoryInterface |
68
|
|
|
*/ |
69
|
|
|
private $exceptionFactory; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Constructor. |
73
|
|
|
* |
74
|
|
|
* @param AuthenticatorInterface $authenticator |
75
|
|
|
*/ |
76
|
|
|
public function __construct(AuthenticatorInterface $authenticator) |
77
|
|
|
{ |
78
|
|
|
$this->authenticator = $authenticator; |
79
|
|
|
$this->visitors = new \SplPriorityQueue(); |
80
|
|
|
$this->uriFactory = new UriFactory(); |
81
|
|
|
$this->payloadEncoder = new PayloadEncoder(); |
82
|
|
|
$this->httpSender = new CurlHttpSender(); |
83
|
|
|
$this->exceptionFactory = new ExceptionFactory(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set authenticator |
88
|
|
|
* |
89
|
|
|
* @param AuthenticatorInterface $authenticator |
90
|
|
|
* |
91
|
|
|
* @return Http20Builder |
92
|
|
|
*/ |
93
|
|
|
public function setAuthenticator(AuthenticatorInterface $authenticator): Http20Builder |
94
|
|
|
{ |
95
|
|
|
$this->authenticator = $authenticator; |
96
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Add visitor |
102
|
|
|
* |
103
|
|
|
* @param HttpProtocolVisitorInterface $visitor |
104
|
|
|
* @param int $priority |
105
|
|
|
* |
106
|
|
|
* @return Http20Builder |
107
|
|
|
*/ |
108
|
|
|
public function addVisitor(HttpProtocolVisitorInterface $visitor, int $priority = 0): Http20Builder |
109
|
|
|
{ |
110
|
|
|
$this->visitors->insert($visitor, $priority); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Add default visitors |
117
|
|
|
* |
118
|
|
|
* @return Http20Builder |
119
|
|
|
*/ |
120
|
|
|
public function addDefaultVisitors(): Http20Builder |
121
|
|
|
{ |
122
|
|
|
$this->addVisitor(new AddExpirationHeaderVisitor()); |
123
|
|
|
$this->addVisitor(new AddPriorityHeaderVisitor()); |
124
|
|
|
$this->addVisitor(new AddApnIdHeaderVisitor()); |
125
|
|
|
$this->addVisitor(new AddCollapseIdHeaderVisitor()); |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set URI factory |
132
|
|
|
* |
133
|
|
|
* @param UriFactoryInterface $uriFactory |
134
|
|
|
* |
135
|
|
|
* @return Http20Builder |
136
|
|
|
*/ |
137
|
|
|
public function setUriFactory(UriFactoryInterface $uriFactory): Http20Builder |
138
|
|
|
{ |
139
|
|
|
$this->uriFactory = $uriFactory; |
140
|
|
|
|
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Set notification encoder |
146
|
|
|
* |
147
|
|
|
* @param PayloadEncoderInterface $payloadEncoder |
148
|
|
|
* |
149
|
|
|
* @return Http20Builder |
150
|
|
|
*/ |
151
|
|
|
public function setPayloadEncoder(PayloadEncoderInterface $payloadEncoder): Http20Builder |
152
|
|
|
{ |
153
|
|
|
$this->payloadEncoder = $payloadEncoder; |
154
|
|
|
|
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set http sender |
160
|
|
|
* |
161
|
|
|
* @param HttpSenderInterface $httpSender |
162
|
|
|
* |
163
|
|
|
* @return Http20Builder |
164
|
|
|
*/ |
165
|
|
|
public function setHttpSender(HttpSenderInterface $httpSender): Http20Builder |
166
|
|
|
{ |
167
|
|
|
$this->httpSender = $httpSender; |
168
|
|
|
|
169
|
|
|
return $this; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Set exception factory |
174
|
|
|
* |
175
|
|
|
* @param ExceptionFactoryInterface $exceptionFactory |
176
|
|
|
* |
177
|
|
|
* @return Http20Builder |
178
|
|
|
*/ |
179
|
|
|
public function setExceptionFactory(ExceptionFactoryInterface $exceptionFactory): Http20Builder |
180
|
|
|
{ |
181
|
|
|
$this->exceptionFactory = $exceptionFactory; |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
|
|
public function buildProtocol(): ProtocolInterface |
190
|
|
|
{ |
191
|
|
|
$chainVisitor = $this->createChainVisitor(); |
192
|
|
|
|
193
|
|
|
return new HttpProtocol( |
194
|
|
|
$this->authenticator, |
195
|
|
|
$this->httpSender, |
196
|
|
|
$this->payloadEncoder, |
197
|
|
|
$this->uriFactory, |
198
|
|
|
$chainVisitor, |
199
|
|
|
$this->exceptionFactory |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
|
|
public function build(): SenderInterface |
207
|
|
|
{ |
208
|
|
|
$protocol = $this->buildProtocol(); |
209
|
|
|
|
210
|
|
|
return new Sender($protocol); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Create chain visitor |
215
|
|
|
* |
216
|
|
|
* @return HttpProtocolChainVisitor |
217
|
|
|
*/ |
218
|
|
|
private function createChainVisitor(): HttpProtocolChainVisitor |
219
|
|
|
{ |
220
|
|
|
$chainVisitors = new HttpProtocolChainVisitor(); |
221
|
|
|
$visitors = clone $this->visitors; |
222
|
|
|
$priority = 0; |
223
|
|
|
|
224
|
|
|
foreach ($visitors as $visitor) { |
225
|
|
|
$chainVisitors->add($visitor, $priority++); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
return $chainVisitors; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|