complete Complete User Request Submission section RAD-1
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
namespace App\Services\Document;
|
||||
|
||||
use App\Models\Document;
|
||||
use RuntimeException;
|
||||
|
||||
class DocumentCodeService
|
||||
{
|
||||
public function generate(int $tries = 10): string
|
||||
{
|
||||
for ($i = 0; $i < $tries; $i++) {
|
||||
$code = str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
|
||||
if (! Document::where('code', $code)->exists()) {
|
||||
return $code;
|
||||
}
|
||||
}
|
||||
throw new RuntimeException('Unable to generate unique code');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user