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 implements ChannelInterface |
19
|
|
|
{ |
20
|
|
|
private $formatter; |
21
|
|
|
private $dispatcher; |
22
|
|
|
|
23
|
|
|
public function setDispatcher(MessageDispatcherInterface $dispatcher) |
24
|
|
|
{ |
25
|
|
|
$this->dispatcher = $dispatcher; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function setDataFormatter(MessageFormatterInterface $formatter) |
29
|
|
|
{ |
30
|
|
|
$this->formatter = $formatter; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function __construct($configured = false, $channel = 'default') |
34
|
|
|
{ |
35
|
|
|
$this->configured = $configured; |
|
|
|
|
36
|
|
|
$this->channel = $channel; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function format(NotificationInterface $notification) |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
|
|
// Do the formatting. |
43
|
|
|
$message = $this->formatter->format($notification); |
44
|
|
|
var_dump($message); |
|
|
|
|
45
|
|
|
|
46
|
|
|
return $message; |
47
|
|
|
} catch (\Exception $e) { |
48
|
|
|
throw new MessageFormatException( |
49
|
|
|
$e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine() |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Dispatch the message |
54
|
|
|
// $this->dispatch($message); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function dispatch(MessageInterface $message) |
58
|
|
|
{ |
59
|
|
|
// Dispatch the message |
60
|
|
|
try { |
61
|
|
|
return $this->dispatcher->dispatch($message); |
62
|
|
|
} catch (\Exception $e) { |
63
|
|
|
throw new MessageDispatchException( |
64
|
|
|
$e->getMessage() . ' ' . $e->getCode() . ' ' . $e->getFile() . ' ' . $e->getLine() |
65
|
|
|
); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: