Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.75% |
15 / 16 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| GetCurrentUserAction | |
93.75% |
15 / 16 |
|
50.00% |
1 / 2 |
3.00 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
93.33% |
14 / 15 |
|
0.00% |
0 / 1 |
2.00 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Action\Auth; |
| 6 | |
| 7 | use App\Domain\Auth\Data\UserAuthData; |
| 8 | use App\Renderer\JsonRenderer; |
| 9 | use Psr\Http\Message\ResponseInterface; |
| 10 | use Psr\Http\Message\ServerRequestInterface; |
| 11 | use RuntimeException; |
| 12 | |
| 13 | /** |
| 14 | * Handles the retrieval of the currently authenticated user's information. |
| 15 | * |
| 16 | * This action is designed to return user details associated with the current |
| 17 | * request. It assumes that the user's authentication data is already included |
| 18 | * in the request attributes by middleware (e.g., JwtAuthMiddleware). |
| 19 | * |
| 20 | * If a valid user is found in the request attributes, the user data is returned |
| 21 | * as a JSON response. If no user is found or an error occurs during processing, |
| 22 | * an appropriate error response is returned. |
| 23 | * |
| 24 | * The returned response will have the following structure: |
| 25 | * - On success: |
| 26 | * { |
| 27 | * "success": true, |
| 28 | * "data": { |
| 29 | * "user_id": int, |
| 30 | * "investor_id": int, |
| 31 | * "username": string, |
| 32 | * "email": string, |
| 33 | * "role": string, |
| 34 | * "is_active": bool, |
| 35 | * "last_login": string|null |
| 36 | * } |
| 37 | * } |
| 38 | * - On failure: |
| 39 | * { |
| 40 | * "success": false, |
| 41 | * "error": string, |
| 42 | * "message": string |
| 43 | * } |
| 44 | * |
| 45 | * @param ServerRequestInterface $request the HTTP request instance that includes the |
| 46 | * authenticated user's data |
| 47 | * @param ResponseInterface $response the HTTP response instance used to send the |
| 48 | * result of the action |
| 49 | * |
| 50 | * @throws RuntimeException if the user attribute is not set or invalid |
| 51 | * |
| 52 | * @return ResponseInterface returns a JSON-encoded response that represents the current user's |
| 53 | * details on success, or an error message on failure |
| 54 | */ |
| 55 | final readonly class GetCurrentUserAction |
| 56 | { |
| 57 | public function __construct( |
| 58 | private JsonRenderer $renderer |
| 59 | ) {} |
| 60 | public function __invoke( |
| 61 | ServerRequestInterface $request, |
| 62 | ResponseInterface $response, |
| 63 | ): ResponseInterface { |
| 64 | $user = $request->getAttribute('user'); |
| 65 | |
| 66 | if (!$user instanceof UserAuthData) { |
| 67 | throw new RuntimeException('User not authenticated'); |
| 68 | } |
| 69 | |
| 70 | return $this->renderer->json($response, [ |
| 71 | 'success' => true, |
| 72 | 'data' => [ |
| 73 | 'userId' => $user->userId, |
| 74 | 'investorId' => $user->investorId, |
| 75 | 'username' => $user->username, |
| 76 | 'email' => $user->email, |
| 77 | 'role' => $user->role, |
| 78 | 'isActive' => $user->isActive, |
| 79 | 'lastLogin' => $user->lastLogin, |
| 80 | ], |
| 81 | ]); |
| 82 | } |
| 83 | } |