Of course emitting events and listening to them is a good way of handling this. It is up to you to use trait or not, if stripe methods are used only in the stripe listener it would be a good thing to not use it.
Call a Trait from a Listener, or not?
I'm building an application - when the user registers, it does two things :
- Create a Stripe user for them and save the Stripe username to the DB.
- Send a welcome email.
When originally set up, I used two traits, one for Stripe-related stuff, and one for email-related stuff, so the registration process just called both functions, one after the other, as part of the signup process.
In the interests of speed, I then created an event (UserRegistered) and two listeners (AddUserToStripe and SendWelcomeEmail) which implemented ShouldQueue, so that the user wasn't sitting waiting for the email to be sent, and site to have its chat with Stripe, before being told that they were successfully registered.
Here's my questions.
-
Can / should I keep the methods in a Trait, and just have the Listeners use the Trait? Whilst I'm unlikely to ever want to create a Stripe user separately, outside the user registration process, the email Trait was set up to send all emails from all aspects of the app, so is very widely used.
-
Is the above the best way of going about things anyway? Although I'm a relatively proficient coder, I'm entirely self-taught, over the years, so it's entirely possible that a much better way exists and I have no idea. Feel free to say so. But gently. ;-)
Thanks in advance,
Giles
Please or to participate in this conversation.