1 | <?php |
||
18 | final class Communicator |
||
19 | { |
||
20 | /** |
||
21 | * The channels over which messages can be broadcasted. |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | private $channels; |
||
26 | |||
27 | /** |
||
28 | * A list with event listeners that will be called when a message is broadcasted. |
||
29 | * |
||
30 | * @var array |
||
31 | */ |
||
32 | private $listeners; |
||
33 | |||
34 | /** |
||
35 | * Initializes a new instance of this class. |
||
36 | */ |
||
37 | public function __construct() |
||
44 | |||
45 | /** |
||
46 | * Attaches a listener to the communicator. |
||
47 | * |
||
48 | * @param callable $listener The listener to attach. |
||
49 | * @param int $priority The priority to add the listener at. |
||
50 | */ |
||
51 | public function addListener(callable $listener, int $priority = 0): void |
||
55 | |||
56 | /** |
||
57 | * Detaches a listener from the communicator. |
||
58 | * |
||
59 | * @param callable $listener The listener to attach. |
||
60 | */ |
||
61 | public function removeListener(callable $listener): void |
||
71 | |||
72 | /** |
||
73 | * Binds a transport to a communication channel. |
||
74 | * |
||
75 | * @param string $channel The channel to bind to. |
||
76 | * @param TransportInterface $transport The transport to bind. |
||
77 | * @return void |
||
78 | */ |
||
79 | public function bindTransport($channel, TransportInterface $transport): void |
||
87 | |||
88 | /** |
||
89 | * Unbinds a transport from a communication channel. |
||
90 | * |
||
91 | * @param string $channel The channel to unbind from. |
||
92 | * @param TransportInterface $transport The transport to unbind. |
||
93 | * @return void |
||
94 | * @throws InvalidArgumentException |
||
95 | */ |
||
96 | public function unbindTransport($channel, TransportInterface $transport): void |
||
116 | |||
117 | /** |
||
118 | * Broadcasts a message to the given channel. |
||
119 | * |
||
120 | * @param array $recipients A list with recipients that the message will be broadcasted to. |
||
121 | * @param string $channel The name of the channel to broadcast the message to. |
||
122 | * @param array $params The parameters of the message. |
||
123 | * @param array $options The options that should be provided to the transport. |
||
124 | * @return void |
||
125 | */ |
||
126 | public function broadcast(array $recipients, $channel, array $params, array $options = []): void |
||
132 | |||
133 | /** |
||
134 | * Sends the given message to all bound transports. |
||
135 | * |
||
136 | * @param array $recipients A list with recipients that the message will be broadcasted to. |
||
137 | * @param Message $message The message to broadcast. |
||
138 | * @return void |
||
139 | */ |
||
140 | public function broadcastMessage(array $recipients, Message $message): void |
||
153 | |||
154 | /** |
||
155 | * Called when the messages can be send to the list of transports. |
||
156 | * |
||
157 | * @param array $recipients A list with recipients that the message will be broadcasted to. |
||
158 | * @param Message $message The message to broadcast. |
||
159 | * @return void |
||
160 | * @internal Will be called internally once all listeners have been executed in order. |
||
161 | */ |
||
162 | public function onSend(array $recipients, Message $message): void |
||
175 | } |
||
176 |