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
|
|
|
/** |
22
|
|
|
* @var |
23
|
|
|
*/ |
24
|
|
|
protected $eventDispatcher; |
25
|
|
|
protected $dispatchToEvent = true; |
26
|
|
|
|
27
|
|
|
public function setDispatchToEvent($dispatchToEvent) |
28
|
|
|
{ |
29
|
|
|
$this->dispatchToEvent = $dispatchToEvent; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
33
|
|
|
{ |
34
|
|
|
$this->eventDispatcher = $eventDispatcher; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function formatAndDispatch(NotificationInterface $notification) |
38
|
|
|
{ |
39
|
|
|
$message = $this->format($notification); |
40
|
|
|
|
41
|
|
|
if (!empty($this->eventDispatcher) && $this->dispatchToEvent) { |
42
|
|
|
$messageEvent = new MessageCreatedEvent($message); |
43
|
|
|
$this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent); |
44
|
|
|
|
45
|
|
|
return true; |
46
|
|
|
} else { |
47
|
|
|
return $this->dispatch($message); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function format(NotificationInterface $notification) |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
|
|
// Do the formatting. |
55
|
|
|
$message = $this->adapter->format($notification); |
56
|
|
|
|
57
|
|
|
return $message; |
58
|
|
|
} catch (\Exception $e) { |
59
|
|
|
throw new MessageFormatException( |
60
|
|
|
$e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine() |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function dispatch(MessageInterface $message) |
66
|
|
|
{ |
67
|
|
|
// Dispatch the message |
68
|
|
|
try { |
69
|
|
|
$sent = $this->adapter->dispatch($message); |
70
|
|
|
|
71
|
|
|
if ($sent && !empty($this->eventDispatcher)) { |
72
|
|
|
$event = new MessageDispatchedEvent($message); |
73
|
|
|
$this->eventDispatcher->dispatch(MessageDispatchedEvent::NAME, $event); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $sent; |
77
|
|
|
} catch (\Exception $e) { |
78
|
|
|
throw new MessageDispatchException( |
79
|
|
|
$e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine() |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|