forked from FriendsOfSymfony/FOSMessageBundle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThread.php
153 lines (133 loc) · 3.57 KB
/
Thread.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace FOS\MessageBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use FOS\MessageBundle\Model\MessageInterface;
use FOS\MessageBundle\Model\Thread as BaseThread;
use FOS\MessageBundle\Model\ParticipantInterface;
use FOS\MessageBundle\Model\ThreadMetadata as ModelThreadMetadata;
abstract class Thread extends BaseThread
{
/**
* Messages contained in this thread
*
* @var Collection of MessageInterface
*/
protected $messages;
/**
* Users participating in this conversation
*
* @var Collection of ParticipantInterface
*/
protected $participants;
/**
* Thread metadata
*
* @var Collection of ThreadMetadata
*/
protected $metadata;
/**
* All text contained in the thread messages
* Used for the full text search
*
* @var string
*/
protected $keywords = '';
/**
* Participant that created the thread
*
* @var ParticipantInterface
*/
protected $createdBy;
/**
* Date this thread was created at
*
* @var DateTime
*/
protected $createdAt;
/**
* Gets the users participating in this conversation
*
* @return array of ParticipantInterface
*/
public function getParticipants()
{
return $this->getParticipantsCollection()->toArray();
}
/**
* Gets the users participating in this conversation
*
* Since the ORM schema does not map the participants collection field, it
* must be created on demand.
*
* @return ArrayCollection
*/
protected function getParticipantsCollection()
{
if ($this->participants == null) {
$this->participants = new ArrayCollection();
foreach ($this->metadata as $data) {
$this->participants->add($data->getParticipant());
}
}
return $this->participants;
}
/**
* Adds a participant to the thread
* If it already exists, nothing is done.
*
* @param ParticipantInterface $participant
* @return null
*/
public function addParticipant(ParticipantInterface $participant)
{
if (!$this->isParticipant($participant)) {
$this->getParticipantsCollection()->add($participant);
}
}
/**
* Adds many participants to the thread
*
* @param array|\Traversable
*
* @throws \InvalidArgumentException
* @return Thread
*/
public function addParticipants($participants)
{
if (!is_array($participants) && !$participants instanceof \Traversable) {
throw new \InvalidArgumentException("Participants must be an array or instanceof Traversable");
}
foreach ($participants as $participant) {
$this->addParticipant($participant);
}
return $this;
}
/**
* Tells if the user participates to the conversation
*
* @param ParticipantInterface $participant
* @return boolean
*/
public function isParticipant(ParticipantInterface $participant)
{
return $this->getParticipantsCollection()->contains($participant);
}
/**
* Get the collection of ThreadMetadata.
*
* @return Collection
*/
public function getAllMetadata()
{
return $this->metadata;
}
/**
* @see FOS\MessageBundle\Model\Thread::addMetadata()
*/
public function addMetadata(ModelThreadMetadata $meta)
{
$meta->setThread($this);
parent::addMetadata($meta);
}
}