clock->today(); $now = $this->clock->now(); return $this->transactions->run(function () use ($today, $now): int { $this->notifications->deleteOlderThan($now->modify('-'.self::KEEP_DAYS.' days')); if (! $this->workingDays->isWorkingDay($today)) { return 0; } $until = $this->workingDays->nextAfter($today)->format('Y-m-d'); $added = 0; foreach ($this->tasks->startingBetween($today->format('Y-m-d'), $until) as $task) { $params = [ 'task_id' => $task->taskId, 'task_title' => $task->title, 'job_id' => $task->jobId, 'job_number' => $task->jobNumber, 'job_name' => $task->jobName, 'starts_on' => $task->plannedFrom, ]; foreach ($task->recipientIds as $userId) { if ($this->notifications->addIfNew(NewNotification::taskStartsSoon($userId, $task->taskId, $task->plannedFrom, $params, $now))) { $added++; } } } return $added; }); } }