|
| 1 | +<?php |
| 2 | + |
| 3 | +// Copilot - Pending review |
| 4 | +// TEMPORARY TEST MIDDLEWARE - Remove after testing |
| 5 | + |
| 6 | +namespace App\Http\Middleware; |
| 7 | + |
| 8 | +use App\Services\Broadcast\DataObjects\Message; |
| 9 | +use App\Services\Broadcast\Enums\BroadcastMessage; |
| 10 | +use App\Services\BroadcastService; |
| 11 | +use Closure; |
| 12 | +use Illuminate\Http\Request; |
| 13 | +use Symfony\Component\HttpFoundation\Response; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test middleware to broadcast user navigation events. |
| 17 | + * |
| 18 | + * This is TEMPORARY and should be removed after testing. |
| 19 | + */ |
| 20 | +class BroadcastNavigationTest |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + protected BroadcastService $broadcast, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Handle an incoming request. |
| 29 | + * |
| 30 | + * @param Closure(Request): (Response) $next |
| 31 | + */ |
| 32 | + public function handle(Request $request, Closure $next): Response |
| 33 | + { |
| 34 | + $user = $request->user(); |
| 35 | + |
| 36 | + if ($user) { |
| 37 | + // Broadcast navigation to user's private channel |
| 38 | + $this->broadcast->toUser( |
| 39 | + $user->id, |
| 40 | + Message::make( |
| 41 | + BroadcastMessage::USER_NAVIGATION, |
| 42 | + [ |
| 43 | + 'url' => $request->fullUrl(), |
| 44 | + 'path' => $request->path(), |
| 45 | + 'method' => $request->method(), |
| 46 | + 'user_id' => $user->id, |
| 47 | + 'user_name' => $user->fullname, |
| 48 | + ], |
| 49 | + ), |
| 50 | + ); |
| 51 | + |
| 52 | + // If user has a current project, also broadcast to project channel |
| 53 | + $projectId = $request->route('project')?->id ?? session('current_project_id'); |
| 54 | + if ($projectId) { |
| 55 | + $this->broadcast->toProject( |
| 56 | + $projectId, |
| 57 | + Message::make( |
| 58 | + BroadcastMessage::USER_NAVIGATION, |
| 59 | + [ |
| 60 | + 'url' => $request->fullUrl(), |
| 61 | + 'path' => $request->path(), |
| 62 | + 'method' => $request->method(), |
| 63 | + 'user_id' => $user->id, |
| 64 | + 'user_name' => $user->fullname, |
| 65 | + 'project_id' => $projectId, |
| 66 | + ], |
| 67 | + ), |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return $next($request); |
| 73 | + } |
| 74 | +} |
0 commit comments