1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the AppleApnPush package |
5
|
|
|
* |
6
|
|
|
* (c) Vitaliy Zhuk <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Apple\ApnPush\Protocol; |
13
|
|
|
|
14
|
|
|
use Apple\ApnPush\Encoder\MessageEncoderInterface; |
15
|
|
|
use Apple\ApnPush\Exception\SendMessage\SendMessageException; |
16
|
|
|
use Apple\ApnPush\Model\Message; |
17
|
|
|
use Apple\ApnPush\Model\Receiver; |
18
|
|
|
use Apple\ApnPush\Protocol\Http\Authenticator\AuthenticatorInterface; |
19
|
|
|
use Apple\ApnPush\Protocol\Http\ExceptionFactory\ExceptionFactoryInterface; |
20
|
|
|
use Apple\ApnPush\Protocol\Http\Request; |
21
|
|
|
use Apple\ApnPush\Protocol\Http\Sender\HttpSenderInterface; |
22
|
|
|
use Apple\ApnPush\Protocol\Http\UriFactory\UriFactoryInterface; |
23
|
|
|
use Apple\ApnPush\Protocol\Http\Visitor\HttpProtocolVisitorInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Implement HTTP protocol for send push notification |
27
|
|
|
*/ |
28
|
|
|
class HttpProtocol implements ProtocolInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var AuthenticatorInterface |
32
|
|
|
*/ |
33
|
|
|
private $authenticator; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var HttpSenderInterface |
37
|
|
|
*/ |
38
|
|
|
private $httpSender; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var MessageEncoderInterface |
42
|
|
|
*/ |
43
|
|
|
private $messageEncoder; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var UriFactoryInterface |
47
|
|
|
*/ |
48
|
|
|
private $uriFactory; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var HttpProtocolVisitorInterface |
52
|
|
|
*/ |
53
|
|
|
private $visitor; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var ExceptionFactoryInterface |
57
|
|
|
*/ |
58
|
|
|
private $exceptionFactory; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Constructor. |
62
|
|
|
* |
63
|
|
|
* @param AuthenticatorInterface $authenticator |
64
|
|
|
* @param HttpSenderInterface $httpSender |
65
|
|
|
* @param MessageEncoderInterface $messageEncoder |
66
|
|
|
* @param UriFactoryInterface $uriFactory |
67
|
|
|
* @param HttpProtocolVisitorInterface $visitor |
68
|
|
|
* @param ExceptionFactoryInterface $exceptionFactory |
69
|
|
|
*/ |
70
|
|
|
public function __construct( |
71
|
|
|
AuthenticatorInterface $authenticator, |
72
|
|
|
HttpSenderInterface $httpSender, |
73
|
|
|
MessageEncoderInterface $messageEncoder, |
74
|
|
|
UriFactoryInterface $uriFactory, |
75
|
|
|
HttpProtocolVisitorInterface $visitor, |
76
|
|
|
ExceptionFactoryInterface $exceptionFactory |
77
|
|
|
) { |
78
|
|
|
$this->authenticator = $authenticator; |
79
|
|
|
$this->httpSender = $httpSender; |
80
|
|
|
$this->messageEncoder = $messageEncoder; |
81
|
|
|
$this->uriFactory = $uriFactory; |
82
|
|
|
$this->visitor = $visitor; |
83
|
|
|
$this->exceptionFactory = $exceptionFactory; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function send(Receiver $receiver, Message $message, bool $sandbox) |
90
|
|
|
{ |
91
|
|
|
try { |
92
|
|
|
$this->doSend($receiver, $message, $sandbox); |
93
|
|
|
} catch (SendMessageException $e) { |
94
|
|
|
$this->httpSender->close(); |
95
|
|
|
|
96
|
|
|
throw $e; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Inner send process |
102
|
|
|
* |
103
|
|
|
* @param Receiver $receiver |
104
|
|
|
* @param Message $message |
105
|
|
|
* @param bool $sandbox |
106
|
|
|
* |
107
|
|
|
* @throws SendMessageException |
108
|
|
|
*/ |
109
|
|
|
private function doSend(Receiver $receiver, Message $message, bool $sandbox) |
110
|
|
|
{ |
111
|
|
|
$content = $this->messageEncoder->encode($message); |
112
|
|
|
$uri = $this->uriFactory->create($receiver->getToken(), $sandbox); |
113
|
|
|
|
114
|
|
|
$request = new Request($uri, $content); |
115
|
|
|
|
116
|
|
|
$headers = [ |
117
|
|
|
'content-type' => 'application/json', |
118
|
|
|
'accept' => 'application/json', |
119
|
|
|
'apns-topic' => $receiver->getTopic(), |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
$request = $request->withHeaders($headers); |
123
|
|
|
$request = $this->authenticator->authenticate($request); |
124
|
|
|
|
125
|
|
|
$request = $this->visitor->visit($message, $request); |
126
|
|
|
|
127
|
|
|
$response = $this->httpSender->send($request); |
128
|
|
|
|
129
|
|
|
if ($response->getStatusCode() !== 200) { |
130
|
|
|
throw $this->exceptionFactory->create($response); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|