1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush\NotificationService\APNS; |
11
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\NotificationService\ServiceClientBase; |
13
|
|
|
use Zbox\UnifiedPush\Exception\ClientException; |
14
|
|
|
use Zbox\UnifiedPush\Utils\SocketClient; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ServiceClient |
18
|
|
|
* @package Zbox\UnifiedPush\NotificationService\APNS |
19
|
|
|
*/ |
20
|
|
|
class ServiceClient extends ServiceClientBase |
21
|
|
|
{ |
22
|
|
|
const SECURE_TRANSPORT_DEFAULT = 'ssl'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Initializing socket client |
26
|
|
|
* |
27
|
|
|
* @return $this |
28
|
|
|
*/ |
29
|
|
|
protected function createClient() |
30
|
|
|
{ |
31
|
|
|
$credentials = $this->getCredentials(); |
32
|
|
|
$url = $this->getServiceURL(); |
33
|
|
|
$transport = !empty($url['transport']) ? $url['transport'] : self::SECURE_TRANSPORT_DEFAULT; |
34
|
|
|
|
35
|
|
|
$this->serviceClient = new SocketClient($url['host'], $url['port']); |
36
|
|
|
$this->serviceClient |
37
|
|
|
->setTransport($transport) |
38
|
|
|
->setContextOptions(array( |
39
|
|
|
'local_cert' => $credentials->getCertificate(), |
40
|
|
|
'passphrase' => $credentials->getCertificatePassPhrase(), |
41
|
|
|
)) |
42
|
|
|
->addConnectionFlag(STREAM_CLIENT_PERSISTENT) |
43
|
|
|
->setBlockingMode(false) |
44
|
|
|
; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Gets socket connection (reestablish, if needed) |
51
|
|
|
* |
52
|
|
|
* @return SocketClient |
53
|
|
|
*/ |
54
|
|
|
public function getClientConnection() |
55
|
|
|
{ |
56
|
|
|
if (!$this->serviceClient) { |
57
|
|
|
$this->createClient(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (!$this->serviceClient->isAlive()) { |
61
|
|
|
$this->serviceClient->connect(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->serviceClient; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* If you send a notification that is accepted by APNs, |
69
|
|
|
* nothing is returned. If you send a notification that is malformed |
70
|
|
|
* or otherwise unintelligible, APNs returns an error-response packet |
71
|
|
|
* |
72
|
|
|
* @throws ClientException |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function sendRequest() |
76
|
|
|
{ |
77
|
|
|
$notification = $this->getNotificationOrThrowException(); |
78
|
|
|
|
79
|
|
|
try { |
80
|
|
|
$connection = $this->getClientConnection(); |
81
|
|
|
$connection->write($notification['body']); |
82
|
|
|
|
83
|
|
|
$errorResponseData = $connection->read(Response::ERROR_RESPONSE_LENGTH); |
84
|
|
|
|
85
|
|
|
} catch (\Exception $e) { |
86
|
|
|
throw new ClientException($e->getMessage()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($errorResponseData) { |
|
|
|
|
90
|
|
|
new Response($errorResponseData, $notification['recipients']); |
91
|
|
|
} |
92
|
|
|
return true; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: