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\Event\NotificationReadyToFormatEvent; |
8
|
|
|
use IrishDan\NotificationBundle\Exception\MessageDispatchException; |
9
|
|
|
use IrishDan\NotificationBundle\Exception\MessageFormatException; |
10
|
|
|
use IrishDan\NotificationBundle\Message\MessageInterface; |
11
|
|
|
use IrishDan\NotificationBundle\Notification\NotificationInterface; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Channel |
17
|
|
|
* |
18
|
|
|
* @package NotificationBundle\Channel |
19
|
|
|
*/ |
20
|
|
|
class Channel extends BaseChannel implements ChannelInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var |
24
|
|
|
*/ |
25
|
|
|
protected $eventDispatcher; |
26
|
|
|
protected $formatToEvent = false; |
27
|
|
|
protected $dispatchToEvent = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param $dispatchToEvent |
31
|
|
|
*/ |
32
|
|
|
public function setDispatchAsEvent(bool $dispatchToEvent): void |
33
|
|
|
{ |
34
|
|
|
$this->dispatchToEvent = $dispatchToEvent; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param $dispatchToEvent |
39
|
|
|
*/ |
40
|
|
|
public function setFormatAsEvent(bool $formatToEvent): void |
41
|
|
|
{ |
42
|
|
|
$this->formatToEvent = $formatToEvent; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
47
|
|
|
*/ |
48
|
|
|
public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
49
|
|
|
{ |
50
|
|
|
$this->eventDispatcher = $eventDispatcher; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function formatAndDispatch(NotificationInterface $notification, $dispatchReadyEvent = true) |
54
|
|
|
{ |
55
|
|
|
// Channels can be configured to format and dispatch either directly.. |
56
|
|
|
// or via events.. |
57
|
|
|
// Using events allows for the hooking into the formatting and dispatching.. |
58
|
|
|
// process, perhaps t offload these process to a queue worker. |
59
|
|
|
if ($this->dispatchToEvent && $dispatchReadyEvent) { |
60
|
|
|
$readyEvent = new NotificationReadyToFormatEvent($notification); |
61
|
|
|
$this->eventDispatcher->dispatch(NotificationReadyToFormatEvent::NAME, $readyEvent); |
62
|
|
|
|
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Format the message.. |
67
|
|
|
// Creates a message object for each recipient |
68
|
|
|
// If twig enabled will use twig to render the message |
69
|
|
|
$message = $this->format($notification); |
70
|
|
|
|
71
|
|
|
// Dispatch the message.. |
72
|
|
|
// Sends the message to the destination.. |
73
|
|
|
$this->dispatch($message); |
74
|
|
|
|
75
|
|
|
$message->setStatus('sent'); |
76
|
|
|
|
77
|
|
|
return $message; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
public function format(NotificationInterface $notification) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
try { |
83
|
|
|
// Do the formatting. |
84
|
|
|
$message = $this->adapter->format($notification); |
85
|
|
|
|
86
|
|
|
if (!empty($this->eventDispatcher)) { |
87
|
|
|
$messageEvent = new MessageCreatedEvent($message); |
88
|
|
|
$this->eventDispatcher->dispatch(MessageCreatedEvent::NAME, $messageEvent); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $message; |
92
|
|
|
} catch (\Exception $e) { |
93
|
|
|
throw new MessageFormatException( |
94
|
|
|
$e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine() |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
View Code Duplication |
public function dispatch(MessageInterface $message) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
// Dispatch the message |
102
|
|
|
try { |
103
|
|
|
$sent = $this->adapter->dispatch($message); |
104
|
|
|
|
105
|
|
|
if ($sent && !empty($this->eventDispatcher)) { |
106
|
|
|
$messageEvent = new MessageDispatchedEvent($message); |
107
|
|
|
$this->eventDispatcher->dispatch(MessageDispatchedEvent::NAME, $messageEvent); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $sent; |
111
|
|
|
} catch (\Exception $e) { |
112
|
|
|
throw new MessageDispatchException( |
113
|
|
|
$e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine() |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.