Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.67% |
11 / 12 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| DeleteUserController | |
91.67% |
11 / 12 |
|
50.00% |
1 / 2 |
5.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| delete | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
4.01 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Identity\Infrastructure\Controllers\Admin; |
| 6 | |
| 7 | use App\Identity\Application\Command\DeleteUserByAdminCommand; |
| 8 | use App\Identity\Application\Command\DeleteUserByAdminHandler; |
| 9 | use App\Identity\Domain\Exceptions\AdminSelfAction; |
| 10 | use App\Identity\Domain\Exceptions\UserNotFound; |
| 11 | use App\Identity\Infrastructure\Http\ResolvesAuthenticatedUser; |
| 12 | use App\Identity\Infrastructure\Http\ValidatesUserId; |
| 13 | use OpenApi\Attributes as OA; |
| 14 | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 15 | use Symfony\Component\HttpFoundation\JsonResponse; |
| 16 | use Symfony\Component\HttpFoundation\Response; |
| 17 | use Symfony\Component\Routing\Attribute\Route; |
| 18 | |
| 19 | #[OA\Tag(name: 'Admin')] |
| 20 | class DeleteUserController extends AbstractController |
| 21 | { |
| 22 | use ResolvesAuthenticatedUser; |
| 23 | use ValidatesUserId; |
| 24 | |
| 25 | public function __construct( |
| 26 | private readonly DeleteUserByAdminHandler $handler, |
| 27 | ) { |
| 28 | } |
| 29 | |
| 30 | /** Delete any user (cascades to their trips/expenses). An admin cannot delete their own account. */ |
| 31 | #[Route('/admin/users/{id}', name: 'admin_users_delete', methods: ['DELETE'])] |
| 32 | public function delete(string $id): Response |
| 33 | { |
| 34 | if ($this->parseUserId($id) === null) { |
| 35 | return new JsonResponse(['error' => 'User not found.'], 404); |
| 36 | } |
| 37 | |
| 38 | try { |
| 39 | $this->handler->handle(new DeleteUserByAdminCommand( |
| 40 | callerId: $this->securityUser()->userId(), |
| 41 | targetId: $id, |
| 42 | )); |
| 43 | } catch (UserNotFound $e) { |
| 44 | return new JsonResponse(['error' => $e->getMessage()], 404); |
| 45 | } catch (AdminSelfAction $e) { |
| 46 | return new JsonResponse(['error' => $e->getMessage()], 409); |
| 47 | } |
| 48 | |
| 49 | return new Response(null, Response::HTTP_NO_CONTENT); |
| 50 | } |
| 51 | } |