Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| LogoutAction | |
100.00% |
13 / 13 |
|
100.00% |
2 / 2 |
3 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| __invoke | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Action\Auth; |
| 6 | |
| 7 | use App\Domain\Auth\Service\AuthService; |
| 8 | use InvalidArgumentException; |
| 9 | use Psr\Http\Message\ResponseInterface; |
| 10 | use Psr\Http\Message\ServerRequestInterface; |
| 11 | |
| 12 | /** |
| 13 | * Logout user. |
| 14 | * |
| 15 | * POST /api/auth/logout |
| 16 | * Requires: JWT authentication |
| 17 | */ |
| 18 | final class LogoutAction |
| 19 | { |
| 20 | private AuthService $authService; |
| 21 | |
| 22 | public function __construct(AuthService $authService) |
| 23 | { |
| 24 | $this->authService = $authService; |
| 25 | } |
| 26 | |
| 27 | public function __invoke( |
| 28 | ServerRequestInterface $request, |
| 29 | ResponseInterface $response, |
| 30 | ): ResponseInterface { |
| 31 | // Get request data |
| 32 | $data = (array)$request->getParsedBody(); |
| 33 | |
| 34 | // Validate required fields |
| 35 | if (empty($data['refreshToken'])) { |
| 36 | throw new InvalidArgumentException('Missing required field: refreshToken'); |
| 37 | } |
| 38 | |
| 39 | // logout (revoke refresh token) |
| 40 | $this->authService->logout($data['refreshToken']); |
| 41 | |
| 42 | // Return success response |
| 43 | $responseData = [ |
| 44 | 'success' => true, |
| 45 | 'message' => 'Logout successful', |
| 46 | ]; |
| 47 | |
| 48 | $response->getBody()->write((string)json_encode($responseData)); |
| 49 | |
| 50 | return $response |
| 51 | ->withHeader('Content-Type', 'application/json') |
| 52 | ->withStatus(200); |
| 53 | } |
| 54 | } |