Can the controller send an email?

Asked

Viewed 146 times

2

In a controller called activities, I have a method called reminder.
This method, receives an id, looks for that activity based on that id and sends an email to the moderator of that activity with data from it.
This method is with 'too many responsibilities'?

I am sending two methods to already join another doubt. By code designer, which one is correct? What sends separates $client into a variable (reminder_a), or what always uses $Activity to refer to clients (reminder_b)?

Follows 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);

}   

1 answer

3


Power, can. But not recommended.

Comparing the two methods, it seems to me that the difference is just passing the client separate from activity, which to me there would be no need since one is an attribute of the other. Therefore, it seems to me that the method reminder_b is more correct.

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

The only thing I would change (besides opting for the method reminder_b) would encapsulate email sending, maybe put it within a service. The controller should be viewed only as an entry point of your application (for example, you could send the email also via the command line), perform a single task and return a corresponding response.

I hope I was clear. :)

  • Perfect @Rodrigo. About encapsulating email, how I would pass information like: the view to be used, custom title and etc, if I don’t call it in the method where I did the activity query?

  • If you observe, I can use sendBasicMail for any type of email.

  • If the SendBasicMail already encapsulates well the task of sending emails, maybe putting another layer of abstraction (for example, a service) is not so necessary. But what I would do, for example, would be to create a method sendActivityEmail, to which you would pass the activity or simply the $id of it. (in that case it would be necessary to inject the activity repository as a dependency of the email sending service).

  • Hmm.. Today my system sends on average 24 different types of emails. There would be a way for each one? :/

  • That’s a matter of style. As I like to have several methods in which each one performs a certain type of task (and not a single generic task), I would do it. There goes much of the will of the developer. :)

  • There is relativity in that right? A repository for example. You wouldn’t create a method for each different query. : p

  • 1

    Of course not, you group according to the parameters of the methods up to an acceptable state :)

  • 1

    Thanks @Rodrigo. The discussion was very productive and cleared my question.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.