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\MPNS; |
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\Exception\ClientException; |
16
|
|
|
use Buzz\Browser; |
17
|
|
|
use Buzz\Client\MultiCurl; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ServiceClient |
21
|
|
|
* @package Zbox\UnifiedPush\NotificationService\MPNS |
22
|
|
|
*/ |
23
|
|
|
class ServiceClient extends ServiceClientBase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Initializing HTTP client |
27
|
|
|
* |
28
|
|
|
* @return $this |
29
|
|
|
*/ |
30
|
|
|
protected function createClient() |
31
|
|
|
{ |
32
|
|
|
$client = new MultiCurl(); |
33
|
|
|
|
34
|
|
|
$credentials = $this->getCredentials(); |
35
|
|
|
$isAuthenticated = $credentials instanceof SSLCertificate; |
36
|
|
|
|
37
|
|
|
$client->setVerifyPeer($isAuthenticated); |
38
|
|
|
|
39
|
|
|
if ($isAuthenticated) { |
40
|
|
|
$client->setOption(CURLOPT_SSLCERT, $credentials->getCertificatePassPhrase()); |
41
|
|
|
$client->setOption(CURLOPT_SSLCERTPASSWD, $credentials->getCertificatePassPhrase()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$this->serviceClient = new Browser($client); |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @throws ClientException |
51
|
|
|
* @return ResponseInterface |
52
|
|
|
*/ |
53
|
|
|
public function sendRequest() |
54
|
|
|
{ |
55
|
|
|
$notification = $this->getNotificationOrThrowException(); |
56
|
|
|
|
57
|
|
|
try { |
58
|
|
|
$connection = $this->getClientConnection(); |
59
|
|
|
$serviceURL = $this->getServiceURL(); |
60
|
|
|
$url = str_replace('[TOKEN]', $notification->getRecipients()->current(), $serviceURL['url']); |
61
|
|
|
|
62
|
|
|
$response = |
63
|
|
|
$connection->post( |
64
|
|
|
$url, |
65
|
|
|
$this->getHeaders(), |
66
|
|
|
$notification->getPayload() |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$connection->getClient()->flush(); |
70
|
|
|
|
71
|
|
|
} catch (\Exception $e) { |
72
|
|
|
throw new ClientException($e->getMessage()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return new Response($response, $notification->getRecipients()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
protected function getMPNSHeaders() |
82
|
|
|
{ |
83
|
|
|
$notification = $this->getNotificationOrThrowException(); |
84
|
|
|
|
85
|
|
|
$customNotification = $notification->getCustomNotificationData(); |
86
|
|
|
|
87
|
|
|
return |
88
|
|
|
array( |
89
|
|
|
'X-MessageID' => $customNotification['message_id'], |
90
|
|
|
'X-NotificationClass' => $customNotification['delay_interval'], |
91
|
|
|
'X-WindowsPhone-Target' => $customNotification['message_type'] |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
protected function getHeaders() |
99
|
|
|
{ |
100
|
|
|
$headers = array( |
101
|
|
|
'Accept' => 'application/*', |
102
|
|
|
'Content-Type' => 'text/xml' |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
$headers += $this->getMPNSHeaders(); |
106
|
|
|
|
107
|
|
|
return |
108
|
|
|
array_map( |
109
|
|
|
function ($value, $key) { |
110
|
|
|
return sprintf("%s: %s'", $key, $value); |
111
|
|
|
}, |
112
|
|
|
$headers, |
113
|
|
|
array_keys($headers) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|