Skip to content

Commit

Permalink
Merge pull request #994 from bavix/identifier
Browse files Browse the repository at this point in the history
[11.3] UUID7. IdentifierFactoryServiceInterface
  • Loading branch information
rez1dent3 authored Sep 11, 2024
2 parents 23a5617 + beac5c4 commit 924aa98
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 40 deletions.
11 changes: 11 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Bavix\Wallet\Internal\Service\ConnectionService;

Check notice on line 22 in config/config.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Usage of internal entity

Class 'ConnectionService' is marked as @internal
use Bavix\Wallet\Internal\Service\DatabaseService;
use Bavix\Wallet\Internal\Service\DispatcherService;
use Bavix\Wallet\Internal\Service\IdentifierFactoryService;
use Bavix\Wallet\Internal\Service\JsonService;

Check notice on line 26 in config/config.php

View workflow job for this annotation

GitHub Actions / Qodana for PHP

Usage of internal entity

Class 'JsonService' is marked as @internal
use Bavix\Wallet\Internal\Service\LockService;
use Bavix\Wallet\Internal\Service\MathService;
Expand Down Expand Up @@ -209,8 +210,18 @@
* The service for generating UUIDs.
*
* @var string
*
* @deprecated use identifier.
* @see IdentifierFactoryService
*/
'uuid' => UuidFactoryService::class,

/**
* The service for generating identifiers.
*
* @var string
*/
'identifier' => IdentifierFactoryService::class,
],

/**
Expand Down
4 changes: 2 additions & 2 deletions database/2021_11_02_202021_update_wallets_uuid_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
use Bavix\Wallet\Internal\Service\IdentifierFactoryServiceInterface;
use Bavix\Wallet\Models\Wallet;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
Expand All @@ -27,7 +27,7 @@ public function up(): void

Wallet::query()->chunk(10000, static function (Collection $wallets) {
$wallets->each(function (Wallet $wallet) {
$wallet->uuid = app(UuidFactoryServiceInterface::class)->uuid4();
$wallet->uuid = app(IdentifierFactoryServiceInterface::class)->generate();
$wallet->save();
});
});
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/cqrs/create-wallet.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function __invoke(CreateWalletCommandMessage $message): void
}
```

You receive requests to create a wallet on the backend, and you create them asynchronously. UUID4 is generated on the client side and the client already knows it. You will not be able to create two wallets with one uuid, because the column in the database is unique.
You receive requests to create a wallet on the backend, and you create them asynchronously. UUID is generated on the client side and the client already knows it. You will not be able to create two wallets with one uuid, because the column in the database is unique.

The user no longer needs to wait for the creation of a wallet, it is enough to know the uuid. You get the most stable application.

Expand Down
6 changes: 3 additions & 3 deletions src/Internal/Assembler/TransactionDtoAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use Bavix\Wallet\Internal\Dto\TransactionDto;
use Bavix\Wallet\Internal\Dto\TransactionDtoInterface;
use Bavix\Wallet\Internal\Service\ClockServiceInterface;
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
use Bavix\Wallet\Internal\Service\IdentifierFactoryServiceInterface;
use Illuminate\Database\Eloquent\Model;

final readonly class TransactionDtoAssembler implements TransactionDtoAssemblerInterface
{
public function __construct(
private UuidFactoryServiceInterface $uuidService,
private IdentifierFactoryServiceInterface $identifierFactoryService,
private ClockServiceInterface $clockService,
) {
}
Expand All @@ -28,7 +28,7 @@ public function create(
?string $uuid
): TransactionDtoInterface {
return new TransactionDto(
$uuid ?? $this->uuidService->uuid4(),
$uuid ?? $this->identifierFactoryService->generate(),
$payable->getMorphClass(),
$payable->getKey(),
$walletId,
Expand Down
6 changes: 3 additions & 3 deletions src/Internal/Assembler/TransferDtoAssembler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use Bavix\Wallet\Internal\Dto\TransferDto;
use Bavix\Wallet\Internal\Dto\TransferDtoInterface;
use Bavix\Wallet\Internal\Service\ClockServiceInterface;
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
use Bavix\Wallet\Internal\Service\IdentifierFactoryServiceInterface;
use Illuminate\Database\Eloquent\Model;

final readonly class TransferDtoAssembler implements TransferDtoAssemblerInterface
{
public function __construct(
private UuidFactoryServiceInterface $uuidService,
private IdentifierFactoryServiceInterface $identifierFactoryService,
private ClockServiceInterface $clockService,
) {
}
Expand All @@ -33,7 +33,7 @@ public function create(
?array $extra,
): TransferDtoInterface {
return new TransferDto(
$uuid ?? $this->uuidService->uuid4(),
$uuid ?? $this->identifierFactoryService->generate(),
$depositId,
$withdrawId,
$status,
Expand Down
43 changes: 43 additions & 0 deletions src/Internal/Service/IdentifierFactoryService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Service;

use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\Exception\UnsupportedOperationException;
use Ramsey\Uuid\Exception\UuidExceptionInterface;
use Ramsey\Uuid\UuidFactory;

final readonly class IdentifierFactoryService implements IdentifierFactoryServiceInterface
{
/**
* @param UuidFactory $uuidFactory Service for generating UUIDs.
* @param ClockServiceInterface $clockService Service for getting the current time.
*/
public function __construct(
private UuidFactory $uuidFactory,
private ClockServiceInterface $clockService,
) {
}

