Completed
Push — master ( 6895de...8b3e50 )
by Artem
02:07
created

SymfonyEventDispatcher   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 56
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A dispatch() 0 4 1
B getEventData() 0 25 3
1
<?php
2
/*
3
 * This file is part of the FirebaseCloudMessagingBundle
4
 *
5
 * (c) Artem Genvald <[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
namespace Fresh\FirebaseCloudMessagingBundle\Event;
12
13
use Fresh\FirebaseCloudMessaging\Event\EventInterface;
14
use Fresh\FirebaseCloudMessaging\Event\FirebaseEvents;
15
use Fresh\FirebaseCloudMessaging\EventDispatcherInterface as FirebaseCloudMessagingEventDispatcherInterface;
16
use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;
17
18
/**
19
 * Class SymfonyEventDispatcher.
20
 */
21
class SymfonyEventDispatcher implements FirebaseCloudMessagingEventDispatcherInterface
22
{
23
    /** @var SymfonyEventDispatcherInterface */
24
    private $dispatcher;
25
26
    /**
27
     * @param SymfonyEventDispatcherInterface $dispatcher
28
     */
29
    public function __construct(SymfonyEventDispatcherInterface $dispatcher)
30
    {
31
        $this->dispatcher = $dispatcher;
32
    }
33
34
    /**
35
     * @param string         $eventName
36
     * @param EventInterface $eventData
37
     */
38
    public function dispatch($eventName, EventInterface $eventData)
39
    {
40
        $this->dispatcher->dispatch($eventName, $this->getEventData($eventName, $eventData));
41
    }
42
43
    /**
44
     * @param string         $eventName
45
     * @param EventInterface $eventData
46
     *
47
     * @throws \Exception
48
     *
49
     * @return MulticastMessageResponseEvent|TopicMessageResponseEvent
50
     */
51
    private function getEventData($eventName, EventInterface $eventData)
52
    {
53
        switch ($eventName) {
54
            case FirebaseEvents::MULTICAST_MESSAGE_RESPONSE_EVENT:
55
                /** @var \Fresh\FirebaseCloudMessaging\Event\MulticastMessageResponseEvent $eventData */
56
                $event = new MulticastMessageResponseEvent(
57
                    $eventData->getMulticastId(),
58
                    $eventData->getSuccessfulMessageResults(),
0 ignored issues
show
Bug introduced by
It seems like $eventData->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...
59
                    $eventData->getFailedMessageResults(),
0 ignored issues
show
Bug introduced by
It seems like $eventData->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...
60
                    $eventData->getCanonicalTokenMessageResults()
0 ignored issues
show
Bug introduced by
It seems like $eventData->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...
61
                );
62
                break;
63
            case FirebaseEvents::TOPIC_MESSAGE_RESPONSE_EVENT:
64
                /** @var \Fresh\FirebaseCloudMessaging\Event\TopicMessageResponseEvent $eventData */
65
                $event = new TopicMessageResponseEvent(
66
                    $eventData->getMessageId(),
67
                    $eventData->getError()
68
                );
69
                break;
70
            default:
71
                throw new \Exception('Unsupported event type');
72
        }
73
74
        return $event;
75
    }
76
}
77