Whats this? We can add new servers now?!

This commit is contained in:
Dane Everitt 2015-12-15 15:08:41 -05:00
parent 6289e7ae8d
commit 264431a271
10 changed files with 214 additions and 85 deletions

View file

@ -8,16 +8,6 @@ use Uuid;
class UuidService
{
/**
* @var string
*/
protected $table = 'users';
/**
* @var string
*/
protected $field = 'uuid';
/**
* Constructor
*/
@ -26,45 +16,23 @@ class UuidService
//
}
/**
* Set the table that we need to be checking in the database.
*
* @param string $table
* @return void
*/
public function table($table)
{
$this->table = $table;
return $this;
}
/**
* Set the field in the given table that we want to check for a unique UUID.
*
* @param string $field
* @return void
*/
public function field($field)
{
$this->field = $field;
return $this;
}
/**
* Generate a unique UUID validating against specified table and column.
* Defaults to `users.uuid`
*
* @param string $table
* @param string $field
* @param integer $type The type of UUID to generate.
* @return string
*/
public function generate($type = 4)
public function generate($table = 'users', $field = 'uuid', $type = 4)
{
$return = false;
do {
$uuid = LaravelUUID::generate($type);
if (!DB::table($this->table)->where($this->field, $uuid)->exists()) {
$uuid = Uuid::generate($type);
if (!DB::table($table)->where($field, $uuid)->exists()) {
$return = $uuid;
}
@ -81,13 +49,15 @@ class UuidService
* @param string $field
* @return string
*/
public function generateShort($table = 'servers', $field = 'uuidShort')
public function generateShort($table = 'servers', $field = 'uuidShort', $attachedUuid = null)
{
$return = false;
do {
$short = substr(Uuid::generate(4), 0, 8);
$short = (is_null($attachedUuid)) ? substr(Uuid::generate(4), 0, 8) : substr($attachedUuid, 0, 8);
$attachedUuid = null;
if (!DB::table($table)->where($field, $short)->exists()) {
$return = $short;
}