|
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
|
|
|
|