Complex classes like SendMessagesAbstract 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 SendMessagesAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | abstract class SendMessagesAbstract |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * Instance of issue that this message belong to. |
||
| 33 | * |
||
| 34 | * @var Issue |
||
| 35 | */ |
||
| 36 | protected $issue; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Instance of project that this message belong to. |
||
| 40 | * |
||
| 41 | * @var Issue |
||
| 42 | */ |
||
| 43 | protected $project; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The latest message queued. |
||
| 47 | * |
||
| 48 | * @var Message\Queue |
||
| 49 | */ |
||
| 50 | protected $latestMessage; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Collection of all of the queued messages. |
||
| 54 | * |
||
| 55 | * @var Collection |
||
| 56 | */ |
||
| 57 | protected $allMessages; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Instance of a queued message that is for adding a record (ie. adding issue). |
||
| 61 | * |
||
| 62 | * @var Message\Queue |
||
| 63 | */ |
||
| 64 | protected $addMessage; |
||
| 65 | /** |
||
| 66 | * Collection of users that must not receive messages. |
||
| 67 | * |
||
| 68 | * @var Collection |
||
| 69 | */ |
||
| 70 | protected $excludeUsers; |
||
| 71 | /** |
||
| 72 | * Collection of all of the project users that should receive messages. |
||
| 73 | * |
||
| 74 | * @var Collection |
||
| 75 | */ |
||
| 76 | protected $projectUsers; |
||
| 77 | /** |
||
| 78 | * Collection of full subscribers that will always receive messages. |
||
| 79 | * |
||
| 80 | * @var Collection |
||
| 81 | */ |
||
| 82 | protected $fullSubscribers; |
||
| 83 | /** |
||
| 84 | * Name of message template. |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | protected $template; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var Mailer |
||
| 92 | */ |
||
| 93 | protected $mailer; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Collection of all messages. |
||
| 97 | * |
||
| 98 | * @var Collection |
||
| 99 | */ |
||
| 100 | protected $messages; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set instance of Mailer. |
||
| 104 | * |
||
| 105 | * @param Mailer $mailer |
||
| 106 | * |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | 4 | public function setMailer(Mailer $mailer) |
|
| 110 | { |
||
| 111 | 4 | $this->mailer = $mailer; |
|
| 112 | |||
| 113 | 4 | return $this; |
|
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * The main method to process the massages queue and send them. |
||
| 118 | * |
||
| 119 | * @param Message\Queue $latestMessage |
||
| 120 | * @param Collection $changes |
||
| 121 | * |
||
| 122 | * @return void |
||
| 123 | */ |
||
| 124 | 4 | public function process(Message\Queue $latestMessage, Collection $changes) |
|
| 125 | { |
||
| 126 | 4 | $this->setup($latestMessage, $changes); |
|
| 127 | |||
| 128 | // Is model deleted? |
||
| 129 | 4 | if (!$this->getModel()) { |
|
| 130 | 1 | return;// $this->sendMessageToAll($this->latestMessage); |
|
| 131 | } |
||
| 132 | |||
| 133 | 4 | if (!$this->validateData()) { |
|
| 134 | return; |
||
| 135 | } |
||
| 136 | |||
| 137 | 4 | $this->processDirectMessages(); |
|
| 138 | |||
| 139 | // Skip if no users found |
||
| 140 | 4 | if ($this->getProjectUsers()->isEmpty()) { |
|
| 141 | return; |
||
| 142 | } |
||
| 143 | |||
| 144 | 4 | $this->populateData(); |
|
| 145 | |||
| 146 | // Send the latest message if it is about status (ie. closed issue) |
||
| 147 | 4 | if ($this->isStatusMessage()) { |
|
| 148 | 2 | return $this->sendMessageToAll($this->latestMessage); |
|
| 149 | } |
||
| 150 | |||
| 151 | // Get message data for all of the messages combined & for add message queue if exists |
||
| 152 | 4 | $addMessageData = []; |
|
| 153 | 4 | if ($this->addMessage) { |
|
| 154 | 4 | $addMessageData = $this->getMessageData($this->addMessage); |
|
| 155 | } |
||
| 156 | 4 | $everythingMessageData = $this->getCombineMessageData($this->allMessages); |
|
| 157 | |||
| 158 | // Send messages to project users |
||
| 159 | 4 | $this->sendMessages($this->getProjectUsers(), [ |
|
| 160 | 4 | 'addMessage' => $addMessageData, |
|
| 161 | 4 | 'everything' => $everythingMessageData, |
|
| 162 | ]); |
||
| 163 | 4 | } |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Setup properties needed for the process. |
||
| 167 | * |
||
| 168 | * @param Message\Queue $latestMessage |
||
| 169 | * @param Collection $allMessages |
||
| 170 | * |
||
| 171 | * @return void |
||
| 172 | */ |
||
| 173 | 4 | protected function setup(Message\Queue $latestMessage, Collection $allMessages) |
|
| 174 | { |
||
| 175 | // Set queue messages |
||
| 176 | 4 | $this->latestMessage = $latestMessage; |
|
| 177 | 4 | $this->allMessages = $allMessages; |
|
| 178 | |||
| 179 | // Exclude the user who made the change from receiving messages |
||
| 180 | 4 | $this->addToExcludeUsers($this->latestMessage->changeBy); |
|
|
|
|||
| 181 | |||
| 182 | // Extract add model message if exists |
||
| 183 | 4 | if ($this->getModel()) { |
|
| 184 | 4 | $addMessageIdentifier = Message\Queue::getAddEventNameFromModel($this->getModel()); |
|
| 185 | 4 | $this->addMessage = $this->allMessages->where('event', $addMessageIdentifier)->first(); |
|
| 186 | } |
||
| 187 | |||
| 188 | // Make sure to load issue |
||
| 189 | 4 | $this->getIssue(); |
|
| 190 | 4 | } |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Whether or not we have all the needed properties. |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | abstract protected function validateData(); |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Process any messages queue that is to send messages to specific users. |
||
| 201 | * For example, assign issue to user to message the user about the issue. |
||
| 202 | * |
||
| 203 | * @return void |
||
| 204 | */ |
||
| 205 | 2 | protected function processDirectMessages() |
|
| 206 | { |
||
| 207 | 2 | } |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Populate any data or properties. |
||
| 211 | * |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | 1 | protected function populateData() |
|
| 215 | { |
||
| 216 | 1 | } |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Whether or not the latest message is about status change such as closed issue. |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | abstract public function isStatusMessage(); |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Returns the message subject. |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | 3 | protected function getSubject() |
|
| 231 | { |
||
| 232 | 3 | return '#' . $this->issue->id . ' / ' . $this->issue->title; |
|
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Returns an array of data needed for the message. |
||
| 237 | * |
||
| 238 | * @param Message\Queue $queue |
||
| 239 | * @param array $extraData |
||
| 240 | * |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | 4 | protected function getMessageData(Message\Queue $queue, array $extraData = []) |
|
| 244 | { |
||
| 245 | // Generic info for all messages emails |
||
| 246 | 4 | $messageData = []; |
|
| 247 | 4 | $messageData['issue'] = $this->getIssue(); |
|
| 248 | 4 | $messageData['project'] = $this->getProject(); |
|
| 249 | 4 | $messageData['changes'] = []; |
|
| 250 | 4 | $messageData['changes']['change_by'] = [ |
|
| 251 | 4 | 'now' => $queue->changeBy->fullname, |
|
| 252 | ]; |
||
| 253 | 4 | if ($this->getIssue()) { |
|
| 254 | 3 | $messageData['changes']['change_by']['url'] = $this->getIssue()->to(); |
|
| 255 | } else { |
||
| 256 | 1 | $messageData['changes']['change_by']['url'] = $this->getProject()->to(); |
|
| 257 | } |
||
| 258 | 4 | $messageData['changeByImage'] = $queue->changeBy->image; |
|
| 259 | 4 | $messageData['changeByHeading'] = $this->getMessageHeading($queue); |
|
| 260 | 4 | $messageData['event'] = $queue->event; |
|
| 261 | |||
| 262 | // Info specific to a message type |
||
| 263 | 4 | $method = 'getMessageDataFor' . ucfirst(camel_case($queue->event)); |
|
| 264 | 4 | if (method_exists($this, $method)) { |
|
| 265 | 4 | $messageData = array_replace_recursive($messageData, $this->{$method}($queue, $extraData)); |
|
| 266 | } |
||
| 267 | |||
| 268 | 4 | return $messageData; |
|
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Loop through all of the messages and combine its message data. |
||
| 273 | * |
||
| 274 | * @param Collection $changes |
||
| 275 | * |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | 4 | protected function getCombineMessageData(Collection $changes) |
|
| 279 | { |
||
| 280 | 4 | $everything = []; |
|
| 281 | $changes->reverse()->each(function (Message\Queue $queue) use (&$everything) { |
||
| 282 | 4 | if (!$everything) { |
|
| 283 | 4 | $everything = $this->getMessageData($queue); |
|
| 284 | } else { |
||
| 285 | 4 | $messageData = $this->getMessageData($queue); |
|
| 286 | 4 | $everything['changes'] = array_merge($everything['changes'], $messageData['changes']); |
|
| 287 | } |
||
| 288 | 4 | }); |
|
| 289 | 4 | $latestMessage = $changes->first(); |
|
| 290 | 4 | $everything['changeByHeading'] = $this->getMessageHeading($latestMessage, $changes); |
|
| 291 | 4 | $everything['event'] = $latestMessage->event; |
|
| 292 | 4 | $messageData = $this->getMessageData($latestMessage); |
|
| 293 | 4 | $everything = array_replace_recursive($everything, $messageData); |
|
| 294 | |||
| 295 | 4 | return $everything; |
|
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Return text to be used for the message heading. |
||
| 300 | * |
||
| 301 | * @param Message\Queue $queue |
||
| 302 | * @param Collection|null $changes |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | 4 | protected function getMessageHeading(Message\Queue $queue, Collection $changes = null) |
|
| 307 | { |
||
| 308 | 4 | $heading = $queue->changeBy->fullname . ' '; |
|
| 309 | |||
| 310 | // If other users have made changes too |
||
| 311 | 4 | if (!is_null($changes) && $changes->unique('change_by_id')->count() > 1) { |
|
| 312 | 1 | $heading .= '& others '; |
|
| 313 | } |
||
| 314 | |||
| 315 | 4 | return $heading; |
|
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns collection of all users in a project that should receive the messages. |
||
| 320 | * |
||
| 321 | * @return Collection |
||
| 322 | */ |
||
| 323 | 4 | protected function getProjectUsers() |
|
| 324 | { |
||
| 325 | 4 | if (null === $this->projectUsers) { |
|
| 326 | 4 | $this->projectUsers = (new Project\User()) |
|
| 327 | 4 | ->with('message', 'user', 'user.role') |
|
| 328 | 4 | ->whereNotIn('user_id', $this->getExcludeUsers()->lists('id')) |
|
| 329 | 4 | ->where('project_id', '=', $this->getProjectId()) |
|
| 330 | 4 | ->get(); |
|
| 331 | } |
||
| 332 | |||
| 333 | 4 | return $this->projectUsers; |
|
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Returns the model that is belong to the queue message. |
||
| 338 | * |
||
| 339 | * @return Issue|Issue\Comment|Note |
||
| 340 | */ |
||
| 341 | 4 | protected function getModel() |
|
| 342 | { |
||
| 343 | 4 | return $this->latestMessage->model; |
|
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Returns an instance of project issue. |
||
| 348 | * |
||
| 349 | * @return Issue |
||
| 350 | */ |
||
| 351 | abstract protected function getIssue(); |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Returns an instance of project. |
||
| 355 | * |
||
| 356 | * @return Project |
||
| 357 | */ |
||
| 358 | abstract protected function getProject(); |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Returns the id of a project. |
||
| 362 | * |
||
| 363 | * @return int |
||
| 364 | */ |
||
| 365 | abstract protected function getProjectId(); |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns collection of all of the users that must not receive messages. |
||
| 369 | * |
||
| 370 | * @return Collection |
||
| 371 | */ |
||
| 372 | 4 | protected function getExcludeUsers() |
|
| 373 | { |
||
| 374 | 4 | if (null === $this->excludeUsers) { |
|
| 375 | 4 | $this->excludeUsers = collect([]); |
|
| 376 | } |
||
| 377 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * Exclude a user from receiving messages. |
||
| 383 | * |
||
| 384 | * @param User $user |
||
| 385 | * |
||
| 386 | * @return $this |
||
| 387 | */ |
||
| 388 | 4 | protected function addToExcludeUsers(User $user) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Find user by id. This search the project users and fallback to excluded list of users. |
||
| 397 | * |
||
| 398 | * @param int $userId |
||
| 399 | * |
||
| 400 | * @return User |
||
| 401 | */ |
||
| 402 | 3 | protected function getUserById($userId) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Returns collection of all messages. |
||
| 415 | * |
||
| 416 | * @return Collection |
||
| 417 | */ |
||
| 418 | 4 | protected function getMessages() |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Send a message to a user. |
||
| 429 | * |
||
| 430 | * @param User $user |
||
| 431 | * @param array $data |
||
| 432 | * |
||
| 433 | * @return mixed |
||
| 434 | */ |
||
| 435 | 4 | private function sendMessage(User $user, array $data) |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Send a message to a collection of users, or send customised message per use logic. |
||
| 449 | * |
||
| 450 | * @param Collection $users |
||
| 451 | * @param array $data |
||
| 452 | * |
||
| 453 | * @return void |
||
| 454 | */ |
||
| 455 | 4 | protected function sendMessages(Collection $users, array $data) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Get customised message per user logic. |
||
| 469 | * |
||
| 470 | * @param int $userId |
||
| 471 | * @param array $messagesData |
||
| 472 | * |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | 4 | protected function getUserMessageData($userId, array $messagesData) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Whether or not the user wants to receive the message. |
||
| 509 | * |
||
| 510 | * @param Project\User $user |
||
| 511 | * @param array $data |
||
| 512 | * |
||
| 513 | * @return bool |
||
| 514 | */ |
||
| 515 | 4 | protected function wantToReceiveMessage(Project\User $user, array $data) |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Send a message to al users in project and full subscribes. |
||
| 553 | * |
||
| 554 | * @param Message\Queue $queue |
||
| 555 | * |
||
| 556 | * @return void |
||
| 557 | */ |
||
| 558 | 2 | protected function sendMessageToAll(Message\Queue $queue) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Load the creator of an issue to the collection of project users. So we can send message to creator if needed. |
||
| 567 | * |
||
| 568 | * @return void |
||
| 569 | */ |
||
| 570 | 3 | protected function loadIssueCreatorToProjectUsers() |
|
| 601 | } |
||
| 602 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: