next($schedulingService->getPickupDay()->value); $employees = $this->getEmployeesToNotify($pickupDay, $orderRepository, $employeeRepository); foreach ($employees as $employee) { $employee->notify(new MissingOrderReminderNotification($pickupDay)); } } /** @return Collection */ private function getEmployeesToNotify( CarbonInterface $pickupDay, OrderRepository $orderRepository, EmployeeRepository $employeeRepository ): Collection { $orders = $orderRepository->getValidatedOrdersForPickupDay($pickupDay); /** @var array $excludedMatricules */ $excludedMatricules = collect($orders) ->map(fn ($order) => $order->employee->matricule) ->uniqueValues() ->all(); return collect($employeeRepository->getWithCustomerWhereMatriculeNotIn($excludedMatricules)) ->filter(fn ($employee) => $this->canNotifyEmployee($employee)); } private function canNotifyEmployee(Employee $employee): bool { $customer = $employee->customer; return $customer && Gate::check('customerPassOrders', $customer); } }