Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
11 / 12
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteUserController
91.67% covered (success)
91.67%
11 / 12
50.00% covered (danger)
50.00%
1 / 2
5.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 delete
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
4.01
1<?php
2
3declare(strict_types=1);
4
5namespace App\Identity\Infrastructure\Controllers\Admin;
6
7use App\Identity\Application\Command\DeleteUserByAdminCommand;
8use App\Identity\Application\Command\DeleteUserByAdminHandler;
9use App\Identity\Domain\Exceptions\AdminSelfAction;
10use App\Identity\Domain\Exceptions\UserNotFound;
11use App\Identity\Infrastructure\Http\ResolvesAuthenticatedUser;
12use App\Identity\Infrastructure\Http\ValidatesUserId;
13use OpenApi\Attributes as OA;
14use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
15use Symfony\Component\HttpFoundation\JsonResponse;
16use Symfony\Component\HttpFoundation\Response;
17use Symfony\Component\Routing\Attribute\Route;
18
19#[OA\Tag(name: 'Admin')]
20class 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}