next($schedulingService->getPickupDay()->value); $employees = $this->getEmployeesToNotify($pickupDay); if ($employees->isEmpty()) { return; } $cutoffDate = CarbonImmutable::instance( $schedulingService->getPeriodForPickupDate($pickupDay)->getStartDate() )->setTimezone('Europe/Paris'); $icsContent = $this->generateIcsContent($icsGenerator, $cutoffDate); foreach ($employees as $employee) { $employee->notify(new MissingOrderReminderNotification($pickupDay, $cutoffDate, $icsContent)); } } private function generateIcsContent(IcsGeneratorService $icsGenerator, CarbonImmutable $cutoffDate): IcsContent { $appUrl = Config::string('app.url'); $appHost = parse_url($appUrl, \PHP_URL_HOST); if (empty($appHost)) { throw new \UnexpectedValueException('Invalid app URL configuration'); } $appName = Config::string('app.name', 'La Boutique de Cocotte'); $closureTime = $cutoffDate->format('G') . 'h'; $eventStart = $cutoffDate->subHours(2); $eventEnd = $eventStart->addMinutes(15); return $icsGenerator->generate(new IcsContentDto( uid: 'reminder-' . $eventStart->format('Y-m-d') . '@' . $appHost, summary: trans('Place my order :appname', [ 'appname' => $appName, ]), description: trans('Remember to validate your order before :closureTime on :url', [ 'closureTime' => $closureTime, 'url' => $appHost, ]), startsAt: $eventStart, endsAt: $eventEnd, url: $appUrl, )); } /** @return Collection */ private function getEmployeesToNotify(CarbonInterface $pickupDay): Collection { $excludedMatricules = Order::query() ->whereDate('pickup_day', $pickupDay->format('Y-m-d')) ->where('status', OrderStatus::VALIDATED) ->get() ->map(fn (Order $order): Matricule => $order->employee->matricule) ->unique(); return Employee::query() ->with('customer') ->whereNotIn('matricule', $excludedMatricules) ->get() ->filter(fn (Employee $employee): bool => $this->canNotifyEmployee($employee)); } private function canNotifyEmployee(Employee $employee): bool { $customer = $employee->customer; return $customer && Gate::forUser($customer)->check('customerPassOrders'); } }