We have a lots of emails and notifications going on in our website. But let's take an example of user registration email and notification.
Scenario:
A user signs up.
Emails and Notifications:
To user: Welcome aboard!
To admin: New User has registered.
There are 100's of such scenarios, where at some places we send an email to a specific admin and cases like that.
How are we doing it currently?
Right now, we are doing it in a very clumsy way.
A method such as signup() after saving the user to the database, gathers the information that is needed to be replaced in the email template.
We call a method sendMail($data) with all the data the we collected in the signup() method.
The sendMail($data) method is a mess of 100's of cases for every type of emails there are. For example: case 'signup_user': ... and case: 'user_signup_to_admin': ...
In the cases, we are just setting the templates and other variables which helps us determine how we send the email and to whom down in the method after the cases end.
My approach, right now:
I've researched so much for this but not able to settle because of the feeling "If this is not the best way...?".
So, Inside signup() method, after adding a user I raise an event, say user_signed_up. An object that is attached to that event calls the event handler method for user_signed_up. The handler method called is in class EventHandler and has a method userSignedUp($data).
Inside userSignedUp($data), I call UserMail class's signedUp($data) method which then collects and creates a data that is structured as per it is needed by the template and it calls the send() method in MailManager class which is extended by UserMail.
Similar thing happens in AdminMail class.
The thing that is bugging me are as follows:
- Is this the better way then before, or I'm just increasing the complexity with no other gain.
- Should
SingedUpEmailshould be a class of its own and handle both user and admin emails as both of them use different templates and little difference in data. - Should I combine both Users and Admins email in a single class? But that would leave me with 100s of methods in this class. Having them separately will still cause them to have 50 each.
What do you think? How would you have solved it? What are the things I'm doing wrong? What is not SOLID in my method? Any design pattern that could help?
I would love your views and what you think about how one should approach such problems.
I'd appreciate your constructive comments and remarks. Thanks.
Aucun commentaire:
Enregistrer un commentaire