1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Notimatica\Driver\Apns; |
4
|
|
|
|
5
|
|
|
class Streamer |
6
|
|
|
{ |
7
|
|
|
const PAYLOAD_MAX_BYTES = 256; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var Certificate |
11
|
|
|
*/ |
12
|
|
|
protected $certificate; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $host; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Resource |
21
|
|
|
*/ |
22
|
|
|
private $apnsResource; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
private $error; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $errorString; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Construct. |
36
|
|
|
* |
37
|
|
|
* @param Certificate $certificate |
38
|
|
|
* @param string $host |
39
|
|
|
*/ |
40
|
1 |
|
public function __construct(Certificate $certificate, $host) |
41
|
|
|
{ |
42
|
1 |
|
$this->certificate = $certificate; |
43
|
1 |
|
$this->host = $host; |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Writes a binary message to apns. |
48
|
|
|
* |
49
|
|
|
* @param string $binaryMessage |
50
|
|
|
* @return int Returns the number of bytes written, or FALSE on error. |
51
|
|
|
* @throws \InvalidArgumentException |
52
|
|
|
*/ |
53
|
1 |
|
public function write($binaryMessage) |
54
|
|
|
{ |
55
|
1 |
|
$length = strlen($binaryMessage); |
56
|
|
|
|
57
|
1 |
|
if ($length > self::PAYLOAD_MAX_BYTES) { |
58
|
|
|
throw new \InvalidArgumentException( |
59
|
|
|
sprintf('The maximum size allowed for a notification payload is %s bytes; Apple Push Notification Service refuses any notification that exceeds this limit.', self::PAYLOAD_MAX_BYTES) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
return fwrite($this->getApnsResource(), $binaryMessage, $length); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create stream resource. |
68
|
|
|
* |
69
|
|
|
* @return Resource |
70
|
|
|
*/ |
71
|
1 |
|
protected function getApnsResource() |
72
|
|
|
{ |
73
|
1 |
|
if (! is_resource($this->apnsResource)) { |
74
|
1 |
|
$this->apnsResource = $this->createStreamClient(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this->apnsResource; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Create stream context. |
82
|
|
|
* |
83
|
|
|
* @return Resource |
84
|
|
|
*/ |
85
|
1 |
|
protected function createStreamContext() |
86
|
|
|
{ |
87
|
1 |
|
$streamContext = stream_context_create(); |
88
|
1 |
|
stream_context_set_option($streamContext, 'ssl', 'local_cert', $this->certificate->getPemCertificatePath()); |
89
|
|
|
|
90
|
1 |
|
return $streamContext; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create stream client. |
95
|
|
|
* |
96
|
|
|
* @return Resource |
97
|
|
|
*/ |
98
|
1 |
|
protected function createStreamClient() |
99
|
|
|
{ |
100
|
1 |
|
$client = stream_socket_client( |
101
|
1 |
|
$this->host, |
102
|
1 |
|
$this->error, |
103
|
1 |
|
$this->errorString, |
104
|
1 |
|
2, |
105
|
1 |
|
STREAM_CLIENT_CONNECT, |
106
|
1 |
|
$this->createStreamContext() |
107
|
1 |
|
); |
108
|
|
|
|
109
|
|
|
return $client; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Close connection. |
114
|
|
|
*/ |
115
|
1 |
|
public function close() |
116
|
|
|
{ |
117
|
1 |
|
if (is_resource($this->apnsResource)) { |
118
|
|
|
fclose($this->apnsResource); |
119
|
|
|
} |
120
|
1 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Destruct callback. |
124
|
|
|
*/ |
125
|
1 |
|
public function __destruct() |
126
|
|
|
{ |
127
|
1 |
|
$this->close(); |
128
|
1 |
|
} |
129
|
|
|
} |
130
|
|
|
|