1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FirebaseCloudMessagingBundle |
4
|
|
|
* |
5
|
|
|
* (c) Artem Henvald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Fresh\FirebaseCloudMessagingBundle\Response; |
14
|
|
|
|
15
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Exception\AuthenticationException; |
16
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Exception\ExceptionInterface; |
17
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Exception\InternalServerErrorException; |
18
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Exception\InvalidJsonException; |
19
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Exception\UnsupportedResponseException; |
20
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Message\Part\Target\TokenTargetInterface; |
21
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Message\Type\AbstractMessage; |
22
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\CanonicalTokenMessageResult; |
23
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\Collection\CanonicalTokenMessageResultCollection; |
24
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\Collection\FailedMessageResultCollection; |
25
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\Collection\SuccessfulMessageResultCollection; |
26
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\FailedMessageResult; |
27
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Response\MessageResult\SuccessfulMessageResult; |
28
|
|
|
use Psr\Http\Message\ResponseInterface; |
29
|
|
|
use Symfony\Component\HttpFoundation\Response; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class ResponseProcessor. |
33
|
|
|
* |
34
|
|
|
* @author Artem Henvald <[email protected]> |
35
|
|
|
*/ |
36
|
|
|
class ResponseProcessor |
37
|
|
|
{ |
38
|
|
|
/** @var array */ |
39
|
|
|
private $jsonContentTypes = [ |
40
|
|
|
'application/json', |
41
|
|
|
'application/json; charset=UTF-8', |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
/** @var AbstractMessage */ |
45
|
|
|
private $message; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param AbstractMessage $message |
49
|
|
|
* @param ResponseInterface $response |
50
|
|
|
* |
51
|
|
|
* @throws ExceptionInterface |
52
|
|
|
* |
53
|
|
|
* @return FirebaseResponseInterface |
54
|
|
|
*/ |
55
|
|
|
public function processResponse(AbstractMessage $message, ResponseInterface $response): FirebaseResponseInterface |
56
|
|
|
{ |
57
|
|
|
$this->message = $message; |
58
|
|
|
|
59
|
|
|
if (Response::HTTP_OK === $response->getStatusCode()) { |
60
|
|
|
$result = $this->processHttpOkResponse($response); |
61
|
|
|
} elseif (Response::HTTP_BAD_REQUEST === $response->getStatusCode()) { |
62
|
|
|
throw new InvalidJsonException(); |
63
|
|
|
} elseif (Response::HTTP_UNAUTHORIZED === $response->getStatusCode()) { |
64
|
|
|
throw new AuthenticationException(); |
65
|
|
|
} elseif (Response::HTTP_INTERNAL_SERVER_ERROR === $response->getStatusCode()) { |
66
|
|
|
throw new InternalServerErrorException(); |
67
|
|
|
} else { |
68
|
|
|
throw new UnsupportedResponseException(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $result; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param ResponseInterface $response |
76
|
|
|
* |
77
|
|
|
* @return FirebaseResponseInterface |
78
|
|
|
*/ |
79
|
|
|
private function processHttpOkResponse(ResponseInterface $response) |
80
|
|
|
{ |
81
|
|
|
$body = $this->getBodyAsArray($response); |
82
|
|
|
|
83
|
|
|
if (isset($body['error'])) { |
84
|
|
|
$response = $this->processHttpOkResponseWithError($body); |
|
|
|
|
85
|
|
|
} else { |
86
|
|
|
$response = $this->processHttpOkResponseWithoutError($body); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $response; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $body |
94
|
|
|
* |
95
|
|
|
* @return MulticastMessageResponse |
96
|
|
|
*/ |
97
|
|
|
private function processHttpOkResponseWithoutError(array $body): MulticastMessageResponse |
98
|
|
|
{ |
99
|
|
|
$successfulMessageResults = new SuccessfulMessageResultCollection(); |
100
|
|
|
$failedMessageResults = new FailedMessageResultCollection(); |
101
|
|
|
$canonicalTokenMessageResults = new CanonicalTokenMessageResultCollection(); |
102
|
|
|
|
103
|
|
|
if ($this->message->getTarget() instanceof TokenTargetInterface) { |
104
|
|
|
$numberOfSequentialSentTokens = $this->message->getTarget()->getNumberOfSequentialSentTokens(); |
105
|
|
|
|
106
|
|
|
if (isset($body['results']) && \count($body['results']) !== $numberOfSequentialSentTokens) { |
107
|
|
|
throw new \Exception('Mismatch number of sent tokens and results'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
for ($i = 0; $i < $numberOfSequentialSentTokens; ++$i) { |
111
|
|
|
$currentToken = $this->message->getTarget()->getSequentialSentTokens()[$i]; |
112
|
|
|
$currentResult = $body['results'][$i]; |
113
|
|
|
|
114
|
|
|
if (isset($currentResult['error'])) { |
115
|
|
|
$messageResult = (new FailedMessageResult()) |
116
|
|
|
->setError($currentResult['error']); |
117
|
|
|
|
118
|
|
|
$failedMessageResults[] = $messageResult; |
119
|
|
|
} elseif (isset($currentResult['registration_id'])) { |
120
|
|
|
$messageResult = (new CanonicalTokenMessageResult()) |
|
|
|
|
121
|
|
|
->setMessageId($currentResult['message_id']) |
122
|
|
|
->setCanonicalToken($currentResult['registration_id']); |
123
|
|
|
|
124
|
|
|
$canonicalTokenMessageResults[] = $messageResult; |
125
|
|
|
} else { |
126
|
|
|
$messageResult = (new SuccessfulMessageResult())->setMessageId($currentResult['message_id']); |
127
|
|
|
$successfulMessageResults[] = $messageResult; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$messageResult->setToken($currentToken); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return (new MulticastMessageResponse()) |
135
|
|
|
->setMulticastId($body['multicast_id']) |
136
|
|
|
->setSuccessfulMessageResults($successfulMessageResults) |
137
|
|
|
->setFailedMessageResults($failedMessageResults) |
138
|
|
|
->setCanonicalTokenMessageResults($canonicalTokenMessageResults); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param array $body |
143
|
|
|
*/ |
144
|
|
|
private function processHttpOkResponseWithError(array $body) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
// @todo finish it |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param ResponseInterface $response |
151
|
|
|
* |
152
|
|
|
* @throws \InvalidArgumentException |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
private function getBodyAsArray(ResponseInterface $response): array |
157
|
|
|
{ |
158
|
|
|
if ($this->responseContentTypeIsJson($response)) { |
159
|
|
|
$response->getBody()->rewind(); |
160
|
|
|
$body = null; |
161
|
|
|
|
162
|
|
|
if ($response->getBody()->getSize() > 0) { |
163
|
|
|
$body = $response->getBody()->getContents(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return \json_decode($body, true); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
throw new \InvalidArgumentException('Response from Firebase Cloud Messaging is not a JSON'); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @param ResponseInterface $response |
174
|
|
|
* |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
private function responseContentTypeIsJson(ResponseInterface $response): bool |
178
|
|
|
{ |
179
|
|
|
return $response->hasHeader('Content-Type') |
180
|
|
|
&& \in_array($response->getHeader('Content-Type')[0], $this->jsonContentTypes); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.