Complex classes like MessageController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MessageController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class MessageController extends JSONController |
||
21 | { |
||
22 | 1 | public function setup() |
|
26 | |||
27 | 1 | protected function prepareTwig() |
|
28 | { |
||
29 | 1 | $currentPage = $this->getCurrentPage(); |
|
30 | |||
31 | 1 | $query = $this->getQueryBuilder('Conversation') |
|
|
|||
32 | 1 | ->forPlayer($this->getMe()) |
|
33 | 1 | ->sortBy('last_activity')->reverse() |
|
34 | 1 | ->limit(5)->fromPage($currentPage); |
|
35 | |||
36 | 1 | $creator = new MessageSearchFormCreator(); |
|
37 | 1 | $searchForm = $creator->create(); |
|
38 | |||
39 | 1 | $twig = $this->container->get('twig'); |
|
40 | 1 | $twig->addGlobal("conversations", $query->getModels()); |
|
41 | 1 | $twig->addGlobal("currentPage", $currentPage); |
|
42 | 1 | $twig->addGlobal("totalPages", $query->countPages()); |
|
43 | 1 | $twig->addGlobal("searchForm", $searchForm->createView()); |
|
44 | |||
45 | 1 | return $twig; |
|
46 | } |
||
47 | |||
48 | public function listAction() |
||
49 | { |
||
50 | return array(); |
||
51 | } |
||
52 | |||
53 | 1 | public function composeAction(Player $me, Request $request) |
|
98 | |||
99 | 1 | public function showAction(Conversation $conversation, Player $me, Request $request) |
|
100 | { |
||
101 | 1 | $this->assertCanParticipate($me, $conversation); |
|
102 | 1 | $conversation->markReadBy($me->getId()); |
|
103 | |||
104 | 1 | $form = $this->showMessageForm($conversation, $me); |
|
105 | 1 | $inviteForm = $this->showInviteForm($conversation, $me); |
|
106 | 1 | $renameForm = $this->showRenameForm($conversation, $me); |
|
107 | |||
108 | 1 | $messages = $this->getQueryBuilder('AbstractMessage') |
|
109 | 1 | ->where('conversation')->is($conversation) |
|
110 | 1 | ->sortBy('time')->reverse() |
|
111 | 1 | ->limit(10)->fromPage($this->getCurrentPage()) |
|
112 | 1 | ->startAt($request->query->get('end')) |
|
113 | 1 | ->endAt($request->query->get('start')) |
|
114 | 1 | ->getModels(); |
|
115 | |||
116 | // Hide the details (author, timestamp) of the first message if they're |
||
117 | // already shown in the previous message (useful for AJAX calls) |
||
118 | 1 | if ($request->query->getBoolean('hideFirstDetails')) { |
|
119 | $previousMessage = Message::get($request->query->get('end')); |
||
120 | } else { |
||
121 | 1 | $previousMessage = null; |
|
122 | } |
||
123 | |||
124 | $params = array( |
||
125 | 1 | "form" => $form->createView(), |
|
126 | 1 | "inviteForm" => $inviteForm->createView(), |
|
127 | 1 | "renameForm" => $renameForm->createView(), |
|
128 | 1 | "conversation" => $conversation, |
|
129 | 1 | "messages" => $messages, |
|
130 | 1 | "previousMessage" => $previousMessage |
|
131 | ); |
||
132 | |||
133 | 1 | if ($request->query->has('nolayout')) { |
|
134 | if ($request->query->getBoolean('reviewLastDetails')) { |
||
135 | // An AJAX call has asked us to check if details (author, |
||
136 | // timestamp) will need to be shown for the next message |
||
137 | |||
138 | $nextMessage = Message::get($request->query->get('start')); |
||
139 | $lastMessage = reset($messages); |
||
140 | |||
141 | if ($lastMessage !== false |
||
142 | && $lastMessage->isMessage() |
||
143 | && $nextMessage->isMessage() |
||
144 | && $lastMessage->getTimestamp()->isSameDay($nextMessage->getTimestamp()) |
||
145 | && $lastMessage->getAuthor()->isSameAs($nextMessage->getAuthor()) |
||
146 | ) { |
||
147 | $hideLastDetails = true; |
||
148 | } else { |
||
149 | $hideLastDetails = false; |
||
150 | } |
||
151 | |||
152 | // Add $hideLastDetails to the list of JSON parameters |
||
153 | $this->attributes->set('hideLastDetails', $hideLastDetails); |
||
154 | } |
||
155 | |||
156 | // Don't show the layout so that ajax can just load the messages |
||
157 | return $this->render('Message/messages.html.twig', $params); |
||
158 | } else { |
||
159 | 1 | return $params; |
|
160 | } |
||
161 | } |
||
162 | |||
163 | public function leaveAction(Player $me, Conversation $conversation) |
||
164 | { |
||
165 | if (!$conversation->isMember($me, $distinct = true)) { |
||
166 | throw new ForbiddenException("You are not a member of this discussion."); |
||
167 | } elseif ($conversation->getCreator()->isSameAs($me)) { |
||
168 | throw new ForbiddenException("You can't abandon the conversation you started!"); |
||
169 | } |
||
170 | |||
171 | return $this->showConfirmationForm(function () use ($conversation, $me) { |
||
172 | $conversation->removeMember($me); |
||
173 | |||
174 | $event = new ConversationAbandonEvent($conversation, $me); |
||
175 | Service::getDispatcher()->dispatch(Events::CONVERSATION_ABANDON, $event); |
||
176 | |||
177 | return new RedirectResponse(Service::getGenerator()->generate('message_list')); |
||
178 | }, "Are you sure you want to abandon this conversation?", |
||
179 | "You will no longer receive messages from this conversation", "Leave"); |
||
180 | } |
||
181 | |||
182 | public function teamRefreshAction(Player $me, Conversation $conversation) |
||
183 | { |
||
184 | $team = $me->getTeam(); |
||
185 | if (!$team->isValid()) { |
||
186 | throw new ForbiddenException("You are not a member of a team."); |
||
187 | } |
||
188 | |||
189 | $missing = $conversation->getMissingTeamMembers($team); |
||
190 | if (empty($missing)) { |
||
191 | throw new ForbiddenException("All members of the specified team are already part of that conversation."); |
||
192 | } |
||
193 | |||
194 | $names = array(); |
||
195 | foreach ($missing as $player) { |
||
196 | $names[] = $player->getEscapedUsername(); |
||
197 | } |
||
198 | $names = implode(', ', $names); |
||
199 | |||
200 | return $this->showConfirmationForm(function () use ($conversation, $missing, $team) { |
||
201 | $conversation->addMember($team); // does all the hard work |
||
202 | |||
203 | $event = new ConversationJoinEvent($conversation, $missing); |
||
204 | Service::getDispatcher()->dispatch(Events::CONVERSATION_JOIN, $event); |
||
205 | |||
206 | return new RedirectResponse($conversation->getURL()); |
||
207 | }, "Are you sure you want to invite $names from {$team->getEscapedName()} to that conversation?"); |
||
208 | } |
||
209 | |||
210 | public function teamLeaveAction(Player $me, Conversation $conversation) |
||
211 | { |
||
212 | $team = $me->getTeam(); |
||
213 | |||
214 | if (!$me->canEdit($team)) { |
||
215 | throw new ForbiddenException("You are not allowed to remove your team from this conversation."); |
||
216 | } elseif (!$conversation->isMember($team)) { |
||
217 | throw new ForbiddenException("That team is not participating in this conversation."); |
||
218 | } |
||
219 | |||
220 | return $this->showConfirmationForm(function () use ($conversation, $team) { |
||
221 | $conversation->removeMember($team); |
||
222 | |||
223 | $event = new ConversationAbandonEvent($conversation, $team); |
||
224 | Service::getDispatcher()->dispatch(Events::CONVERSATION_ABANDON, $event); |
||
225 | |||
226 | return new RedirectResponse($conversation->getURL()); |
||
227 | }, "Are you sure you want to remove {$team->getEscapedName()} from this conversation?", |
||
228 | "Your team is no longer participating in that conversation.", "Remove team"); |
||
229 | } |
||
230 | |||
231 | public function kickAction(Conversation $conversation, Player $me, $type, $member) |
||
232 | { |
||
233 | $this->assertCanEdit($me, $conversation, "You are not allowed to kick a member off that discussion!"); |
||
234 | |||
235 | if (strtolower($type) === 'player') { |
||
236 | $member = Player::fetchFromSlug($member); |
||
237 | } elseif (strtolower($type) === 'team') { |
||
238 | $member = Team::fetchFromSlug($member); |
||
239 | } else { |
||
240 | throw new BadRequestException("Unrecognized member type."); |
||
241 | } |
||
242 | |||
243 | if ($conversation->isCreator($member->getId())) { |
||
244 | throw new ForbiddenException("You can't leave your own conversation."); |
||
245 | } |
||
246 | |||
247 | if (!$conversation->isMember($member)) { |
||
248 | throw new ForbiddenException("The specified player is not a member of this conversation."); |
||
249 | } |
||
250 | |||
251 | return $this->showConfirmationForm(function () use ($conversation, $member, $me) { |
||
252 | $conversation->removeMember($member); |
||
253 | |||
254 | $event = new ConversationKickEvent($conversation, $member, $me); |
||
255 | Service::getDispatcher()->dispatch(Events::CONVERSATION_KICK, $event); |
||
256 | |||
257 | return new RedirectResponse($conversation->getUrl()); |
||
258 | }, "Are you sure you want to kick {$member->getEscapedName()} from the discussion?", |
||
259 | "{$member->getName()} has been kicked from the conversation", "Kick"); |
||
260 | } |
||
261 | |||
262 | 1 | public function searchAction(Player $me, Request $request) |
|
263 | { |
||
264 | 1 | $query = $request->query->get('q'); |
|
265 | |||
266 | 1 | if (strlen($query) < 3 && !$this->isDebug()) { |
|
267 | // TODO: Find a better error message |
||
268 | throw new BadRequestException('The search term you have provided is too short'); |
||
269 | } |
||
270 | |||
271 | 1 | $search = new MessageSearch($this->getQueryBuilder(), $me); |
|
272 | 1 | $results = $search->search($query); |
|
273 | |||
274 | return array( |
||
275 | 1 | 'messages' => $results |
|
276 | ); |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @param Conversation $conversation |
||
281 | * @param Player $me |
||
282 | * |
||
283 | * @return $this|Form|\Symfony\Component\Form\FormInterface |
||
284 | */ |
||
285 | 1 | private function showInviteForm($conversation, $me) |
|
286 | { |
||
287 | 1 | $creator = new ConversationInviteFormCreator($conversation); |
|
288 | 1 | $form = $creator->create()->handleRequest($this->getRequest()); |
|
289 | |||
290 | 1 | if ($form->isValid()) { |
|
291 | $this->assertCanEdit($me, $conversation); |
||
292 | $invitees = array(); |
||
293 | |||
294 | foreach ($form->get('players')->getData() as $player) { |
||
295 | if (!$conversation->isMember($player, $distinct = true)) { |
||
296 | $conversation->addMember($player); |
||
297 | $invitees[] = $player; |
||
298 | } |
||
299 | } |
||
300 | |||
301 | if (!empty($invitees)) { |
||
302 | $event = new ConversationJoinEvent($conversation, $invitees); |
||
303 | Service::getDispatcher()->dispatch(Events::CONVERSATION_JOIN, $event); |
||
304 | } |
||
305 | |||
306 | $this->getFlashBag()->add('success', "The conversation has been updated"); |
||
307 | |||
308 | // Reset the form fields |
||
309 | return $creator->create(); |
||
310 | } |
||
311 | |||
312 | 1 | return $form; |
|
313 | } |
||
314 | |||
315 | /** |
||
316 | * @param Conversation $conversation |
||
317 | * @param Player $me |
||
318 | */ |
||
319 | 1 | private function showRenameForm($conversation, $me) |
|
338 | |||
339 | /** |
||
340 | * @param Conversation $conversation |
||
341 | * @param Player $me |
||
342 | */ |
||
343 | 1 | private function showMessageForm($conversation, $me) |
|
363 | |||
364 | /** |
||
365 | * Make sure that a player can participate in a conversation |
||
366 | * |
||
367 | * Throws an exception if a player is not an admin or a member of that conversation |
||
368 | * @todo Permission for spying on other people's conversations? |
||
369 | * @param Player $player The player to test |
||
370 | * @param Conversation $conversation The message conversation |
||
371 | * @param string $message The error message to show |
||
372 | * @throws HTTPException |
||
373 | * @return void |
||
374 | */ |
||
375 | 1 | private function assertCanParticipate(Player $player, Conversation $conversation, |
|
382 | |||
383 | /** |
||
384 | * Sends a message to a conversation |
||
385 | * |
||
386 | * @param Player $from The sender |
||
387 | * @param Conversation $to The conversation that will receive the message |
||
388 | * @param Form $form The message's form |
||
389 | * @param Form $form The form before it handled the request |
||
390 | * @param Form $cloned |
||
391 | * @throws HTTPException Thrown if the user doesn't have the |
||
392 | * SEND_PRIVATE_MSG permission |
||
393 | * @return void |
||
394 | */ |
||
395 | 1 | private function sendMessage(Player $from, Conversation $to, &$form, $cloned) |
|
396 | { |
||
397 | 1 | if (!$from->hasPermission(Permission::SEND_PRIVATE_MSG)) { |
|
398 | 1 | throw new ForbiddenException("You are not allowed to send messages"); |
|
399 | } |
||
400 | |||
401 | 1 | $message = $form->get('message')->getData(); |
|
402 | 1 | $message = $to->sendMessage($from, $message); |
|
403 | |||
404 | 1 | $this->getFlashBag()->add('success', "Your message was sent successfully"); |
|
405 | |||
406 | // Let javascript know the message's ID |
||
407 | 1 | $this->attributes->set('id', $message->getId()); |
|
408 | |||
409 | // Reset the form |
||
410 | 1 | $form = $cloned; |
|
411 | |||
412 | // Notify everyone that we sent a new message |
||
413 | 1 | $event = new NewMessageEvent($message, false); |
|
414 | 1 | $this->dispatch(Events::MESSAGE_NEW, $event); |
|
415 | 1 | } |
|
416 | |||
417 | /** |
||
418 | * Get an error message from a form |
||
419 | * |
||
420 | * @param Form $form The form to check |
||
421 | * |
||
422 | * @return string Will return "Unknown error" if an error is not found |
||
423 | */ |
||
424 | private function getErrorMessage(Form $form) |
||
425 | { |
||
426 | foreach ($form->all() as $child) { |
||
427 | foreach ($child->getErrors() as $error) { |
||
428 | return $error->getMessage(); |
||
429 | } |
||
430 | } |
||
431 | |||
432 | foreach ($form->getErrors() as $error) { |
||
433 | return $error->getMessage(); |
||
434 | } |
||
435 | |||
436 | return "Unknown Error"; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Make sure that a player can edit a conversation |
||
441 | * |
||
442 | * Throws an exception if a player is not an admin or the leader of a team |
||
443 | * @param Player $player The player to test |
||
444 | * @param Conversation $conversation The team |
||
445 | * @param string $message The error message to show |
||
446 | * @throws HTTPException |
||
447 | * @return void |
||
448 | */ |
||
449 | private function assertCanEdit(Player $player, Conversation $conversation, $message = "You are not allowed to edit the discussion") |
||
455 | } |
||
456 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: