|
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\GCM; |
|
11
|
|
|
|
|
12
|
|
|
use Zbox\UnifiedPush\NotificationService\ServiceClientBase; |
|
13
|
|
|
use Zbox\UnifiedPush\Exception\ClientException; |
|
14
|
|
|
use Buzz\Browser; |
|
15
|
|
|
use Buzz\Client\MultiCurl; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class ServiceClient |
|
19
|
|
|
* @package Zbox\UnifiedPush\NotificationService\GCM |
|
20
|
|
|
*/ |
|
21
|
|
|
class ServiceClient extends ServiceClientBase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Initializing HTTP client |
|
25
|
|
|
* |
|
26
|
|
|
* @return $this |
|
27
|
|
|
*/ |
|
28
|
|
|
protected function createClient() |
|
29
|
|
|
{ |
|
30
|
|
|
$client = new MultiCurl(); |
|
31
|
|
|
$client->setVerifyPeer(false); |
|
32
|
|
|
|
|
33
|
|
|
$this->serviceClient = new Browser($client); |
|
34
|
|
|
|
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* When the message is processed successfully, the HTTP response has a 200 status. |
|
40
|
|
|
* Body contains more information about the status of the message. When the request is rejected, |
|
41
|
|
|
* the HTTP response contains a non-200 status code. |
|
42
|
|
|
* |
|
43
|
|
|
* @throws ClientException |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function sendRequest() |
|
47
|
|
|
{ |
|
48
|
|
|
$notification = $this->getNotificationOrThrowException(); |
|
49
|
|
|
|
|
50
|
|
|
try { |
|
51
|
|
|
$connection = $this->getClientConnection(); |
|
52
|
|
|
$serviceURL = $this->getServiceURL(); |
|
53
|
|
|
|
|
54
|
|
|
$response = |
|
55
|
|
|
$connection->post( |
|
56
|
|
|
$serviceURL['url'], |
|
57
|
|
|
$this->getHeaders(), |
|
58
|
|
|
$notification->getPayload() |
|
59
|
|
|
); |
|
60
|
|
|
|
|
61
|
|
|
$connection->getClient()->flush(); |
|
62
|
|
|
|
|
63
|
|
|
} catch (\Exception $e) { |
|
64
|
|
|
throw new ClientException($e->getMessage()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
new Response($response, $notification->getRecipients()); |
|
68
|
|
|
|
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function getHeaders() |
|
76
|
|
|
{ |
|
77
|
|
|
/** @var \Zbox\UnifiedPush\NotificationService\GCM\Credentials $credentials */ |
|
78
|
|
|
$credentials = $this->getCredentials(); |
|
79
|
|
|
|
|
80
|
|
|
return |
|
81
|
|
|
array( |
|
82
|
|
|
sprintf('Authorization: key=%s', $credentials->getAuthToken()), |
|
83
|
|
|
'Content-Type: application/json' |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|