1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP APNS. |
4
|
|
|
* |
5
|
|
|
* @author Gennady Telegin <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Apns\Handler; |
12
|
|
|
|
13
|
|
|
use Apns\Client; |
14
|
|
|
use Apns\Exception\ApnsException; |
15
|
|
|
use Apns\Exception\CertificateException; |
16
|
|
|
use Apns\Exception\InactiveDeviceTokenException; |
17
|
|
|
use Apns\Message; |
18
|
|
|
use GuzzleHttp\Client as HttpClient; |
19
|
|
|
use GuzzleHttp\Exception\RequestException; |
20
|
|
|
|
21
|
|
|
if (!defined('CURL_HTTP_VERSION_2_0')) { |
22
|
|
|
define('CURL_HTTP_VERSION_2_0', 3); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class GuzzleHandler. |
27
|
|
|
*/ |
28
|
|
|
class GuzzleHandler |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var Client |
32
|
|
|
*/ |
33
|
|
|
private $httpClient; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* GuzzleHandler constructor. |
37
|
|
|
*/ |
38
|
|
|
public function __construct() |
39
|
|
|
{ |
40
|
|
|
$this->httpClient = new HttpClient(); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Client $apns |
45
|
|
|
* @param Message $message |
46
|
|
|
* |
47
|
|
|
* @return bool |
48
|
|
|
* |
49
|
|
|
* @throws ApnsException |
50
|
|
|
*/ |
51
|
|
|
public function __invoke(Client $apns, Message $message) |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
|
|
$response = $this->httpClient->request( |
|
|
|
|
55
|
|
|
'POST', |
56
|
|
|
$apns->getPushURI($message), |
57
|
|
|
[ |
58
|
|
|
'json' => $message->getMessageBody(), |
59
|
|
|
'cert' => $apns->getSslCert(), |
60
|
|
|
'headers' => $message->getMessageHeaders(), |
61
|
|
|
'curl' => [ |
62
|
|
|
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_2_0, |
63
|
|
|
], |
64
|
|
|
] |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
return 200 === $response->getStatusCode(); |
68
|
|
|
} catch (RequestException $e) { |
69
|
|
|
throw self::factoryException($e); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param RequestException $exception |
75
|
|
|
* |
76
|
|
|
* @return ApnsException |
77
|
|
|
*/ |
78
|
|
|
private static function factoryException(RequestException $exception) |
79
|
|
|
{ |
80
|
|
|
$response = $exception->getResponse(); |
81
|
|
|
|
82
|
|
|
if (null === $response) { |
83
|
|
|
return new ApnsException( |
84
|
|
|
'Unknown network error', |
85
|
|
|
0, |
86
|
|
|
$exception |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
try { |
91
|
|
|
$data = json_decode($response->getBody()->getContents(), true); |
92
|
|
|
} catch (\Exception $e) { |
93
|
|
|
return new ApnsException( |
94
|
|
|
'Unknown network error', |
95
|
|
|
0, |
96
|
|
|
$e |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$reason = isset($data['reason']) ? (string) $data['reason'] : ''; |
101
|
|
|
|
102
|
|
|
switch ($response->getStatusCode()) { |
103
|
|
|
case 403: |
104
|
|
|
return new CertificateException( |
105
|
|
|
$reason, |
106
|
|
|
$response->getStatusCode(), |
107
|
|
|
$exception |
108
|
|
|
); |
109
|
|
|
|
110
|
|
|
case 410: |
111
|
|
|
return new InactiveDeviceTokenException( |
112
|
|
|
$reason, |
113
|
|
|
isset($data['timestamp']) ? $data['timestamp'] : 0, |
114
|
|
|
$response->getStatusCode(), |
115
|
|
|
$exception |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
default: |
119
|
|
|
return new ApnsException( |
120
|
|
|
$reason, |
121
|
|
|
$response->getStatusCode(), |
122
|
|
|
$exception |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..