Completed
Push — master ( 72cf95...b4fc03 )
by Artem
08:38
created

FirebaseClient::sendMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
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
    /** @var HttpClient */
32
    private $httpClient;
33
34
    /** @var MessageBuilder */
35
    private $messageBuilder;
36
37
    /** @var ResponseProcessor */
38
    private $responseProcessor;
39
40
    /** @var EventDispatcherInterface */
41
    private $eventDispatcher;
42
43
    /**
44
     * @param HttpClient               $httpClient
45
     * @param MessageBuilder           $messageBuilder
46
     * @param ResponseProcessor        $responseProcessor
47
     * @param EventDispatcherInterface $eventDispatcher
48
     */
49
    public function __construct(HttpClient $httpClient, MessageBuilder $messageBuilder, ResponseProcessor $responseProcessor, EventDispatcherInterface $eventDispatcher)
50
    {
51
        $this->messageBuilder = $messageBuilder;
52
        $this->responseProcessor = $responseProcessor;
53
        $this->httpClient = $httpClient;
54
        $this->eventDispatcher = $eventDispatcher;
55
    }
56
57
    /**
58
     * @param AbstractMessage $message
59
     *
60
     * @return FirebaseResponseInterface
61
     */
62
    public function sendMessage(AbstractMessage $message): FirebaseResponseInterface
63
    {
64
        $this->messageBuilder->setMessage($message);
65
66
        $response = $this->httpClient->post(
67
            '',
68
            ['body' => $this->messageBuilder->getMessageAsJson()]
69
        );
70
71
        $processedResponse = $this->responseProcessor->processResponse($message, $response);
72
        $this->dispatchEvent($processedResponse);
73
74
        return $processedResponse;
75
    }
76
77
    /**
78
     * @param FirebaseResponseInterface $response
79
     */
80
    private function dispatchEvent(FirebaseResponseInterface $response): void
81
    {
82
        if ($response instanceof MulticastMessageResponse) {
83
            $this->eventDispatcher->dispatch(
84
                FirebaseEvents::MULTICAST_MESSAGE_RESPONSE_EVENT,
85
                new MulticastMessageResponseEvent(
86
                    $response->getMulticastId(),
87
                    $response->getSuccessfulMessageResults(),
0 ignored issues
show
Bug introduced by
It seems like $response->getSuccessfulMessageResults() targeting Fresh\FirebaseCloudMessa...cessfulMessageResults() can also be of type array<integer,object<Fre...ccessfulMessageResult>>; however, Fresh\FirebaseCloudMessa...nseEvent::__construct() does only seem to accept object<Fresh\FirebaseClo...essageResultCollection>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
88
                    $response->getFailedMessageResults(),
0 ignored issues
show
Bug introduced by
It seems like $response->getFailedMessageResults() targeting Fresh\FirebaseCloudMessa...tFailedMessageResults() can also be of type array<integer,object<Fre...t\FailedMessageResult>>; however, Fresh\FirebaseCloudMessa...nseEvent::__construct() does only seem to accept object<Fresh\FirebaseClo...essageResultCollection>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
89
                    $response->getCanonicalTokenMessageResults()
0 ignored issues
show
Bug introduced by
It seems like $response->getCanonicalTokenMessageResults() targeting Fresh\FirebaseCloudMessa...alTokenMessageResults() can also be of type array<integer,object<Fre...calTokenMessageResult>>; however, Fresh\FirebaseCloudMessa...nseEvent::__construct() does only seem to accept object<Fresh\FirebaseClo...essageResultCollection>, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
90
                )
91
            );
92
        }
93
    }
94
}
95