Completed
Push — master ( 705569...50fbec )
by dan
02:16
created

EventChannel::setDispatchers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace IrishDan\NotificationBundle\Channel;
4
5
use IrishDan\NotificationBundle\Dispatcher\MessageDispatcherInterface;
6
use IrishDan\NotificationBundle\Event\MessageCreatedEvent;
7
use IrishDan\NotificationBundle\Exception\MessageDispatchException;
8
use IrishDan\NotificationBundle\Exception\MessageFormatException;
9
use IrishDan\NotificationBundle\Formatter\MessageFormatterInterface;
10
use IrishDan\NotificationBundle\Message\MessageInterface;
11
use IrishDan\NotificationBundle\Notification\NotificationInterface;
12
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
13
14
/**
15
 * Class DefaultChannel
16
 *
17
 * @package NotificationBundle\Channel
18
 */
19
class EventChannel extends BaseChannel implements ChannelInterface
20
{
21
    private $dispatchers = [];
22
    private $eventDispatcher;
23
24
    public function formatAndDispatch(NotificationInterface $notification)
25
    {
26
        $this->format($notification);
27
    }
28
29
    public function setDispatchers($key, MessageDispatcherInterface $dispatcher)
30
    {
31
        $this->dispatchers[$key] = $dispatcher;
32
    }
33
34
    public function __construct(EventDispatcherInterface $eventDispatcher)
35
    {
36
        // @TODO: Configured, for what??
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
37
        // $this->configured      = $configured;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
38
        // $this->channel         = $channel;
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
        $this->eventDispatcher = $eventDispatcher;
40
    }
41
42
    public function format(NotificationInterface $notification)
43
    {
44
        try {
45
            // Do the formatting.
46
            $message = $this->formatter->format($notification);
47
        } catch (\Exception $exception) {
48
            throw new MessageFormatException(
49
                $exception->getMessage()
50
            );
51
        }
52
53
        // Dispatch the message event
54
        $messageEvent = new MessageCreatedEvent($message);
55
        $this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent);
56
57
        return $message;
58
    }
59
60
    public function dispatchFromEvent(MessageCreatedEvent $event)
61
    {
62
        $message = $event->getMessage();
63
        $this->dispatch($message);
64
    }
65
66
    public function dispatch(MessageInterface $message)
67
    {
68
        // @TODO: need to figure out which channel message was supposed to be dispatched on.
69
        $dispatcherKey = $message->getChannel();
70
71
        // Dispatch the message
72
        try {
73
            if (!empty($this->dispatchers[$dispatcherKey])) {
74
                $this->dispatchers[$dispatcherKey]->dispatch($message);
75
            }
76
            else {
77
                throw new MessageDispatchException(
78
                    sprintf('No dispatcher available with key "%s"', $dispatcherKey)
79
                );
80
            }
81
        } catch (\Exception $exception) {
82
            throw new MessageDispatchException($exception->getMessage());
83
        }
84
85
        // Dispatch the message event
86
        $messageEvent = new MessageCreatedEvent($message);
87
        $this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent);
88
    }
89
}
90