/**
* Generate a ID string using the uuid7 algorithm.
*
* uuid7 is a time-based UUID algorithm that uses the current time in milliseconds,
* combined with a random number to generate a unique ID.
*
* @return non-empty-string The generated ID string.
*
* @throws InvalidArgumentException If a field is invalid in the UUID.
* @throws InvalidUuidStringException If the string we are parsing is not a valid UUID.
* @throws UnsupportedOperationException If the UUID implementation can't support a feature.
* @throws UuidExceptionInterface If there is an error generating the UUID.
*/
public function generate(): string
{
return $this->uuidFactory->uuid7($this->clockService->now())
->toString();
}
}
27 changes: 27 additions & 0 deletions src/Internal/Service/IdentifierFactoryServiceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Bavix\Wallet\Internal\Service;

use Ramsey\Uuid\Exception\InvalidArgumentException;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\Exception\UnsupportedOperationException;
use Ramsey\Uuid\Exception\UuidExceptionInterface;

interface IdentifierFactoryServiceInterface
{
/**
* Generate a unique identifier string.
*
* This method generates a unique identifier string using internal algorithm.
*
* @return non-empty-string The generated ID string.
*
* @throws InvalidArgumentException If a field is invalid in the UUID.
* @throws InvalidUuidStringException If the string we are parsing is not a valid UUID.
* @throws UnsupportedOperationException If the UUID implementation can't support a feature.
* @throws UuidExceptionInterface If there is an error generating the UUID.
*/
public function generate(): string;
}
3 changes: 3 additions & 0 deletions src/Internal/Service/UuidFactoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use Ramsey\Uuid\UuidFactory;

