mardi 10 septembre 2019

Which design pattern should I use for notifications?

I am working on laravel project using queues. My 'checkAgeAndNotify' API will check if age is greater than 50, then it will send email notification to that user using queue.

class Age extends Controller
 {
    public function checkAgeAndNotify(Request $request)
    {
       if(Input::get('age') > 50){
           $job = (new notification());
           dispatch($job);
       }
    }
 }

Following is my job class:

`class notification implements ShouldQueue
 {
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public construct(){}

    public function handle()
    {
        Mail::send('welcome', $data, function ($message) use ($data) {
                $message->from('my-test@gmail.com', 'unknown-sender');
                $message->to('alpha@test.com')->subject('Test Email');
        });
    }

}`

My code is working 100% perfectly. But I am not sure how to transform my code using any design pattern according to the situation.

If pattern could not be applied in this small code. So, we can assume that we have multiple channels to send notifications such as email, via sms and etc.

Aucun commentaire:

Enregistrer un commentaire