1 | <?php |
||
27 | class FcmAdapter implements PushAdapter |
||
28 | { |
||
29 | use ParameterTrait; |
||
30 | use SandboxTrait; |
||
31 | |||
32 | const PARAMETER_API_KEY = 'api_key'; |
||
33 | |||
34 | const RESPONSE_OK = 'OK'; |
||
35 | const RESPONSE_MISSING_REGISTRATION = 'MissingRegistration'; |
||
36 | const RESPONSE_INVALID_REGISTRATION = 'InvalidRegistration'; |
||
37 | const RESPONSE_NOT_REGISTERED = 'NotRegistered'; |
||
38 | const RESPONSE_INVALID_PACKAGE_NAME = 'InvalidPackageName'; |
||
39 | const RESPONSE_MISMATCH_SENDER_ID = 'MismatchSenderId'; |
||
40 | const RESPONSE_MESSAGE_TOO_BIG = 'MessageTooBig'; |
||
41 | const RESPONSE_INVALID_DATA_KEY = 'InvalidDataKey'; |
||
42 | const RESPONSE_INVALID_TTL = 'InvalidTtl'; |
||
43 | const RESPONSE_TIMEOUT = 'Timeout'; |
||
44 | const RESPONSE_INTERNAL_SERVER_ERROR = 'InternalServerError'; |
||
45 | const RESPONSE_DEVICE_MESSAGE_RATE_EXCEEDED = 'DeviceMessageRateExceeded'; |
||
46 | const RESPONSE_TOPIC_MESSAGE_RATE_EXCEEDED = 'TopicsMessageRateExceeded'; |
||
47 | const RESPONSE_SERVER_UNAVAILABLE = 'ServerUnavailable'; |
||
48 | const RESPONSE_AUTHENTICATION_ERROR = 'AuthenticationError'; |
||
49 | const RESPONSE_INVALID_MESSAGE = 'InvalidMessage'; |
||
50 | const RESPONSE_BADLY_FORMATTED_RESPONSE = 'BadlyFormattedResponse'; |
||
51 | const RESPONSE_UNKNOWN_ERROR = 'Unknown'; |
||
52 | |||
53 | /** |
||
54 | * Status codes mapping. |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | protected static $statusCodeMap = [ |
||
59 | self::RESPONSE_OK => Result::STATUS_SUCCESS, |
||
60 | |||
61 | self::RESPONSE_MISSING_REGISTRATION => Result::STATUS_INVALID_MESSAGE, |
||
62 | self::RESPONSE_INVALID_PACKAGE_NAME => Result::STATUS_INVALID_MESSAGE, |
||
63 | self::RESPONSE_MISMATCH_SENDER_ID => Result::STATUS_INVALID_MESSAGE, |
||
64 | self::RESPONSE_MESSAGE_TOO_BIG => Result::STATUS_INVALID_MESSAGE, |
||
65 | self::RESPONSE_INVALID_DATA_KEY => Result::STATUS_INVALID_MESSAGE, |
||
66 | self::RESPONSE_INVALID_TTL => Result::STATUS_INVALID_MESSAGE, |
||
67 | self::RESPONSE_INVALID_MESSAGE => Result::STATUS_INVALID_MESSAGE, |
||
68 | |||
69 | self::RESPONSE_INVALID_REGISTRATION => Result::STATUS_INVALID_DEVICE, |
||
70 | self::RESPONSE_NOT_REGISTERED => Result::STATUS_INVALID_DEVICE, |
||
71 | |||
72 | self::RESPONSE_DEVICE_MESSAGE_RATE_EXCEEDED => Result::STATUS_RATE_ERROR, |
||
73 | self::RESPONSE_TOPIC_MESSAGE_RATE_EXCEEDED => Result::STATUS_RATE_ERROR, |
||
74 | |||
75 | self::RESPONSE_AUTHENTICATION_ERROR => Result::STATUS_AUTH_ERROR, |
||
76 | |||
77 | self::RESPONSE_TIMEOUT => Result::STATUS_SERVER_ERROR, |
||
78 | self::RESPONSE_INTERNAL_SERVER_ERROR => Result::STATUS_SERVER_ERROR, |
||
79 | self::RESPONSE_SERVER_UNAVAILABLE => Result::STATUS_SERVER_ERROR, |
||
80 | self::RESPONSE_BADLY_FORMATTED_RESPONSE => Result::STATUS_SERVER_ERROR, |
||
81 | |||
82 | self::RESPONSE_UNKNOWN_ERROR => Result::STATUS_UNKNOWN_ERROR, |
||
83 | ]; |
||
84 | |||
85 | /** |
||
86 | * Status messages mapping. |
||
87 | * |
||
88 | * @see https://developers.google.com/cloud-messaging/http-server-ref |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | protected static $statusMessageMap = [ |
||
93 | self::RESPONSE_OK => 'OK', |
||
94 | |||
95 | self::RESPONSE_MISSING_REGISTRATION => 'Missing Registration Token', |
||
96 | self::RESPONSE_INVALID_PACKAGE_NAME => 'Invalid Package Name', |
||
97 | self::RESPONSE_MISMATCH_SENDER_ID => 'Mismatched Sender', |
||
98 | self::RESPONSE_MESSAGE_TOO_BIG => 'Message Too Big', |
||
99 | self::RESPONSE_INVALID_DATA_KEY => 'Invalid Data Key', |
||
100 | self::RESPONSE_INVALID_TTL => 'Invalid Time to Live', |
||
101 | self::RESPONSE_INVALID_MESSAGE => 'Invalid message', |
||
102 | |||
103 | self::RESPONSE_INVALID_REGISTRATION => 'Invalid Registration Token', |
||
104 | self::RESPONSE_NOT_REGISTERED => 'Unregistered Device', |
||
105 | |||
106 | self::RESPONSE_DEVICE_MESSAGE_RATE_EXCEEDED => 'Device Message Rate Exceeded', |
||
107 | self::RESPONSE_TOPIC_MESSAGE_RATE_EXCEEDED => 'Topics Message Rate Exceeded', |
||
108 | |||
109 | self::RESPONSE_AUTHENTICATION_ERROR => 'Authentication Error', |
||
110 | |||
111 | self::RESPONSE_TIMEOUT => 'Timeout', |
||
112 | self::RESPONSE_INTERNAL_SERVER_ERROR => 'Internal Server Error', |
||
113 | self::RESPONSE_SERVER_UNAVAILABLE => 'Server Unavailable', |
||
114 | self::RESPONSE_BADLY_FORMATTED_RESPONSE => 'Bad Formatted Response', |
||
115 | |||
116 | self::RESPONSE_UNKNOWN_ERROR => 'Unknown Error', |
||
117 | ]; |
||
118 | |||
119 | /** |
||
120 | * FCM service factory. |
||
121 | * |
||
122 | * @var Factory |
||
123 | */ |
||
124 | protected $factory; |
||
125 | |||
126 | /** |
||
127 | * Push service client. |
||
128 | * |
||
129 | * @var \ZendService\Google\Gcm\Client |
||
130 | */ |
||
131 | protected $pushClient; |
||
132 | |||
133 | /** |
||
134 | * FCM service adapter constructor. |
||
135 | * |
||
136 | * @param array $parameters |
||
137 | * @param bool $sandbox |
||
138 | * @param Factory $factory |
||
|
|||
139 | * |
||
140 | * @throws \Jgut\Tify\Exception\AdapterException |
||
141 | */ |
||
142 | public function __construct(array $parameters = [], $sandbox = false, Factory $factory = null) |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | * |
||
159 | * @throws \InvalidArgumentException |
||
160 | * @throws \ZendService\Google\Exception\InvalidArgumentException |
||
161 | * @throws \ZendService\Google\Exception\RuntimeException |
||
162 | */ |
||
163 | public function push(Notification $notification) |
||
209 | |||
210 | /** |
||
211 | * Get opened push client. |
||
212 | * |
||
213 | * @return \ZendService\Google\Gcm\Client |
||
214 | */ |
||
215 | protected function getPushClient() |
||
223 | |||
224 | /** |
||
225 | * Get service formatted push message. |
||
226 | * |
||
227 | * @param Notification $notification |
||
228 | * |
||
229 | * @throws \ZendService\Google\Exception\InvalidArgumentException |
||
230 | * @throws \ZendService\Google\Exception\RuntimeException |
||
231 | * |
||
232 | * @return \Generator|\ZendService\Google\Gcm\Message[] |
||
233 | */ |
||
234 | protected function getPushMessage(Notification $notification) |
||
253 | |||
254 | /** |
||
255 | * Extract error code from exception. |
||
256 | * |
||
257 | * @param GcmRuntimeException $exception |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | protected function getErrorCodeFromException(GcmRuntimeException $exception) |
||
287 | |||
288 | /** |
||
289 | * {@inheritdoc} |
||
290 | */ |
||
291 | protected function getDefinedParameters() |
||
295 | |||
296 | /** |
||
297 | * {@inheritdoc} |
||
298 | */ |
||
299 | protected function getRequiredParameters() |
||
303 | } |
||
304 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.