/**
* @codeCoverageIgnore
*/
final readonly class UuidFactoryService implements UuidFactoryServiceInterface
{
public function __construct(
Expand Down
5 changes: 5 additions & 0 deletions src/Internal/Service/UuidFactoryServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace Bavix\Wallet\Internal\Service;

/**
* @deprecated use IdentifierFactoryServiceInterface instead.
* @see IdentifierFactoryServiceInterface
* @since 11.3.0
*/
interface UuidFactoryServiceInterface
{
/**
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
use Bavix\Wallet\Interfaces\WalletFloat;
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
use Bavix\Wallet\Internal\Exceptions\TransactionFailedException;
use Bavix\Wallet\Internal\Service\IdentifierFactoryServiceInterface;
use Bavix\Wallet\Internal\Service\MathServiceInterface;
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
use Bavix\Wallet\Services\AtomicServiceInterface;
use Bavix\Wallet\Services\RegulatorServiceInterface;
use Bavix\Wallet\Traits\CanConfirm;
Expand Down Expand Up @@ -193,6 +193,6 @@ public function getCurrencyAttribute(): string

protected function initializeMorphOneWallet(): void
{
$this->uuid ??= app(UuidFactoryServiceInterface::class)->uuid4();
$this->uuid ??= app(IdentifierFactoryServiceInterface::class)->generate();
}
}
6 changes: 3 additions & 3 deletions src/Services/WalletService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Bavix\Wallet\Internal\Exceptions\ModelNotFoundException;
use Bavix\Wallet\Internal\Repository\WalletRepositoryInterface;
use Bavix\Wallet\Internal\Service\DispatcherServiceInterface;
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
use Bavix\Wallet\Internal\Service\IdentifierFactoryServiceInterface;
use Bavix\Wallet\Models\Wallet;
use Illuminate\Database\Eloquent\Model;

Expand All @@ -19,7 +19,7 @@
{
public function __construct(
private WalletCreatedEventAssemblerInterface $walletCreatedEventAssembler,
private UuidFactoryServiceInterface $uuidFactoryService,
private IdentifierFactoryServiceInterface $identifierFactoryService,
private DispatcherServiceInterface $dispatcherService,
private WalletRepositoryInterface $walletRepository
) {
Expand All @@ -30,7 +30,7 @@ public function create(Model $model, array $data): Wallet
$wallet = $this->walletRepository->create(array_merge(
config('wallet.wallet.creating', []),
[
'uuid' => $this->uuidFactoryService->uuid4(),
'uuid' => $this->identifierFactoryService->generate(),
],
$data,
[
Expand Down
7 changes: 7 additions & 0 deletions src/WalletServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
use Bavix\Wallet\Internal\Service\DispatcherService;
use Bavix\Wallet\Internal\Service\DispatcherServiceInterface;
use Bavix\Wallet\Internal\Service\IdentifierFactoryService;
use Bavix\Wallet\Internal\Service\IdentifierFactoryServiceInterface;
use Bavix\Wallet\Internal\Service\JsonService;
use Bavix\Wallet\Internal\Service\JsonServiceInterface;
use Bavix\Wallet\Internal\Service\LockService;
Expand Down Expand Up @@ -253,6 +255,10 @@ private function internal(array $configure): void
$this->app->singleton(StateServiceInterface::class, $configure['state'] ?? StateService::class);
$this->app->singleton(TranslatorServiceInterface::class, $configure['translator'] ?? TranslatorService::class);
$this->app->singleton(UuidFactoryServiceInterface::class, $configure['uuid'] ?? UuidFactoryService::class);
$this->app->singleton(
IdentifierFactoryServiceInterface::class,
$configure['identifier'] ?? IdentifierFactoryService::class
);
}

/**
Expand Down Expand Up @@ -451,6 +457,7 @@ private function internalProviders(): array
StateServiceInterface::class,
TranslatorServiceInterface::class,
UuidFactoryServiceInterface::class,
IdentifierFactoryServiceInterface::class,
];
}

Expand Down
6 changes: 3 additions & 3 deletions tests/Units/Domain/BlockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Bavix\Wallet\External\Dto\Extra;
use Bavix\Wallet\External\Dto\Option;
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
use Bavix\Wallet\Internal\Service\IdentifierFactoryServiceInterface;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
use Bavix\Wallet\Test\Infra\Models\Buyer;
Expand All @@ -26,8 +26,8 @@ public function testBlockTransfer(): void
/** @var Buyer $buyer1 */
/** @var Buyer $buyer2 */
[$buyer1, $buyer2] = BuyerFactory::times(2)->create();
$uuidFactory = app(UuidFactoryServiceInterface::class);
$idempotent = $uuidFactory->uuid4();
$uuidFactory = app(IdentifierFactoryServiceInterface::class);
$idempotent = $uuidFactory->generate();

$transfer = $buyer1->forceTransfer($buyer2, 500, new Extra(
deposit: new Option(
Expand Down
10 changes: 5 additions & 5 deletions tests/Units/Domain/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
use Bavix\Wallet\Internal\Service\ClockServiceInterface;
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
use Bavix\Wallet\Internal\Service\IdentifierFactoryServiceInterface;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Objects\Cart;
use Bavix\Wallet\Services\PurchaseServiceInterface;
Expand Down Expand Up @@ -96,8 +96,8 @@ public function testWalletCreatedThrowListener(): void
/** @var Buyer $buyer */
$buyer = BuyerFactory::new()->create();

$uuidFactoryService = app(UuidFactoryServiceInterface::class);
$buyer->wallet->uuid = $uuidFactoryService->uuid4();
$uuidFactoryService = app(IdentifierFactoryServiceInterface::class);
$buyer->wallet->uuid = $uuidFactoryService->generate();

$holderType = $buyer->getMorphClass();
$uuid = $buyer->wallet->uuid;
Expand All @@ -121,8 +121,8 @@ public function testMultiWalletCreatedThrowListener(): void
/** @var UserMulti $user */
$user = UserMultiFactory::new()->create();

$uuidFactoryService = app(UuidFactoryServiceInterface::class);
$uuid = $uuidFactoryService->uuid4();
$uuidFactoryService = app(IdentifierFactoryServiceInterface::class);
$uuid = $uuidFactoryService->generate();

$holderType = $user->getMorphClass();
$createdAt = app(ClockServiceInterface::class)->now()->format(DateTimeInterface::ATOM);
Expand Down
18 changes: 9 additions & 9 deletions tests/Units/Domain/ExtraTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Bavix\Wallet\External\Dto\Extra;
use Bavix\Wallet\External\Dto\Option;
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
use Bavix\Wallet\Internal\Service\IdentifierFactoryServiceInterface;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Test\Infra\Factories\BuyerFactory;
use Bavix\Wallet\Test\Infra\Factories\UserMultiFactory;
Expand Down Expand Up @@ -71,10 +71,10 @@ public function testExtraTransferUuidFixed(): void
$user1->deposit(1000);
self::assertSame(1000, $user1->balanceInt);

$uuidFactory = app(UuidFactoryServiceInterface::class);
$depositUuid = $uuidFactory->uuid4();
$withdrawUuid = $uuidFactory->uuid4();
$transferUuid = $uuidFactory->uuid4();
$uuidFactory = app(IdentifierFactoryServiceInterface::class);
$depositUuid = $uuidFactory->generate();
$withdrawUuid = $uuidFactory->generate();
$transferUuid = $uuidFactory->generate();

$transfer = $user1->transfer(
$user2,
Expand Down Expand Up @@ -219,10 +219,10 @@ public function testExtraExchangeUuidFixed(): void
self::assertSame(10_000, $rub->balanceInt);
self::assertSame(0, $usd->balanceInt);

$uuidFactory = app(UuidFactoryServiceInterface::class);
$depositUuid = $uuidFactory->uuid4();
$withdrawUuid = $uuidFactory->uuid4();
$transferUuid = $uuidFactory->uuid4();
$uuidFactory = app(IdentifierFactoryServiceInterface::class);
$depositUuid = $uuidFactory->generate();
$withdrawUuid = $uuidFactory->generate();
$transferUuid = $uuidFactory->generate();

$transfer = $rub->exchange($usd, 10000, new Extra(
deposit: new Option(
Expand Down
4 changes: 2 additions & 2 deletions tests/Units/Domain/MultiWalletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Bavix\Wallet\Internal\Exceptions\ExceptionInterface;
use Bavix\Wallet\Internal\Exceptions\ModelNotFoundException;
use Bavix\Wallet\Internal\Service\DatabaseServiceInterface;
use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface;
use Bavix\Wallet\Internal\Service\IdentifierFactoryServiceInterface;
use Bavix\Wallet\Models\Transaction;
use Bavix\Wallet\Models\Transfer;
use Bavix\Wallet\Services\BookkeeperServiceInterface;
Expand Down Expand Up @@ -441,7 +441,7 @@ public function testGetWallet(): void
$secondWallet = $user->getWalletOrFail('test');
self::assertSame($secondWallet->getKey(), $firstWallet->getKey());

$uuid = app(UuidFactoryServiceInterface::class)->uuid4();
$uuid = app(IdentifierFactoryServiceInterface::class)->generate();
$test2 = $user->wallets()
->create([
'name' => 'Test2',
Expand Down
Loading

0 comments on commit 924aa98

Please sign in to comment.