1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Notifications; |
4
|
|
|
|
5
|
|
|
use ByTIC\Notifications\Channels\AbstractChannel; |
6
|
|
|
use ByTIC\Notifications\Channels\EmailDbChannel; |
7
|
|
|
use ByTIC\Notifications\Dispatcher\Dispatcher; |
8
|
|
|
use Nip\Collection; |
9
|
|
|
use Nip\Utility\Traits\SingletonTrait; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ChannelManager |
13
|
|
|
* @package ByTIC\Notifications |
14
|
|
|
*/ |
15
|
|
|
class ChannelManager |
16
|
|
|
{ |
17
|
|
|
use SingletonTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The array of created "drivers". |
21
|
|
|
* |
22
|
|
|
* @var AbstractChannel[] |
23
|
|
|
*/ |
24
|
|
|
protected $channels = null; |
25
|
|
|
|
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
$this->channels = new Collection(); |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Collection $notifiables |
33
|
|
|
* @param Notification $notification |
34
|
|
|
*/ |
35
|
|
|
public function send($notifiables, $notification) |
36
|
|
|
{ |
37
|
|
|
return (new Dispatcher($this))->send($notifiables, $notification); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get a driver instance. |
42
|
|
|
* |
43
|
|
|
* @param string $channel |
44
|
|
|
* |
45
|
|
|
* @return AbstractChannel |
46
|
|
|
*/ |
47
|
|
|
public function channel($channel = null) |
48
|
|
|
{ |
49
|
|
|
// If the given driver has not been created before, we will create the instances |
50
|
|
|
// here and cache it so we can return it next time very quickly. If there is |
51
|
|
|
// already a driver created by this name, we'll just return that instance. |
52
|
|
|
if (!$this->hasChannel($channel)) { |
53
|
|
|
$this->addChannel($channel, $this->createChannel($channel)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->getChannel($channel); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $channel |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
public function hasChannel($channel) |
64
|
|
|
{ |
65
|
|
|
return $this->channels->has($channel); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $name |
70
|
|
|
* @param AbstractChannel $driver |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
protected function addChannel($name, AbstractChannel $driver) |
74
|
|
|
{ |
75
|
|
|
$this->channels->set($name, $driver); |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $channel |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
protected function getChannel($channel) |
84
|
|
|
{ |
85
|
|
|
return $this->channels->get($channel); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create a new driver instance. |
90
|
|
|
* |
91
|
|
|
* @param string $channel |
92
|
|
|
* |
93
|
|
|
* @return AbstractChannel |
94
|
|
|
*/ |
95
|
|
|
protected function createChannel($channel) |
96
|
|
|
{ |
97
|
|
|
$method = 'create' . ucfirst($channel) . 'Channel'; |
98
|
|
|
|
99
|
|
|
return $this->$method(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Create an instance of the mail driver. |
104
|
|
|
* |
105
|
|
|
* @return EmailDbChannel |
106
|
|
|
*/ |
107
|
|
|
protected function createEmailDbChannel() |
108
|
|
|
{ |
109
|
|
|
return new EmailDbChannel(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..