Completed
Push — master ( 611a4d...0d4705 )
by Artem
11:18 queued 04:23
created

FirebaseClient::dispatchEvent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
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;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
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