company = $company; $this->file_path = $file_path; $this->options = $options; $this->current_app_version = config('ninja.app_version'); } public function handle() { MultiDB::setDb($this->company->db); $this->company =Company::where('company_key', $this->company->company_key)->firstOrFail(); $this->account = $this->company->account; $this->unzipFile() ->preFlightChecks(); foreach($this->importables as $import){ $method = Str::ucfirst(Str::camel($import)); $this->{$method}(); } } //check if this is a complete company import OR if it is selective /* Company and settings only Data */ private function preFlightChecks() { //check the file version and perform any necessary adjustments to the file in order to proceed - needed when we change schema if($this->current_app_version != $this->backup_file->app_version) { //perform some magic here } return $this; } private function unzipFile() { $zip = new ZipArchive(); $archive = $zip->open(public_path("storage/backups/{$this->file_path}")); $filename = pathinfo($this->filepath, PATHINFO_FILENAME); $zip->extractTo(public_path("storage/backups/{$filename}")); $zip->close(); $file_location = public_path("storage/backups/$filename/backup.json"); if (! file_exists($file_location)) { throw new NonExistingMigrationFile('Backup file does not exist, or it is corrupted.'); } $this->backup_file = json_decode(file_get_contents($file_location)); return $this; } private function importCompany() { //$this->import_company = .. return $this; } private function importUsers() { User::unguard(); foreach ($this->backup_file->users as $user) { $new_user = User::firstOrNew( ['email' => $user->email], (array)$user, ); $new_user->account_id = $this->account->id; $new_user->save(['timestamps' => false]); $this->ids['users']["{$user->id}"] = $new_user->id; } Expense::reguard(); } public function transformId(string$resource, string $old): int { if (! array_key_exists($resource, $this->ids)) { throw new \Exception("Resource {$resource} not available."); } if (! array_key_exists("{$old}", $this->ids[$resource])) { throw new \Exception("Missing resource key: {$old}"); } return $this->ids[$resource]["{$old}"]; } }