|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the MilioooMessageBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Michiel boeckaert <[email protected]> |
|
7
|
|
|
* This source file is subject to the MIT license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Miliooo\Messaging\Manager; |
|
12
|
|
|
|
|
13
|
|
|
use Miliooo\Messaging\User\ParticipantInterface; |
|
14
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
15
|
|
|
use Miliooo\Messaging\Event\MilioooMessagingEvents; |
|
16
|
|
|
use Miliooo\Messaging\Model\MessageInterface; |
|
17
|
|
|
use Miliooo\Messaging\Event\ReadStatusMessageEvent; |
|
18
|
|
|
use Miliooo\Messaging\ValueObjects\ReadStatus; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Read status manager who dispatches read status updates events. |
|
22
|
|
|
* |
|
23
|
|
|
* This class uses the decorator pattern. |
|
24
|
|
|
* The read status manager returns an array with updated messages. |
|
25
|
|
|
* This class uses this array to dispatch events for these updated messages. |
|
26
|
|
|
* Since it implements the same interface it also has to return this array. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Michiel Boeckaert <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class ReadStatusManagerEventAware implements ReadStatusManagerInterface |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* A read status manager instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @var ReadStatusManagerInterface |
|
36
|
|
|
*/ |
|
37
|
|
|
private $readStatusManager; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* An event dispatcher instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @var EventDispatcherInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
private $eventDispatcher; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param ReadStatusManagerInterface $readStatusManager |
|
50
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct( |
|
53
|
|
|
ReadStatusManagerInterface $readStatusManager, |
|
54
|
|
|
EventDispatcherInterface $eventDispatcher |
|
55
|
|
|
) { |
|
56
|
|
|
$this->readStatusManager = $readStatusManager; |
|
57
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
public function updateReadStatusForMessageCollection( |
|
|
|
|
|
|
64
|
|
|
ReadStatus $updatedReadStatus, |
|
65
|
|
|
ParticipantInterface $participant, |
|
66
|
|
|
$messages = [] |
|
67
|
|
|
) { |
|
68
|
|
|
|
|
69
|
|
|
$updatedMessages = $this->readStatusManager->updateReadStatusForMessageCollection( |
|
70
|
|
|
$updatedReadStatus, |
|
71
|
|
|
$participant, |
|
72
|
|
|
$messages |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
$this->maybeDispatchMessages($updatedMessages, $participant); |
|
76
|
|
|
|
|
77
|
|
|
return $updatedMessages; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Looping over the updated messages (if any) and dispatching them. |
|
82
|
|
|
* |
|
83
|
|
|
* @param MessageInterface[]|[] $updatedMessages An array with message interfaces or an empty array if none updated |
|
|
|
|
|
|
84
|
|
|
* @param ParticipantInterface $participant The participant for whom we updated the messages |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function maybeDispatchMessages($updatedMessages, $participant) |
|
87
|
|
|
{ |
|
88
|
|
|
foreach ($updatedMessages as $message) { |
|
89
|
|
|
$this->dispatchMessage($message, $participant); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Dispatch a read status changed event. |
|
95
|
|
|
* |
|
96
|
|
|
* @param MessageInterface $message The message whom read status is changed for the participant |
|
97
|
|
|
* @param ParticipantInterface $participant The participant for whom the read status is changed. |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function dispatchMessage(MessageInterface $message, ParticipantInterface $participant) |
|
100
|
|
|
{ |
|
101
|
|
|
$event = new ReadStatusMessageEvent($message, $participant); |
|
102
|
|
|
$this->eventDispatcher->dispatch(MilioooMessagingEvents::READ_STATUS_CHANGED, $event); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.