|
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\Client; |
|
14
|
|
|
|
|
15
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Event\FirebaseEvents; |
|
16
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Event\MulticastMessageResponseEvent; |
|
17
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Message\Builder\MessageBuilder; |
|
18
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Message\Type\AbstractMessage; |
|
19
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Response\FirebaseResponseInterface; |
|
20
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Response\MulticastMessageResponse; |
|
21
|
|
|
use Fresh\FirebaseCloudMessagingBundle\Response\ResponseProcessor; |
|
22
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* FirebaseClient. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Artem Henvald <[email protected]> |
|
28
|
|
|
*/ |
|
29
|
|
|
class FirebaseClient |
|
30
|
|
|
{ |
|
31
|
|
|
private HttpClient $httpClient; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
private MessageBuilder $messageBuilder; |
|
34
|
|
|
|
|
35
|
|
|
private ResponseProcessor $responseProcessor; |
|
36
|
|
|
|
|
37
|
|
|
private EventDispatcherInterface $eventDispatcher; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param HttpClient $httpClient |
|
41
|
|
|
* @param MessageBuilder $messageBuilder |
|
42
|
|
|
* @param ResponseProcessor $responseProcessor |
|
43
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(HttpClient $httpClient, MessageBuilder $messageBuilder, ResponseProcessor $responseProcessor, EventDispatcherInterface $eventDispatcher) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->messageBuilder = $messageBuilder; |
|
48
|
|
|
$this->responseProcessor = $responseProcessor; |
|
49
|
|
|
$this->httpClient = $httpClient; |
|
50
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param AbstractMessage $message |
|
55
|
|
|
* |
|
56
|
|
|
* @return FirebaseResponseInterface |
|
57
|
|
|
*/ |
|
58
|
|
|
public function sendMessage(AbstractMessage $message): FirebaseResponseInterface |
|
59
|
|
|
{ |
|
60
|
|
|
$this->messageBuilder->setMessage($message); |
|
61
|
|
|
|
|
62
|
|
|
$response = $this->httpClient->post( |
|
63
|
|
|
'', |
|
64
|
|
|
['body' => $this->messageBuilder->getMessageAsJson()] |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$processedResponse = $this->responseProcessor->processResponse($message, $response); |
|
68
|
|
|
$this->dispatchEvent($processedResponse); |
|
69
|
|
|
|
|
70
|
|
|
return $processedResponse; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param FirebaseResponseInterface $response |
|
75
|
|
|
*/ |
|
76
|
|
|
private function dispatchEvent(FirebaseResponseInterface $response): void |
|
77
|
|
|
{ |
|
78
|
|
|
if ($response instanceof MulticastMessageResponse) { |
|
79
|
|
|
$this->eventDispatcher->dispatch( |
|
80
|
|
|
FirebaseEvents::MULTICAST_MESSAGE_RESPONSE_EVENT, |
|
81
|
|
|
new MulticastMessageResponseEvent( |
|
82
|
|
|
$response->getMulticastId(), |
|
83
|
|
|
$response->getSuccessfulMessageResults(), |
|
84
|
|
|
$response->getFailedMessageResults(), |
|
85
|
|
|
$response->getCanonicalTokenMessageResults() |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|