Reputation: 10828
I like to write the heavy logic in the Service class app(MailService::class);
, however from Service class - how would you response back to Job Class to execute $this->release()
or attempt check like $this->attempts()
?
Similar, how would you response back for Command (SendReminderCommand
) to pass into $this->error()
or $this->info()
- This should also be flexible for Controller.
I want to use Service Class to be flexible so it can be used with Job Class, Command or even Controller.
For example:
Job Class
class SendReminderEmail extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
public function handle()
{
$mail = app(MailService::class); //service class
$this->release(10)
$this->attempts()
}
}
Command Class
class SendReminderCommand extends Command
{
protected $signature = 'email:reminder';
protected $description = 'Send Reminder';
public function handle()
{
$mail = app(MailService::class); //service class
$this->info("Emails Sent");
$this->error('Something went wrong!');
}
}
Upvotes: 1
Views: 3039
Reputation: 1035
You can structure your service methods so that non-query methods can be assumed to have been successful if no exception was thrown like so:
public function handle(MailService $mail)
{
try {
$mail->performAction();
$this->info('Action Complete');
} catch (RecoverableProblem $e) {
/* Recover and perhaps retry at a later time? */
} catch (UnrecoverableProblem $e) {
$this->error('Something went wrong!');
/* Abort? */
}
}
If you need to pass some additional information to the logging methods, simply return that information from the service methods and use the return value to build the message.
Upvotes: 1