diff --git a/config/config.php b/config/config.php index cb5e88f51..c4ef3f09a 100644 --- a/config/config.php +++ b/config/config.php @@ -22,6 +22,7 @@ use Bavix\Wallet\Internal\Service\ConnectionService; use Bavix\Wallet\Internal\Service\DatabaseService; use Bavix\Wallet\Internal\Service\DispatcherService; +use Bavix\Wallet\Internal\Service\IdentifierFactoryService; use Bavix\Wallet\Internal\Service\JsonService; use Bavix\Wallet\Internal\Service\LockService; use Bavix\Wallet\Internal\Service\MathService; @@ -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, ], /** diff --git a/database/2021_11_02_202021_update_wallets_uuid_table.php b/database/2021_11_02_202021_update_wallets_uuid_table.php index 61293b37d..a442bedda 100644 --- a/database/2021_11_02_202021_update_wallets_uuid_table.php +++ b/database/2021_11_02_202021_update_wallets_uuid_table.php @@ -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; @@ -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(); }); }); diff --git a/docs/guide/cqrs/create-wallet.md b/docs/guide/cqrs/create-wallet.md index 118195240..eb7c6e029 100644 --- a/docs/guide/cqrs/create-wallet.md +++ b/docs/guide/cqrs/create-wallet.md @@ -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. diff --git a/src/Internal/Assembler/TransactionDtoAssembler.php b/src/Internal/Assembler/TransactionDtoAssembler.php index 93b0a6e4c..5c19e9874 100644 --- a/src/Internal/Assembler/TransactionDtoAssembler.php +++ b/src/Internal/Assembler/TransactionDtoAssembler.php @@ -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, ) { } @@ -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, diff --git a/src/Internal/Assembler/TransferDtoAssembler.php b/src/Internal/Assembler/TransferDtoAssembler.php index 327311744..0bbb3b3e5 100644 --- a/src/Internal/Assembler/TransferDtoAssembler.php +++ b/src/Internal/Assembler/TransferDtoAssembler.php @@ -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, ) { } @@ -33,7 +33,7 @@ public function create( ?array $extra, ): TransferDtoInterface { return new TransferDto( - $uuid ?? $this->uuidService->uuid4(), + $uuid ?? $this->identifierFactoryService->generate(), $depositId, $withdrawId, $status, diff --git a/src/Internal/Service/IdentifierFactoryService.php b/src/Internal/Service/IdentifierFactoryService.php new file mode 100644 index 000000000..8964fd116 --- /dev/null +++ b/src/Internal/Service/IdentifierFactoryService.php @@ -0,0 +1,43 @@ +uuidFactory->uuid7($this->clockService->now()) + ->toString(); + } +} diff --git a/src/Internal/Service/IdentifierFactoryServiceInterface.php b/src/Internal/Service/IdentifierFactoryServiceInterface.php new file mode 100644 index 000000000..2855a6c85 --- /dev/null +++ b/src/Internal/Service/IdentifierFactoryServiceInterface.php @@ -0,0 +1,27 @@ +uuid ??= app(UuidFactoryServiceInterface::class)->uuid4(); + $this->uuid ??= app(IdentifierFactoryServiceInterface::class)->generate(); } } diff --git a/src/Services/WalletService.php b/src/Services/WalletService.php index d33a072c9..664ec5a62 100644 --- a/src/Services/WalletService.php +++ b/src/Services/WalletService.php @@ -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; @@ -19,7 +19,7 @@ { public function __construct( private WalletCreatedEventAssemblerInterface $walletCreatedEventAssembler, - private UuidFactoryServiceInterface $uuidFactoryService, + private IdentifierFactoryServiceInterface $identifierFactoryService, private DispatcherServiceInterface $dispatcherService, private WalletRepositoryInterface $walletRepository ) { @@ -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, [ diff --git a/src/WalletServiceProvider.php b/src/WalletServiceProvider.php index 4461136e3..364307b07 100644 --- a/src/WalletServiceProvider.php +++ b/src/WalletServiceProvider.php @@ -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; @@ -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 + ); } /** @@ -451,6 +457,7 @@ private function internalProviders(): array StateServiceInterface::class, TranslatorServiceInterface::class, UuidFactoryServiceInterface::class, + IdentifierFactoryServiceInterface::class, ]; } diff --git a/tests/Units/Domain/BlockTest.php b/tests/Units/Domain/BlockTest.php index cea263307..b7445d196 100644 --- a/tests/Units/Domain/BlockTest.php +++ b/tests/Units/Domain/BlockTest.php @@ -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; @@ -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( diff --git a/tests/Units/Domain/EventTest.php b/tests/Units/Domain/EventTest.php index b57446dab..c0f2487f3 100644 --- a/tests/Units/Domain/EventTest.php +++ b/tests/Units/Domain/EventTest.php @@ -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; @@ -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; @@ -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); diff --git a/tests/Units/Domain/ExtraTest.php b/tests/Units/Domain/ExtraTest.php index ce2104c68..1551af2b2 100644 --- a/tests/Units/Domain/ExtraTest.php +++ b/tests/Units/Domain/ExtraTest.php @@ -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; @@ -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, @@ -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( diff --git a/tests/Units/Domain/MultiWalletTest.php b/tests/Units/Domain/MultiWalletTest.php index ee756389d..084b00394 100644 --- a/tests/Units/Domain/MultiWalletTest.php +++ b/tests/Units/Domain/MultiWalletTest.php @@ -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; @@ -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', diff --git a/tests/Units/Service/WalletTest.php b/tests/Units/Service/WalletTest.php index 2d03dcc57..68a17786d 100644 --- a/tests/Units/Service/WalletTest.php +++ b/tests/Units/Service/WalletTest.php @@ -6,7 +6,7 @@ use Bavix\Wallet\Internal\Exceptions\ExceptionInterface; use Bavix\Wallet\Internal\Exceptions\ModelNotFoundException; -use Bavix\Wallet\Internal\Service\UuidFactoryServiceInterface; +use Bavix\Wallet\Internal\Service\IdentifierFactoryServiceInterface; use Bavix\Wallet\Services\WalletServiceInterface; use Bavix\Wallet\Test\Infra\Factories\BuyerFactory; use Bavix\Wallet\Test\Infra\Factories\UserMultiFactory; @@ -24,10 +24,10 @@ public function testFindBy(): void /** @var Buyer $buyer */ $buyer = BuyerFactory::new()->create(); - $uuidFactoryService = app(UuidFactoryServiceInterface::class); + $uuidFactoryService = app(IdentifierFactoryServiceInterface::class); $walletService = app(WalletServiceInterface::class); - $uuid = $uuidFactoryService->uuid4(); + $uuid = $uuidFactoryService->generate(); self::assertNull($walletService->findBySlug($buyer, 'default')); self::assertNull($walletService->findByUuid($uuid)); @@ -66,10 +66,10 @@ public function testCreateWalletWithUuid(): void /** @var UserMulti $user */ $user = UserMultiFactory::new()->create(); - $uuidFactoryService = app(UuidFactoryServiceInterface::class); + $uuidFactoryService = app(IdentifierFactoryServiceInterface::class); /** @var string[] $uuids */ - $uuids = array_map(static fn () => $uuidFactoryService->uuid4(), range(1, 10)); + $uuids = array_map(static fn () => $uuidFactoryService->generate(), range(1, 10)); foreach ($uuids as $uuid) { $user->createWallet([ @@ -87,8 +87,8 @@ public function testGetByUuid(): void $this->expectException(ModelNotFoundException::class); $this->expectExceptionCode(ExceptionInterface::MODEL_NOT_FOUND); - $uuidFactoryService = app(UuidFactoryServiceInterface::class); + $uuidFactoryService = app(IdentifierFactoryServiceInterface::class); - app(WalletServiceInterface::class)->getByUuid($uuidFactoryService->uuid4()); + app(WalletServiceInterface::class)->getByUuid($uuidFactoryService->generate()); } }