Completed
Push — master ( 6ee4e9...166b13 )
by dan
02:00
created

DefaultChannel::setDispatcher()   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 1
1
<?php
2
3
namespace IrishDan\NotificationBundle\Channel;
4
5
use IrishDan\NotificationBundle\Dispatcher\MessageDispatcherInterface;
6
use IrishDan\NotificationBundle\Exception\MessageDispatchException;
7
use IrishDan\NotificationBundle\Exception\MessageFormatException;
8
use IrishDan\NotificationBundle\Formatter\MessageFormatterInterface;
9
use IrishDan\NotificationBundle\Message\MessageInterface;
10
use IrishDan\NotificationBundle\Notification\NotificationInterface;
11
12
13
/**
14
 * Class DefaultChannel
15
 *
16
 * @package NotificationBundle\Channel
17
 */
18
class DefaultChannel extends BaseChannel implements ChannelInterface
19
{
20
    public function formatAndDispatch(NotificationInterface $notification)
21
    {
22
        $message = $this->format($notification);
23
24
        return $this->dispatch($message);
25
    }
26
27
    public function format(NotificationInterface $notification)
28
    {
29
        try {
30
            // Do the formatting.
31
            $message = $this->formatter->format($notification);
32
33
            return $message;
34
        } catch (\Exception $e) {
35
            throw new MessageFormatException(
36
                $e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine()
37
            );
38
        }
39
    }
40
41
    public function dispatch(MessageInterface $message)
42
    {
43
        // Dispatch the message
44
        try {
45
            return $this->dispatcher->dispatch($message);
46
        } catch (\Exception $e) {
47
            throw new MessageDispatchException(
48
                $e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine()
49
            );
50
        }
51
    }
52
}
53