ShowThreadController::getThreadForLoggedInUser()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
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\MessagingBundle\Controller;
12
13
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
14
use Miliooo\Messaging\ThreadProvider\SecureThreadProviderInterface;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\HttpFoundation\Response;
17
use Miliooo\Messaging\User\ParticipantProviderInterface;
18
use Miliooo\Messaging\Form\FormFactory\ReplyMessageFormFactory;
19
use Miliooo\Messaging\Form\FormHandler\NewReplyFormHandler;
20
use Miliooo\Messaging\Manager\ReadStatusManagerInterface;
21
use Symfony\Component\HttpFoundation\RedirectResponse;
22
use Symfony\Component\Routing\RouterInterface;
23
use Miliooo\Messaging\Model\MessageMetaInterface;
24
use Miliooo\Messaging\ValueObjects\ReadStatus;
25
use Miliooo\Messaging\User\ParticipantInterface;
26
use Miliooo\Messaging\Model\ThreadInterface;
27
use Symfony\Component\Form\FormInterface;
28
29
/**
30
 * Controller for showing a single thread.
31
 *
32
 * This controller is responsible for showing a single thread to the user.
33
 * It should only show the thread if the user has permissions to view that thread.
34
 *
35
 */
36
class ShowThreadController
37
{
38
    /**
39
     * A reply message form factory instance.
40
     *
41
     * @var ReplyMessageFormFactory
42
     */
43
    protected $formFactory;
44
45
    /**
46
     * A new reply form handler instance.
47
     *
48
     * @var NewReplyFormHandler
49
     */
50
    protected $formHandler;
51
52
    /**
53
     * A secure thread provider instance.
54
     *
55
     * @var SecureThreadProviderInterface
56
     */
57
    protected $threadProvider;
58
59
    /**
60
     * A templating instance.
61
     *
62
     * @var EngineInterface
63
     */
64
    protected $templating;
65
66
    /**
67
     * A participant provider instance.
68
     *
69
     * @var ParticipantProviderInterface
70
     */
71
    protected $participantProvider;
72
73
    /**
74
     * A read status manager instance.
75
     *
76
     * @var ReadStatusManagerInterface
77
     */
78
    protected $readStatusManager;
79
80
    /**
81
     * A router instance.
82
     *
83
     * @var RouterInterface
84
     */
85
    protected $router;
86
87
    /**
88
     * Constructor.
89
     *
90
     * @param ReplyMessageFormFactory       $formFactory         A reply form factory
91
     * @param NewReplyFormHandler           $formHandler         A reply form handler
92
     * @param SecureThreadProviderInterface $threadProvider      A secure thread provider instance
93
     * @param EngineInterface               $templating          A templating engine
94
     * @param ParticipantProviderInterface  $participantProvider A participant provider
95
     * @param ReadStatusManagerInterface    $readStatusManager   A read status manager instance
96
     * @param RouterInterface               $router              A router instance
97
     */
98
    public function __construct(
99
        ReplyMessageFormFactory $formFactory,
100
        NewReplyFormHandler $formHandler,
101
        SecureThreadProviderInterface $threadProvider,
102
        EngineInterface $templating,
103
        ParticipantProviderInterface $participantProvider,
104
        ReadStatusManagerInterface $readStatusManager,
105
        RouterInterface $router
106
    ) {
107
        $this->formFactory = $formFactory;
108
        $this->formHandler = $formHandler;
109
        $this->threadProvider = $threadProvider;
110
        $this->templating = $templating;
111
        $this->participantProvider = $participantProvider;
112
        $this->readStatusManager = $readStatusManager;
113
        $this->router = $router;
114
    }
115
116
    /**
117
     * Shows a single thread and allows the user to reply on it
118
     *
119
     * @param integer $threadId The unique thread id
120
     *
121
     * @throws NotFoundHttpException
122
     *
123
     * @return Response
124
     */
125
    public function showAction($threadId)
126
    {
127
        //get the logged in user
128
        $loggedInUser = $this->participantProvider->getAuthenticatedParticipant();
129
        //get the thread
130
        $thread = $this->getThreadForLoggedInUser($loggedInUser, $threadId);
131
        $form = $this->formFactory->create($thread, $loggedInUser);
132
133
        //check form submit..
134
        $response = $this->maybeProcessForm($form, $threadId);
135
        if ($response) {
136
            return $response;
137
        }
138
        //do some pre thread processing.
139
        $thread = $this->doThreadProcessing($loggedInUser, $thread);
140
141
        return $this->createResponse($thread, $form);
142
    }
143
144
    /**
145
     * Gets the thread for the logged in user.
146
     *
147
     * This function gets the thread for the logged in user and throws an exception when no thread is found.
148
     *
149
     * @param ParticipantInterface $loggedInUser The user for whom we lookup the thread
150
     * @param integer              $threadId     The unique thread id
151
     *
152
     * @return ThreadInterface The thread
153
     *
154
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
155
     */
156
    protected function getThreadForLoggedInUser(ParticipantInterface $loggedInUser, $threadId)
157
    {
158
        $thread = $this->threadProvider->findThreadForParticipant($loggedInUser, $threadId);
159
        if (!$thread) {
160
            throw new NotFoundHttpException('Thread not found');
161
        }
162
163
        return $thread;
164
    }
165
166
    /**
167
     * Checks if the form is valid returns a redirect response if valid or false if not submitted or not valid
168
     *
169
     * @param FormInterface $form     The form we process
170
     * @param integer       $threadId The unique thread id
171
     *
172
     * @return RedirectResponse|false
173
     */
174
    protected function maybeProcessForm(FormInterface $form, $threadId)
175
    {
176
        $submittedAndValidForm = $this->formHandler->process($form);
177
        if ($submittedAndValidForm) {
178
            $url = $this->router->generate('miliooo_message_thread_view', ['threadId' => $threadId]);
179
180
            return new RedirectResponse($url);
181
        }
182
183
        return false;
184
    }
185
186
    /**
187
     * doThreadProcessing.
188
     *
189
     * Here we do some thread actions before sending it to the view.
190
     *
191
     * We are marking the messages of the thread as read since we'll show those messages to the user.
192
     *
193
     * @param ParticipantInterface $loggedInUser
194
     * @param ThreadInterface      $thread
195
     *
196
     * @return threadInterface
0 ignored issues
show
Documentation introduced by
Should the return type not be ThreadInterface?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
197
     */
198
    protected function doThreadProcessing(ParticipantInterface $loggedInUser, ThreadInterface $thread)
199
    {
200
        $this->readStatusManager->updateReadStatusForMessageCollection(
201
            new ReadStatus(MessageMetaInterface::READ_STATUS_READ),
202
            $loggedInUser,
203
            $thread->getMessages()->toArray()
204
        );
205
206
        return $thread;
207
    }
208
209
    /**
210
     * Creates the response.
211
     *
212
     * @param ThreadInterface $thread
213
     * @param FormInterface   $form
214
     *
215
     * @return Response
216
     */
217
    protected function createResponse(ThreadInterface $thread, FormInterface $form)
218
    {
219
        return $this->templating->renderResponse(
220
            'MilioooMessagingBundle:ShowThread:show_thread.html.twig',
221
            [
222
                'thread' => $thread,
223
                'form' => $form->createView()
224
            ]
225
        );
226
    }
227
}
228