quantity = $quantity; $this->length = $length; $this->settings = $settings; } /** * Execute the job. * * @return void */ public function handle() { set_time_limit(0); for ($ib = 1; $ib <= $this->quantity;) { try { $code = new Codes(); $code->code_unique = $this->getCode($this->length); $code->date_creation = now(); $code->save(); if ($code) { $ib++; } } catch (\PDOException $e) { if (!strpos($e->getMessage(), 'Duplicate entry')) { Mail::to(config('mail.MAIL_ADMIN'))->send(new JobFailed("ProcessUniqueCode", ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()])); die(); } } } // ---- Fin de la génération Mail::to(config('mail.MAIL_ADMIN'))->send(new CodeGenerationEnd($this->quantity, $this->settings)); } private function getCode($car) { $chaine = "ABCDEFGHJKMNPQRSTUWXYZ123456789"; // Caractères autorisés $valprec = ""; $string = ""; srand(); for ($i = 0; $i < $car;) { $val = $chaine[rand() % strlen($chaine)]; if ($val != $valprec) { $string .= $val; $valprec = $val; $i++; } } return $string; } public function failed(Exception $exception) { Mail::to(config('mail.MAIL_ADMIN'))->send(new JobFailed("ProcessUniqueCode", ['message' => $exception->getMessage(), 'trace' => $exception->getTraceAsString()])); } }