credits = $credits; $this->company = $company; $this->user = $user; $this->settings = $company->settings; } /** * Execute the job. * * @return void */ public function handle() { MultiDB::setDb($this->company->db); // create new zip object $zipFile = new \PhpZip\ZipFile(); $file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.credits')).'.zip'; $invitation = $this->credits->first()->invitations->first(); $path = $this->credits->first()->client->quote_filepath($invitation); $this->credits->each(function ($credit) { (new CreateEntityPdf($credit->invitations()->first()))->handle(); }); try { foreach ($this->credits as $credit) { $file = $credit->service()->getCreditPdf($credit->invitations()->first()); $zip_file_name = basename($file); $zipFile->addFromString($zip_file_name, Storage::get($file)); } Storage::put($path.$file_name, $zipFile->outputAsString()); $nmo = new NinjaMailerObject; $nmo->mailable = new DownloadCredits(Storage::url($path.$file_name), $this->company); $nmo->to_user = $this->user; $nmo->settings = $this->settings; $nmo->company = $this->company; NinjaMailerJob::dispatch($nmo); UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1)); } catch (\PhpZip\Exception\ZipException $e) { // handle exception } finally { $zipFile->close(); } } }