|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright 2015 Alexey Maslov <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace alxmsl\Google\GCM; |
|
19
|
|
|
|
|
20
|
|
|
use alxmsl\Google\GCM\Exception\GCMFormatException; |
|
21
|
|
|
use alxmsl\Google\GCM\Exception\GCMServerError; |
|
22
|
|
|
use alxmsl\Google\GCM\Exception\GCMUnauthorizedException; |
|
23
|
|
|
use alxmsl\Google\GCM\Exception\GCMUnrecoverableError; |
|
24
|
|
|
use alxmsl\Google\GCM\Message\PayloadMessage; |
|
25
|
|
|
use alxmsl\Google\GCM\Response\Response; |
|
26
|
|
|
use alxmsl\Network\Exception\HttpClientErrorCodeException; |
|
27
|
|
|
use alxmsl\Network\Exception\HttpServerErrorCodeException; |
|
28
|
|
|
use alxmsl\Network\Http\Request; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* GCM sender client class |
|
32
|
|
|
* @author alxmsl |
|
33
|
|
|
* @date 5/26/13 |
|
34
|
|
|
*/ |
|
35
|
|
|
final class Client { |
|
36
|
|
|
/** |
|
37
|
|
|
* Supported content types |
|
38
|
|
|
*/ |
|
39
|
|
|
const CONTENT_TYPE_JSON = 'application/json', |
|
40
|
|
|
CONTENT_TYPE_PLAIN_TEXT = 'application/x-www-form-urlencoded; charset=UTF-8'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* GCM sender service endpoint |
|
44
|
|
|
*/ |
|
45
|
|
|
const ENDPOINT_SEND = 'https://android.googleapis.com/gcm/send'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string authorization key |
|
49
|
|
|
*/ |
|
50
|
|
|
private $authorizationKey = ''; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var null|Request HTTP request instance |
|
54
|
|
|
*/ |
|
55
|
|
|
private $Request = null; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Authorization key setter |
|
59
|
|
|
* @param string $authorizationKey authorization key |
|
60
|
|
|
* @return Client self |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function setAuthorizationKey($authorizationKey) { |
|
63
|
1 |
|
$this->authorizationKey = (string) $authorizationKey; |
|
64
|
1 |
|
$this->getRequest()->addHeader('Authorization', 'key=' . $this->getAuthorizationKey()); |
|
65
|
1 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Authorization key getter |
|
70
|
|
|
* @return string authorization key |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function getAuthorizationKey() { |
|
73
|
2 |
|
return $this->authorizationKey; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* GCM service request instance getter |
|
78
|
|
|
* @return Request GCM service request instance |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public function getRequest() { |
|
81
|
2 |
|
if (is_null($this->Request)) { |
|
82
|
2 |
|
$this->Request = new Request(); |
|
83
|
2 |
|
$this->Request->setTransport(Request::TRANSPORT_CURL); |
|
84
|
2 |
|
$this->Request->setUrl(self::ENDPOINT_SEND); |
|
85
|
2 |
|
} |
|
86
|
2 |
|
return $this->Request; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Send GCM message method |
|
91
|
|
|
* @param PayloadMessage $Message GCM message instance |
|
92
|
|
|
* @return Response sent response instance |
|
93
|
|
|
* @throws GCMFormatException when request or response format was incorrect |
|
94
|
|
|
* @throws GCMUnauthorizedException when was incorrect authorization key |
|
95
|
|
|
* @throws GCMServerError when something wrong on the GCM server |
|
96
|
|
|
* @throws GCMUnrecoverableError when GCM server is not available |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function send(PayloadMessage $Message) { |
|
99
|
1 |
|
switch ($Message->getType()) { |
|
100
|
1 |
|
case PayloadMessage::TYPE_PLAIN: |
|
101
|
|
|
$this->getRequest()->setContentTypeCode(Request::CONTENT_TYPE_UNDEFINED); |
|
102
|
|
|
$this->getRequest()->addHeader('Content-Type', self::CONTENT_TYPE_PLAIN_TEXT); |
|
103
|
|
|
break; |
|
104
|
1 |
|
case PayloadMessage::TYPE_JSON: |
|
105
|
|
|
$this->getRequest()->setContentTypeCode(Request::CONTENT_TYPE_JSON); |
|
106
|
|
|
$this->getRequest()->addHeader('Content-Type', self::CONTENT_TYPE_JSON); |
|
107
|
|
|
break; |
|
108
|
1 |
|
default: |
|
109
|
1 |
|
throw new GCMFormatException('unsupported request format code \'' . $Message->getType() . '\''); |
|
110
|
1 |
|
} |
|
111
|
|
|
$this->getRequest()->setPostData($Message->export()); |
|
112
|
|
|
try { |
|
113
|
|
|
$result = $this->getRequest()->send(); |
|
114
|
|
|
Response::$type = $Message->getType(); |
|
115
|
|
|
return Response::initializeByString($result); |
|
116
|
|
|
} catch (HttpClientErrorCodeException $Ex) { |
|
117
|
|
|
switch ($Ex->getCode()) { |
|
118
|
|
|
case '400': |
|
119
|
|
|
throw new GCMFormatException('invalid JSON request with message \'' . $Ex->getMessage() . '\''); |
|
120
|
|
|
case '401': |
|
121
|
|
|
throw new GCMUnauthorizedException('invalid authorization key \'' . $this->getAuthorizationKey() . '\''); |
|
122
|
|
|
default: |
|
123
|
|
|
throw $Ex; |
|
124
|
|
|
} |
|
125
|
|
|
} catch (HttpServerErrorCodeException $Ex) { |
|
126
|
|
|
switch ($Ex->getCode()) { |
|
127
|
|
|
case '500': |
|
128
|
|
|
throw new GCMUnrecoverableError('unrecoverable GCM server error'); |
|
129
|
|
|
default: |
|
130
|
|
|
$headers = $this->getRequest()->getResponseHeaders(); |
|
131
|
|
|
throw new GCMServerError(@$headers['Retry-After']); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|