Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.00% covered (warning)
80.00%
8 / 10
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteExpenseController
80.00% covered (warning)
80.00%
8 / 10
50.00% covered (danger)
50.00%
1 / 2
6.29
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
77.78% covered (warning)
77.78%
7 / 9
0.00% covered (danger)
0.00%
0 / 1
5.27
1<?php
2
3declare(strict_types=1);
4
5namespace App\Expense\Infrastructure\Controllers;
6
7use App\Expense\Application\Command\DeleteExpenseCommand;
8use App\Expense\Application\Command\DeleteExpenseHandler;
9use App\Expense\Domain\Exceptions\ExpenseNotFound;
10use App\Expense\Infrastructure\Http\ValidatesExpenseId;
11use App\Identity\Infrastructure\Http\ResolvesAuthenticatedUser;
12use App\Trip\Domain\Exceptions\TripAccessDenied;
13use App\Trip\Domain\Exceptions\TripNotFound;
14use App\Trip\Infrastructure\Http\ValidatesTripId;
15use OpenApi\Attributes as OA;
16use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
17use Symfony\Component\HttpFoundation\JsonResponse;
18use Symfony\Component\HttpFoundation\Response;
19use Symfony\Component\Routing\Attribute\Route;
20
21#[OA\Tag(name: 'Expenses')]
22class DeleteExpenseController extends AbstractController
23{
24    use ResolvesAuthenticatedUser;
25    use ValidatesExpenseId;
26    use ValidatesTripId;
27
28    public function __construct(
29        private readonly DeleteExpenseHandler $handler,
30    ) {
31    }
32
33    #[Route('/trips/{tripId}/expenses/{id}', name: 'expenses_delete', methods: ['DELETE'])]
34    public function delete(string $tripId, string $id): Response
35    {
36        $securityUser = $this->securityUser();
37
38        if ($this->parseTripId($tripId) === null || $this->parseExpenseId($id) === null) {
39            return new JsonResponse(['error' => 'Expense not found.'], 404);
40        }
41
42        try {
43            $this->handler->handle(new DeleteExpenseCommand($tripId, $securityUser->userId(), $id));
44        } catch (ExpenseNotFound|TripNotFound $e) {
45            return new JsonResponse(['error' => $e->getMessage()], 404);
46        } catch (TripAccessDenied $e) {
47            return new JsonResponse(['error' => $e->getMessage()], 403);
48        }
49
50        return new Response(null, Response::HTTP_NO_CONTENT);
51    }
52}