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() |
|
23 | { |
||
24 | 1 | $this->requireLogin(); |
|
25 | 1 | } |
|
26 | |||
27 | 1 | protected function prepareTwig() |
|
28 | { |
||
29 | 1 | $currentPage = $this->getRequest()->query->get('page', 1); |
|
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) |
|
54 | { |
||
55 | 1 | if (!$me->hasPermission(Permission::SEND_PRIVATE_MSG)) { |
|
56 | 1 | throw new ForbiddenException("You are not allowed to send messages"); |
|
57 | } |
||
58 | |||
59 | 1 | $creator = new ConversationFormCreator($me); |
|
60 | 1 | $form = $creator->create()->handleRequest($request); |
|
61 | |||
62 | 1 | if ($form->isSubmitted()) { |
|
63 | 1 | if ($form->isValid()) { |
|
64 | 1 | $subject = $form->get('Subject')->getData(); |
|
65 | 1 | $content = $form->get('Message')->getData(); |
|
66 | 1 | $recipients = $form->get('Recipients')->getData(); |
|
67 | |||
68 | 1 | $conversation_to = Conversation::createConversation($subject, $me->getId(), $recipients); |
|
69 | 1 | $message = $conversation_to->sendMessage($me, $content); |
|
70 | |||
71 | 1 | $event = new NewMessageEvent($message, true); |
|
72 | 1 | $this->dispatch(Events::MESSAGE_NEW, $event); |
|
73 | |||
74 | 1 | if ($this->isJson()) { |
|
75 | return new JsonResponse(array( |
||
76 | 'success' => true, |
||
77 | 'message' => 'Your message was sent successfully', |
||
78 | 'id' => $conversation_to->getId() |
||
79 | )); |
||
80 | } else { |
||
81 | 1 | return new RedirectResponse($conversation_to->getUrl()); |
|
82 | } |
||
83 | 1 | } elseif ($this->isJson()) { |
|
84 | 1 | throw new BadRequestException($this->getErrorMessage($form)); |
|
85 | } |
||
86 | } else { |
||
87 | // Load the list of recipients from the URL |
||
88 | 1 | if ($request->query->has('recipients')) { |
|
89 | $form->get('Recipients')->setData($this->decompose( |
||
90 | $request->query->get('recipients'), |
||
91 | array('Player', 'Team') |
||
92 | )); |
||
93 | } |
||
94 | } |
||
95 | |||
96 | 1 | return array("form" => $form->createView()); |
|
97 | } |
||
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($request->query->get('page', 1)) |
|
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) |
||
206 | |||
207 | public function teamLeaveAction(Player $me, Conversation $conversation) |
||
208 | { |
||
209 | $team = $me->getTeam(); |
||
210 | |||
211 | if (!$me->canEdit($team)) { |
||
212 | throw new ForbiddenException("You are not allowed to remove your team from this conversation."); |
||
227 | |||
228 | public function kickAction(Conversation $conversation, Player $me, $type, $member) |
||
258 | |||
259 | 1 | public function searchAction(Player $me, Request $request) |
|
275 | |||
276 | /** |
||
277 | * @param Conversation $conversation |
||
278 | * @param Player $me |
||
279 | * |
||
280 | * @return $this|Form|\Symfony\Component\Form\FormInterface |
||
281 | */ |
||
282 | 1 | private function showInviteForm($conversation, $me) |
|
311 | |||
312 | /** |
||
313 | * @param Conversation $conversation |
||
314 | * @param Player $me |
||
315 | */ |
||
316 | 1 | private function showRenameForm($conversation, $me) |
|
335 | |||
336 | /** |
||
337 | * @param Conversation $conversation |
||
338 | * @param Player $me |
||
339 | */ |
||
340 | 1 | private function showMessageForm($conversation, $me) |
|
360 | |||
361 | /** |
||
362 | * Make sure that a player can participate in a conversation |
||
363 | * |
||
364 | * Throws an exception if a player is not an admin or a member of that conversation |
||
365 | * @todo Permission for spying on other people's conversations? |
||
366 | * @param Player $player The player to test |
||
367 | * @param Conversation $conversation The message conversation |
||
368 | * @param string $message The error message to show |
||
369 | * @throws HTTPException |
||
370 | * @return void |
||
371 | */ |
||
372 | 1 | private function assertCanParticipate(Player $player, Conversation $conversation, |
|
379 | |||
380 | /** |
||
381 | * Sends a message to a conversation |
||
382 | * |
||
383 | * @param Player $from The sender |
||
384 | * @param Conversation $to The conversation that will receive the message |
||
385 | * @param Form $form The message's form |
||
386 | * @param Form $form The form before it handled the request |
||
387 | * @param Form $cloned |
||
388 | * @throws HTTPException Thrown if the user doesn't have the |
||
389 | * SEND_PRIVATE_MSG permission |
||
390 | * @return void |
||
391 | */ |
||
392 | 1 | private function sendMessage(Player $from, Conversation $to, &$form, $cloned) |
|
413 | |||
414 | /** |
||
415 | * @return string|null |
||
416 | */ |
||
417 | private function getErrorMessage(Form $form) |
||
431 | |||
432 | /** |
||
433 | * Make sure that a player can edit a conversation |
||
434 | * |
||
435 | * Throws an exception if a player is not an admin or the leader of a team |
||
436 | * @param Player $player The player to test |
||
437 | * @param Conversation $conversation The team |
||
438 | * @param string $message The error message to show |
||
439 | * @throws HTTPException |
||
440 | * @return void |
||
441 | */ |
||
442 | private function assertCanEdit(Player $player, Conversation $conversation, $message = "You are not allowed to edit the discussion") |
||
448 | } |
||
449 |
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: