client = $payment->client; $this->payment = $payment; } public function run() { if ($this->payment->number != '') { return $this->payment; } $this->trySaving(); // $this->payment->number = $this->getNextPaymentNumber($this->client, $this->payment); return $this->payment; } private function trySaving() { $x = 1; do { try { $this->payment->number = $this->getNextPaymentNumber($this->client, $this->payment); $this->payment->saveQuietly(); $this->completed = false; } catch (QueryException $e) { $x++; if ($x > 10) { $this->completed = false; } } } while ($this->completed); } }