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