0

I'm working on a pretty simple site for photo galleries, users have the ability to vote on their favorite photo in gallery. I have some events setup each time a user votes, a VoteCase event is called, which receives the vote object.

The VOTE object consist of...

  • photoID
  • voterID

The event receives the vote object and sends the photo owner and email saying their photo was chosen. There's a separate AppMailer.php class for sending the email.

My question is, obviously I need to do a lookup to see who the photo belongs to (I already have an eloquent relationship for that ($photo->user;)

But is that the job of the event? To do a database call and retrieve the user object to pass to the AppMailer? Or should the AppMailer class receive the entire event, and do the user lookup itself?

There's a few other events associated with voting as well. incrementing vote count, giving the voting user credit and possibly assigning a "badge" etc. Each of these might have additional DB lookups. So knowing the best place to put this is helpful.

I've been told an event is very similar to a DTO, but my experience with them is non-existent.

dangel
  • 7,238
  • 7
  • 48
  • 74

1 Answers1

0

Note: This is my opinion not a rule ( maybe not best practice )

Yes it is okay for you to make a DB call in the event listener ( assuming thats where you are calling your mailer )

  1. This way your app mailer class is not tightly coupled to your ORM/DB and has a Single Responsibility

  2. You are implementing ShouldQueue in your event listener removing any performance concerns. ( You are right? )

Angad Dubey
  • 5,067
  • 7
  • 30
  • 51