Skip to content

Commit

Permalink
[4.x] Improve id generators (#1300)
Browse files Browse the repository at this point in the history
* add RandomIntGenerator

* remove string assertions

* make int ranges configurable

* update test to use min & max
  • Loading branch information
stancl authored Jan 21, 2025
1 parent 7ce7629 commit 25360f6
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions assets/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*
* @see \Stancl\Tenancy\UniqueIdentifierGenerators\UUIDGenerator
* @see \Stancl\Tenancy\UniqueIdentifierGenerators\RandomHexGenerator
* @see \Stancl\Tenancy\UniqueIdentifierGenerators\RandomIntGenerator
* @see \Stancl\Tenancy\UniqueIdentifierGenerators\RandomStringGenerator
*/
'id_generator' => UniqueIdentifierGenerators\UUIDGenerator::class,
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/UniqueIdentifierGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ interface UniqueIdentifierGenerator
/**
* Generate a unique identifier for a model.
*/
public static function generate(Model $model): string;
public static function generate(Model $model): string|int;
}
2 changes: 1 addition & 1 deletion src/UniqueIdentifierGenerators/RandomHexGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RandomHexGenerator implements UniqueIdentifierGenerator
{
public static int $bytes = 6;

public static function generate(Model $model): string
public static function generate(Model $model): string|int
{
return bin2hex(random_bytes(static::$bytes));
}
Expand Down
22 changes: 22 additions & 0 deletions src/UniqueIdentifierGenerators/RandomIntGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Stancl\Tenancy\UniqueIdentifierGenerators;

use Illuminate\Database\Eloquent\Model;
use Stancl\Tenancy\Contracts\UniqueIdentifierGenerator;

/**
* Generates a cryptographically secure random integer for the tenant key.
*/
class RandomIntGenerator implements UniqueIdentifierGenerator
{
public static int $min = 0;
public static int $max = PHP_INT_MAX;

public static function generate(Model $model): string|int
{
return random_int(static::$min, static::$max);
}
}
2 changes: 1 addition & 1 deletion src/UniqueIdentifierGenerators/RandomStringGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RandomStringGenerator implements UniqueIdentifierGenerator
{
public static int $length = 8;

public static function generate(Model $model): string
public static function generate(Model $model): string|int
{
return Str::random(static::$length);
}
Expand Down
2 changes: 1 addition & 1 deletion src/UniqueIdentifierGenerators/UUIDGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/
class UUIDGenerator implements UniqueIdentifierGenerator
{
public static function generate(Model $model): string
public static function generate(Model $model): string|int
{
return Uuid::uuid4()->toString();
}
Expand Down
16 changes: 16 additions & 0 deletions tests/TenantModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@
use Stancl\Tenancy\UniqueIdentifierGenerators\UUIDGenerator;
use Stancl\Tenancy\Exceptions\TenancyNotInitializedException;
use Stancl\Tenancy\UniqueIdentifierGenerators\RandomHexGenerator;
use Stancl\Tenancy\UniqueIdentifierGenerators\RandomIntGenerator;
use Stancl\Tenancy\UniqueIdentifierGenerators\RandomStringGenerator;

afterEach(function () {
RandomIntGenerator::$min = 0;
RandomIntGenerator::$max = PHP_INT_MAX;
});

test('created event is dispatched', function () {
Event::fake([TenantCreated::class]);

Expand Down Expand Up @@ -87,6 +93,16 @@
RandomHexGenerator::$bytes = 6; // reset
});

test('random ints are supported', function () {
app()->bind(UniqueIdentifierGenerator::class, RandomIntGenerator::class);
RandomIntGenerator::$min = 200;
RandomIntGenerator::$max = 1000;

$tenant1 = Tenant::create();
expect($tenant1->id >= 200)->toBeTrue();
expect($tenant1->id <= 1000)->toBeTrue();
});

test('random string ids are supported', function () {
app()->bind(UniqueIdentifierGenerator::class, RandomStringGenerator::class);

Expand Down

0 comments on commit 25360f6

Please sign in to comment.