|
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\ResponseInterface; |
|
13
|
|
|
use Zbox\UnifiedPush\NotificationService\ServiceClientBase; |
|
14
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\SSLCertificate; |
|
15
|
|
|
use Zbox\UnifiedPush\Utils\SocketClient; |
|
16
|
|
|
use Zbox\UnifiedPush\Exception\ClientException; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class ServiceClient |
|
20
|
|
|
* @package Zbox\UnifiedPush\NotificationService\APNS |
|
21
|
|
|
*/ |
|
22
|
|
|
class ServiceClient extends ServiceClientBase |
|
23
|
|
|
{ |
|
24
|
|
|
const SECURE_TRANSPORT_DEFAULT = 'ssl'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Initializing socket client |
|
28
|
|
|
* |
|
29
|
|
|
* @return $this |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function createClient() |
|
32
|
|
|
{ |
|
33
|
|
|
/** @var SSLCertificate $credentials */ |
|
34
|
|
|
$credentials = $this->getCredentials(); |
|
35
|
|
|
$url = $this->getServiceURL(); |
|
36
|
|
|
$transport = !empty($url['transport']) ? $url['transport'] : self::SECURE_TRANSPORT_DEFAULT; |
|
37
|
|
|
|
|
38
|
|
|
$this->serviceClient = new SocketClient($url['host'], $url['port']); |
|
39
|
|
|
$this->serviceClient |
|
40
|
|
|
->setTransport($transport) |
|
41
|
|
|
->setContextOptions(array( |
|
42
|
|
|
'local_cert' => $credentials->getCertificate(), |
|
43
|
|
|
'passphrase' => $credentials->getCertificatePassPhrase(), |
|
44
|
|
|
)) |
|
45
|
|
|
->addConnectionFlag(STREAM_CLIENT_PERSISTENT) |
|
46
|
|
|
->setBlockingMode(false) |
|
47
|
|
|
; |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Gets socket connection (reestablish, if needed) |
|
54
|
|
|
* |
|
55
|
|
|
* @return SocketClient |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getClientConnection() |
|
58
|
|
|
{ |
|
59
|
|
|
if (!$this->serviceClient) { |
|
60
|
|
|
$this->createClient(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if (!$this->serviceClient->isAlive()) { |
|
64
|
|
|
$this->serviceClient->connect(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->serviceClient; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* If you send a notification that is accepted by APNs, |
|
72
|
|
|
* nothing is returned. If you send a notification that is malformed |
|
73
|
|
|
* or otherwise unintelligible, APNs returns an error-response packet |
|
74
|
|
|
* |
|
75
|
|
|
* @throws ClientException |
|
76
|
|
|
* @return ResponseInterface |
|
77
|
|
|
*/ |
|
78
|
|
|
public function sendRequest() |
|
79
|
|
|
{ |
|
80
|
|
|
$notification = $this->getNotificationOrThrowException(); |
|
81
|
|
|
|
|
82
|
|
|
try { |
|
83
|
|
|
$connection = $this->getClientConnection(); |
|
84
|
|
|
$connection->write($notification->getPayload()); |
|
85
|
|
|
|
|
86
|
|
|
$errorResponseData = $connection->read(Response::ERROR_RESPONSE_LENGTH); |
|
87
|
|
|
|
|
88
|
|
|
} catch (\Exception $e) { |
|
89
|
|
|
throw new ClientException($e->getMessage()); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return new Response($errorResponseData ? : '', $notification->getRecipients()); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|