findByKey($key); if (is_null($existing->id)) { $importJob = new ImportJob; $importJob->user()->associate($this->user); $importJob->file_type = $fileType; $importJob->key = Str::random(12); $importJob->status = 'import_status_never_started'; $importJob->extended_status = [ 'total_steps' => 0, 'steps_done' => 0, 'import_count' => 0, 'importTag' => 0, 'errors' => [], ]; $importJob->save(); // breaks the loop: return $importJob; } $count++; } return new ImportJob; } /** * @param string $key * * @return ImportJob */ public function findByKey(string $key): ImportJob { $result = $this->user->importJobs()->where('key', $key)->first(['import_jobs.*']); if (is_null($result)) { return new ImportJob; } return $result; } /** * @param ImportJob $job * @param array $configuration * * @return ImportJob */ public function setConfiguration(ImportJob $job, array $configuration): ImportJob { $job->configuration = $configuration; $job->save(); return $job; } /** * @param User $user */ public function setUser(User $user) { $this->user = $user; } /** * @param ImportJob $job * @param string $status * * @return ImportJob */ public function updateStatus(ImportJob $job, string $status): ImportJob { $job->status = $status; $job->save(); return $job; } }