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\ResponseInterface; |
13
|
|
|
use Zbox\UnifiedPush\NotificationService\ServiceClientBase; |
14
|
|
|
use Zbox\UnifiedPush\Utils\ClientCredentials\DTO\AuthToken; |
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\GCM |
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
|
|
|
$client->setVerifyPeer(false); |
34
|
|
|
|
35
|
|
|
$this->serviceClient = new Browser($client); |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* When the message is processed successfully, the HTTP response has a 200 status. |
42
|
|
|
* Body contains more information about the status of the message. When the request is rejected, |
43
|
|
|
* the HTTP response contains a non-200 status code. |
44
|
|
|
* |
45
|
|
|
* @throws ClientException |
46
|
|
|
* @return ResponseInterface |
47
|
|
|
*/ |
48
|
|
|
public function sendRequest() |
49
|
|
|
{ |
50
|
|
|
$notification = $this->getNotificationOrThrowException(); |
51
|
|
|
|
52
|
|
|
try { |
53
|
|
|
$connection = $this->getClientConnection(); |
54
|
|
|
$serviceURL = $this->getServiceURL(); |
55
|
|
|
|
56
|
|
|
$response = |
57
|
|
|
$connection->post( |
58
|
|
|
$serviceURL['url'], |
59
|
|
|
$this->getHeaders(), |
60
|
|
|
$notification->getPayload() |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
$connection->getClient()->flush(); |
64
|
|
|
|
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
throw new ClientException($e->getMessage()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return new Response($response, $notification->getRecipients()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
|
protected function getHeaders() |
76
|
|
|
{ |
77
|
|
|
/** @var AuthToken $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
|
|
|
|