updateThreadStatusForParticipant()   B
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 14
nc 2
nop 3
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 Miliooo\Messaging\Model\ThreadInterface;
15
use Miliooo\Messaging\Repository\ThreadRepositoryInterface;
16
use Miliooo\Messaging\ValueObjects\ThreadStatus;
17
use Miliooo\Messaging\Repository\MessageRepositoryInterface;
18
use Miliooo\Messaging\Model\ThreadMetaInterface;
19
use Miliooo\Messaging\Model\MessageMetaInterface;
20
use Miliooo\Messaging\ValueObjects\ReadStatus;
21
22
/**
23
 * ThreadStatusManager.
24
 *
25
 * The thread status manager is responsible for updating the thread status and persisting the updated thread to
26
 * the storage engine.
27
 *
28
 * @author Michiel Boeckaert <[email protected]>
29
 */
30
class ThreadStatusManager implements ThreadStatusManagerInterface
31
{
32
    /**
33
     * A thread repository instance.
34
     *
35
     * @var ThreadRepositoryInterface
36
     */
37
    private $threadRepository;
38
39
    /**
40
     * A message repository instance.
41
     *
42
     * @var MessageRepositoryInterface
43
     */
44
    private $messageRepository;
45
46
    /**
47
     * A read status manager instance.
48
     *
49
     * @var ReadStatusManagerInterface
50
     */
51
    private $readStatusManager;
52
53
    /**
54
     * @param ThreadRepositoryInterface  $threadRepository  A thread repository
55
     * @param MessageRepositoryInterface $messageRepository A message repository
56
     * @param ReadStatusManagerInterface $readStatusManager A read status manager
57
     */
58
    public function __construct(
59
        ThreadRepositoryInterface $threadRepository,
60
        MessageRepositoryInterface $messageRepository,
61
        ReadStatusManagerInterface $readStatusManager
62
    ) {
63
        $this->threadRepository = $threadRepository;
64
        $this->messageRepository = $messageRepository;
65
        $this->readStatusManager = $readStatusManager;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function updateThreadStatusForParticipant(
72
        ThreadStatus $newThreadStatus,
73
        ThreadInterface $thread,
74
        ParticipantInterface $participant
75
    ) {
76
        $threadMeta = $thread->getThreadMetaForParticipant($participant);
77
78
        //get the integer value of the thread status.
79
        $newThreadStatusInt = $newThreadStatus->getThreadStatus();
80
        $oldThreadStatusInt = $threadMeta->getStatus();
81
82
83
        //if no thread meta can happen if the current user is not participant of the thread
84
        if (!$threadMeta || $oldThreadStatusInt === $newThreadStatusInt) {
85
            return false;
86
        }
87
88
        //updates the status
89
        $threadMeta->setStatus($newThreadStatusInt);
90
91
        //saves the thread
92
        $this->threadRepository->save($thread);
93
94
        //create a value object from the old status integer
95
        $oldThreadStatus = new ThreadStatus($oldThreadStatusInt);
96
        $this->maybeMarkMessagesAsRead($thread, $participant, $oldThreadStatus, $newThreadStatus);
97
98
        return true;
99
    }
100
101
    /**
102
     * When archiving an active thread we want to mark all unread messages as read.
103
     *
104
     * Since this is a thread status update we do this here. We do use the read status manager for this should also
105
     * dispatch a new read status event.
106
     *
107
     * @param ThreadInterface      $thread          The current thread
108
     * @param ParticipantInterface $participant     The participant who wants to update the thread status
109
     * @param ThreadStatus         $oldThreadStatus The integer value of the old thread status
110
     * @param ThreadStatus         $newThreadStatus The integer value of the new thread status
111
     *
112
     * @return boolean false if no messages where marked as read, true if there were messages marked as read
113
     */
114
    protected function maybeMarkMessagesAsRead(
115
        ThreadInterface $thread,
116
        ParticipantInterface $participant,
117
        ThreadStatus $oldThreadStatus,
118
        ThreadStatus $newThreadStatus
119
    ) {
120
        if (!$this->updateActiveThreadToArchivedThread($oldThreadStatus, $newThreadStatus)) {
121
            return false;
122
        }
123
124
        //get the messages...
125
        $messages = $this->messageRepository->getUnreadMessagesFromThreadForParticipant($participant, $thread);
126
        // return if not array (should never happen) or if empty messages()
127
        if (!is_array($messages) || empty($messages)) {
128
            return false;
129
        }
130
131
        $readStatus = new ReadStatus(MessageMetaInterface::READ_STATUS_READ);
132
        $this->readStatusManager->updateReadStatusForMessageCollection($readStatus, $participant, $messages);
133
134
        return true;
135
    }
136
137
    /**
138
     * Checks whether we are updating the thread status from an active to an archived thread
139
     *
140
     * @param ThreadStatus $oldThreadStatus
141
     * @param ThreadStatus $newThreadStatus
142
     *
143
     * @return boolean true if we are updating an active thread to an archived thread false otherwise
144
     */
145
    protected function updateActiveThreadToArchivedThread(ThreadStatus $oldThreadStatus, ThreadStatus $newThreadStatus)
0 ignored issues
show
Coding Style introduced by
function updateActiveThreadToArchivedThread() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

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...
146
    {
147
        if ($oldThreadStatus->getThreadStatus() !== ThreadMetaInterface::STATUS_ACTIVE
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($oldThreadStatu...face::STATUS_ARCHIVED);.
Loading history...
148
            ||
149
            $newThreadStatus->getThreadStatus() !== ThreadMetaInterface::STATUS_ARCHIVED
150
        ) {
151
            return false;
152
        }
153
154
        return true;
155
    }
156
}
157