From b2b8fcf92f16d73ed261a6f9a91f9ca2b5658b80 Mon Sep 17 00:00:00 2001 From: Fabrice Lecomte Date: Fri, 31 Jul 2026 20:59:48 +0200 Subject: [PATCH] refactor: fix cast warning --- .../application/command/handlers/GameEventManager.kt | 12 ++---------- .../command/handlers/JoinTheGameHandler.kt | 2 +- .../application/command/handlers/PlayCardHandler.kt | 2 +- .../command/handlers/ReadyToPlayHandler.kt | 2 +- .../command/handlers/TakeCartFromDrawPileHandler.kt | 2 +- 5 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/GameEventManager.kt b/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/GameEventManager.kt index 7e9b4bd..f829a33 100644 --- a/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/GameEventManager.kt +++ b/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/GameEventManager.kt @@ -37,16 +37,8 @@ abstract class GameEventManager( return this } - protected fun Game.isStatusOrFail( - kClass: KClass, - message: String, - ): G { - if (kClass.isInstance(this)) { - return this as G - } else { - throw CommandException(message) - } - } + protected inline fun Game.isStatusOrFail(message: String): G = + this as? G ?: throw CommandException(message) protected fun retry( mapAttempts: Int = 5, diff --git a/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/JoinTheGameHandler.kt b/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/JoinTheGameHandler.kt index c006f22..68fb8d7 100644 --- a/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/JoinTheGameHandler.kt +++ b/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/JoinTheGameHandler.kt @@ -20,7 +20,7 @@ class JoinTheGameHandler( retry { command .getGame() - .isStatusOrFail(GameCreated::class, "The game is started") + .isStatusOrFail("The game is started") .userJoinTheGame( userId = command.userId, name = user.username, diff --git a/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/PlayCardHandler.kt b/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/PlayCardHandler.kt index c047722..9a0c057 100644 --- a/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/PlayCardHandler.kt +++ b/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/PlayCardHandler.kt @@ -16,7 +16,7 @@ class PlayCardHandler( override fun handle(command: PlayCardCommand) { command .getGame() - .isStatusOrFail(GameStarted::class, "The game is not started") + .isStatusOrFail("The game is not started") .playTheCard( card = command.payload.card, playerId = command.payload.playerId, diff --git a/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/ReadyToPlayHandler.kt b/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/ReadyToPlayHandler.kt index 28c21e8..723e532 100644 --- a/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/ReadyToPlayHandler.kt +++ b/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/ReadyToPlayHandler.kt @@ -16,7 +16,7 @@ class ReadyToPlayHandler( override fun handle(command: ReadyToPlayCommand) { command .getGame() - .isStatusOrFail(GameCreated::class, "The game is started") + .isStatusOrFail("The game is started") .setReadyPlayer(command.payload.playerId) .saveEvents() .publishEvents() diff --git a/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/TakeCartFromDrawPileHandler.kt b/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/TakeCartFromDrawPileHandler.kt index 2b7f92f..b35b8e3 100644 --- a/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/TakeCartFromDrawPileHandler.kt +++ b/src/main/kotlin/eventDemo/contexts/game/application/command/handlers/TakeCartFromDrawPileHandler.kt @@ -18,7 +18,7 @@ class TakeCartFromDrawPileHandler( override fun handle(command: TakeCartFromDrawPileCommand) { command .getGame() - .isStatusOrFail(GameStarted::class, "The game is not started") + .isStatusOrFail("The game is not started") .playerTakeCartFromDrawPile(command.payload.playerId, 1) .saveEvents() .publishEvents()