subSecond(); foreach ($employeeList as $employeeToImport) { $matricule = $employeeToImport->matricule->value; if (in_array($matricule, $matriculesToInsert, true)) { $errorMsg = "Duplicate detected in the file: The matricule {$matricule} appears multiple times."; $errors[] = $errorMsg; $this->logger->warning($errorMsg, [ 'matricule' => $matricule, ]); continue; } $matriculesToInsert[] = $matricule; // NOTE: We generate a new code for each row, but only insert it for new records. $code = $this->codeService->generateFrom(new Matricule($matricule)); $upsertData[] = new EmployeeUpsertData( matricule: $employeeToImport->matricule, firstname: $employeeToImport->firstname, lastname: $employeeToImport->lastname, etablissement: $employeeToImport->etablissement, service: $employeeToImport->service, site: $employeeToImport->site, startDate: $employeeToImport->startDate, endDate: $employeeToImport->endDate, code: $code ); } if (empty($upsertData) && empty($errors)) { $errors[] = 'The file contains no valid data.'; $this->logger->notice('Upsert skipped: No valid employee data found in list.'); } if (! empty($upsertData)) { $this->logger->debug('Dispatching upsert batch to storage service.', [ 'batch_size' => count($upsertData), ]); $this->employeeImportStorageService->upsertEmployees($upsertData); $result = $this->employeeImportStorageService ->getImportedAfter( array_map( fn ($m) => new Matricule($m), $matriculesToInsert ), $importStartTime ); $insertedCount = $result->insertedCount; $updatedCount = $result->updatedCount; $this->logger->info('Batch upsert completed.', [ 'inserted' => $insertedCount, 'updated' => $updatedCount, 'errors' => count($errors), ]); } else { $insertedCount = 0; $updatedCount = 0; } return new EmployeeUpsertServiceResult( insertedCount: $insertedCount, updatedCount: $updatedCount, errors: $errors, ); } }