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