66
77use App \Infrastructure \ClientIdCookieSubscriber ;
88use Symfony \Bundle \FrameworkBundle \Controller \AbstractController ;
9+ use Symfony \Component \HttpFoundation \Exception \SessionNotFoundException ;
910use Symfony \Component \HttpFoundation \JsonResponse ;
1011use Symfony \Component \HttpFoundation \Request ;
1112use Symfony \Component \HttpFoundation \Response ;
1213use Symfony \Component \Routing \Attribute \Route ;
1314
1415class Controller extends AbstractController
1516{
17+ private const SESSION_TOKEN_KEY = 'airlock.redis_lottery.token ' ;
18+
1619 #[Route('/redis-lottery-queue ' , methods: [Request::METHOD_GET ])]
1720 public function index (): Response
1821 {
@@ -39,17 +42,51 @@ public function success(): Response
3942 );
4043 }
4144
45+ #[Route('/redis-lottery-queue/release ' , methods: [Request::METHOD_POST ])]
46+ public function release (Request $ request , RedisLotteryQueueService $ service ): Response
47+ {
48+ try {
49+ $ session = $ request ->getSession ();
50+ } catch (SessionNotFoundException ) {
51+ return new JsonResponse (['ok ' => false , 'error ' => 'Session not available ' ], Response::HTTP_BAD_REQUEST );
52+ }
53+
54+ $ serializedToken = $ session ->get (self ::SESSION_TOKEN_KEY );
55+ if (!is_string ($ serializedToken ) || $ serializedToken === '' ) {
56+ return new JsonResponse (['ok ' => false , 'error ' => 'Missing airlock token ' ], Response::HTTP_BAD_REQUEST );
57+ }
58+
59+ $ service ->release ($ serializedToken );
60+ $ session ->remove (self ::SESSION_TOKEN_KEY );
61+
62+ return new JsonResponse (['ok ' => true ]);
63+ }
64+
4265 #[Route('/redis-lottery-queue/start ' , methods: [Request::METHOD_POST ])]
4366 public function start (Request $ request , RedisLotteryQueueService $ service ): JsonResponse
4467 {
4568 $ clientId = (string ) $ request ->attributes ->get (ClientIdCookieSubscriber::ATTRIBUTE );
69+
4670 $ result = $ service ->start ($ clientId );
4771
4872 if ($ result ->isAdmitted ()) {
73+ try {
74+ $ session = $ request ->getSession ();
75+ $ token = $ result ->getToken ();
76+ if ($ token !== null ) {
77+ $ session ->set (self ::SESSION_TOKEN_KEY , (string ) $ token );
78+ }
79+ } catch (SessionNotFoundException ) {
80+ // Session not available; continue without persisting token.
81+ }
82+
4983 return new JsonResponse ([
5084 'ok ' => true ,
5185 'status ' => 'admitted ' ,
5286 'clientId ' => $ clientId ,
87+ 'topic ' => $ service ->getTopic ($ clientId ),
88+ 'hubUrl ' => $ service ->getHubUrl (),
89+ 'token ' => $ service ->getSubscriberToken ($ clientId ),
5390 ]);
5491 }
5592
@@ -58,6 +95,9 @@ public function start(Request $request, RedisLotteryQueueService $service): Json
5895 'status ' => 'queued ' ,
5996 'position ' => $ result ->getPosition (),
6097 'clientId ' => $ clientId ,
98+ 'topic ' => $ service ->getTopic ($ clientId ),
99+ 'hubUrl ' => $ service ->getHubUrl (),
100+ 'token ' => $ service ->getSubscriberToken ($ clientId ),
61101 ]);
62102 }
63103}
0 commit comments