Can the controller send email?

2

In a controller called activities, I have a method called reminder. This method, receives an id, searches for that activity based on this id and sends an email to the moderator of that activity with data of the same. Is this method with 'too much responsibility'?

I am sending two methods to join another question. By code designer, which one is correct? What does it send separates the $ client into a variable (reminder_a), or does it always use $ activity to refer to clients (reminder_b)?

Follow method:

/**
 * Send the remember email with a resource
 * @param  int $id
 */
public function reminder_a($id)
{

    $activity   = $this->activities->getById($id);
    $client     = $activity->client;

    $title = 'Reminder of activity ' . $activity->name;
    $view  = 'emails.activity.reminder';
    $data  = ['activity' => $activity, 'cliente' => $client];
    CustomMail::sendBasicMail($view, $title, $activity->client->email, $data);

}


/**
 * Send the remember email with a resource
 * @param  int $id
 */
public function reminder_b($id)
{

    $activity = $this->activities->getById($id);

    $title = 'Reminder of activity ' . $activity->name;
    $view  = 'emails.activity.reminder';
    CustomMail::sendBasicMail($view, $title, $activity->client->email, $activity);

}   
    
asked by anonymous 06.12.2014 / 20:05

1 answer

3

Power, you can. But it is not recommended.

Comparing the two methods, it seems to me that the difference is just passing the client separated from the activity , which for me would not be necessary since one is the attribute of the other. So it seems to me that the reminder_b method is more correct.

I also do not see any error in applying the principle of Single Responsibility Principle (the "S" of SOLID) because each controller method has only one responsibility, which is to send the email.

The only thing I would change (apart from opting for the reminder_b method) would be encapsulating the sending of email, perhaps placing it inside a service. The controller should only be seen as an entry point for your application (for example, you could send the email also through the command line), perform a single task, and return a corresponding response.

I hope I have been clear. :)

    
06.12.2014 / 23:00