option('user'); // if no email was passed to the option, prompt the user to enter the email if (! $email) { $email = $this->ask('what is the user\'s email?'); } // retrieve the user with the specified email $user = User::where('email', $email)->first(); if (! $user) { // show an error and exist if the user does not exist $this->error('No user with that email.'); return -1; } $path = $this->option('path'); // if no email was passed to the option, prompt the user to enter the email if (! $path) { $path = $this->ask('what file you want to import?'); } if (! $filesystem->exists($path) || ! $this->acceptedExtensions($filesystem, $path)) { $this->error('The provided vcard file was not found or is not valid!'); return -2; } $importJob = $this->import($path, $user); return $this->report($importJob) ? 0 : 1; } private function acceptedExtensions(Filesystem $filesystem, string $path): bool { switch ($filesystem->extension($path)) { case 'vcf': case 'vcard': return true; default: return false; } } private function import(string $path, User $user): ImportJob { $pathName = Storage::putFile('public', new File($path)); $importJob = $user->account->importjobs()->create([ 'user_id' => $user->id, 'type' => 'vcard', 'filename' => $pathName, ]); AddContactFromVCard::dispatchSync($importJob); return $importJob; } private function report(ImportJob $importJob) { $importJob->refresh(); if ($importJob->failed) { $this->warn('Error: '.$importJob->failed_reason); return false; } $this->info('Contacts found: '.$importJob->contacts_found); $this->info('Contacts skipped: '.$importJob->contacts_skipped); $this->info('Contacts imported: '.$importJob->contacts_imported); return true; } }