Thread::setCreatedBy()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\Model;
12
13
use Miliooo\Messaging\User\ParticipantInterface;
14
use Doctrine\Common\Collections\ArrayCollection;
15
16
/**
17
 * The thread model
18
 *
19
 * @author Michiel Boeckaert <[email protected]>
20
 */
21
abstract class Thread implements ThreadInterface
0 ignored issues
show
Coding Style introduced by
Thread does not seem to conform to the naming convention (^Abstract|Factory$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
22
{
23
    /**
24
     * The unique id of the thread
25
     *
26
     * @var integer
27
     */
28
    protected $id;
29
30
    /**
31
     * The subject of the thread
32
     *
33
     * @var string
34
     */
35
    protected $subject;
36
37
    /**
38
     * The participant who created the thread
39
     *
40
     * @var ParticipantInterface
41
     */
42
    protected $createdBy;
43
44
    /**
45
     * The datetime when the thread was created
46
     *
47
     * @var \DateTime
48
     */
49
    protected $createdAt;
50
51
    /**
52
     * An array collection of messages for this thread
53
     *
54
     * @var ArrayCollection|MessageInterface[]
55
     */
56
    protected $messages;
57
58
    /**
59
     * An array collection of thread metas for this thread
60
     *
61
     * @var ArrayCollection|ThreadMeta[]
62
     */
63
    protected $threadMeta;
64
65
    /**
66
     * An array collection with participants
67
     *
68
     * @var ArrayCollection
69
     */
70
    protected $participants;
71
72
    /**
73
     * The last message posted in this thread.
74
     *
75
     * @var MessageInterface
76
     */
77
    protected $lastMessage;
78
79
    /**
80
     * Constructor.
81
     */
82
    public function __construct()
83
    {
84
        $this->messages = new ArrayCollection();
85
        $this->threadMeta = new ArrayCollection();
86
        $this->participants = new ArrayCollection();
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function getId()
93
    {
94
        return $this->id;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function setSubject($subject)
101
    {
102
        $this->subject = $subject;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108
    public function getSubject()
109
    {
110
        return $this->subject;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function setCreatedBy(ParticipantInterface $participant)
117
    {
118
        $this->createdBy = $participant;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     */
124
    public function getCreatedBy()
125
    {
126
        return $this->createdBy;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    public function setCreatedAt(\DateTime $createdAt)
133
    {
134
        $this->createdAt = $createdAt;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function getCreatedAt()
141
    {
142
        return $this->createdAt;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function addMessage(MessageInterface $message)
149
    {
150
        $message->setThread($this);
151
        $this->messages->add($message);
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function getMessages()
158
    {
159
        return $this->messages;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->messages; of type Doctrine\Common\Collecti...odel\MessageInterface[] adds the type Miliooo\Messaging\Model\MessageInterface[] to the return on line 159 which is incompatible with the return type declared by the interface Miliooo\Messaging\Model\...dInterface::getMessages of type Doctrine\Common\Collections\ArrayCollection.
Loading history...
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getFirstMessage()
166
    {
167
        return $this->messages->first();
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function getLastMessage()
174
    {
175
        return $this->lastMessage;
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function setLastMessage(MessageInterface $message)
182
    {
183
        $this->lastMessage = $message;
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function getThreadMeta()
190
    {
191
        return $this->threadMeta;
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->threadMeta; of type Doctrine\Common\Collecti...ging\Model\ThreadMeta[] adds the type Miliooo\Messaging\Model\ThreadMeta[] to the return on line 191 which is incompatible with the return type declared by the interface Miliooo\Messaging\Model\...nterface::getThreadMeta of type Doctrine\Common\Collections\ArrayCollection.
Loading history...
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function addThreadMeta(ThreadMetaInterface $threadMeta)
198
    {
199
        $threadMeta->setThread($this);
200
        $this->threadMeta->add($threadMeta);
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206
    public function getThreadMetaForParticipant(ParticipantInterface $participant)
207
    {
208
        foreach ($this->threadMeta as $meta) {
209
            if ($meta->getParticipant()->getParticipantId() == $participant->getParticipantId()) {
210
                return $meta;
211
            }
212
        }
213
214
        return null;
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function getParticipants()
221
    {
222
        return $this->getParticipantsCollection()->toArray();
223
    }
224
225
     /**
226
     * {@inheritdoc}
227
     */
228
    public function isParticipant(ParticipantInterface $participant)
229
    {
230
        return $this->getParticipantsCollection()->contains($participant);
231
    }
232
233
    /**
234
     * Returns an array collection of participants for the given thread.
235
     *
236
     * We do not map the participantscollection because the thread meta allready
237
     * gives us all the information needed. It contains the thread AND the participant
238
     *
239
     * So in order to get the participants we need to loop over the threadmeta
240
     * Get the participant and add it to the collection.
241
     *
242
     * if we want to add a participant to the thread collection we need to create that threadmeta.
243
     *
244
     * @return ArrayCollection
245
     */
246
    protected function getParticipantsCollection()
247
    {
248
        //doctrine skips constructor and does not make an array collection
249
        //since there is no mapping taking place
250
        if ($this->participants == null) {
251
            $this->participants = new ArrayCollection();
252
        }
253
254
        // no point in looping over the threadmeta since there is none
255
        if ($this->threadMeta->count() === 0) {
256
            return $this->participants;
257
        }
258
259
        //there is thread meta in the collection so let's loop over it
260
        foreach ($this->threadMeta as $threadMeta) {
261
            $this->addParticipantFromThreadMeta($threadMeta);
262
        }
263
264
        return $this->participants;
265
    }
266
267
    /**
268
     * {@inheritdoc}
269
     */
270
    public function getOtherParticipants(ParticipantInterface $participant)
271
    {
272
        $otherParticipants = $this->getParticipants();
273
        $key = array_search($participant, $otherParticipants, true);
274
275
        if (false !== $key) {
276
            unset($otherParticipants[$key]);
277
        }
278
279
        return array_values($otherParticipants);
280
    }
281
282
    /**
283
     * Adds a participant form the thread meta
284
     *
285
     * @param ThreadMetaInterface $threadMeta The threadmeta we extract the participant from
286
     *
287
     * @throws \InvalidArgumentException if participant is not instance of participantinterface
288
     */
289
    protected function addParticipantFromThreadMeta(ThreadMetaInterface $threadMeta)
290
    {
291
        $participant = $threadMeta->getParticipant();
292
293
        //this should not happen unless you did not delete the messages from deleted users
294
        //If there is no longer a link between those there could be other problems though...
295
        // Let's throw an exception here for the moment...
296
        if (!is_object($participant) || !$participant instanceof ParticipantInterface) {
297
            throw new \InvalidArgumentException('ThreadMeta contains participant with no participantinterface');
298
        }
299
300
        if (!$this->participants->contains($participant)) {
301
            $this->participants->add($participant);
302
        }
303
    }
304
}
305