Completed
Push — master ( 0e7751...1494d2 )
by dan
02:09
created

DirectChannel   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 62
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setDispatchToEvent() 0 4 1
A setEventDispatcher() 0 4 1
A formatAndDispatch() 0 13 3
A format() 0 13 2
A dispatch() 0 18 4
1
<?php
2
3
namespace IrishDan\NotificationBundle\Channel;
4
5
use IrishDan\NotificationBundle\Event\MessageCreatedEvent;
6
use IrishDan\NotificationBundle\Event\MessageDispatchedEvent;
7
use IrishDan\NotificationBundle\Exception\MessageDispatchException;
8
use IrishDan\NotificationBundle\Exception\MessageFormatException;
9
use IrishDan\NotificationBundle\Message\MessageInterface;
10
use IrishDan\NotificationBundle\Notification\NotificationInterface;
11
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
12
13
14
/**
15
 * Class DirectChannel
16
 *
17
 * @package NotificationBundle\Channel
18
 */
19
class DirectChannel extends BaseChannel implements ChannelInterface
20
{
21
    protected $eventDispatcher;
22
    protected $dispatchToEvent = true;
23
24
    public function setDispatchToEvent($dispatchToEvent)
25
    {
26
        $this->dispatchToEvent = $dispatchToEvent;
27
    }
28
29
    public function setEventDispatcher(EventDispatcherInterface $eventDispatcher)
30
    {
31
        $this->eventDispatcher = $eventDispatcher;
32
    }
33
34
    public function formatAndDispatch(NotificationInterface $notification)
35
    {
36
        $message = $this->format($notification);
37
38
        if (!empty($this->eventDispatcher) && $this->dispatchToEvent) {
39
            $messageEvent = new MessageCreatedEvent($message);
40
            $this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent);
41
42
            return true;
43
        } else {
44
            return $this->dispatch($message);
45
        }
46
    }
47
48
    public function format(NotificationInterface $notification)
49
    {
50
        try {
51
            // Do the formatting.
52
            $message = $this->adapter->format($notification);
53
54
            return $message;
55
        } catch (\Exception $e) {
56
            throw new MessageFormatException(
57
                $e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine()
58
            );
59
        }
60
    }
61
62
    public function dispatch(MessageInterface $message)
63
    {
64
        // Dispatch the message
65
        try {
66
            $sent = $this->adapter->dispatch($message);
67
68
            if ($sent && !empty($this->eventDispatcher)) {
69
                $event = new MessageDispatchedEvent($message);
70
                $this->eventDispatcher->dispatch(MessageDispatchedEvent::NAME, $event);
71
            }
72
73
            return $sent;
74
        } catch (\Exception $e) {
75
            throw new MessageDispatchException(
76
                $e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine()
77
            );
78
        }
79
    }
80
}
81