/ app / Jobs / TaskEndNotifier.php
TaskEndNotifier.php
 1  <?php
 2  
 3  namespace App\Jobs;
 4  
 5  use App\Models\Task;
 6  use App\Notifications\TaskEndNotification;
 7  use Illuminate\Bus\Queueable;
 8  use Illuminate\Contracts\Queue\ShouldQueue;
 9  use Illuminate\Foundation\Bus\Dispatchable;
10  use Illuminate\Queue\InteractsWithQueue;
11  use Illuminate\Queue\SerializesModels;
12  
13  class TaskEndNotifier implements ShouldQueue
14  {
15      use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
16  
17      /**
18       * Create a new job instance.
19       *
20       * @return void
21       */
22      public function __construct()
23      {
24          //
25      }
26  
27      /**
28       * Execute the job.
29       *
30       * @return void
31       */
32      public function handle()
33      {
34          try {
35              Task::deadlineClose()
36                  ->notFinished()
37                  ->get()
38                  ->each(function ($task) {
39                      $notifyTo = $task->user ?? $task->dossier->getCertifierId() ?? null;
40  
41                      if ($notifyTo) {
42                          $notifyTo->notify(new TaskEndNotification($task));
43                      }
44                  });
45          } catch (\Exception $e) {
46              logger()->error('TaskEndNotifier error: ' . $e->getMessage());
47              log_exception($e);
48          }
49      }
50  }