Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
UsersController
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createHandler
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 listHandler
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
 getHandler
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace App\Controller;
6
7use App\Dto\CreateUser;
8use App\Response\UserResponse;
9use App\Service\UserService;
10use Fig\Http\Message\RequestMethodInterface as RequestMethod;
11use Fig\Http\Message\StatusCodeInterface as StatusCode;
12use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
13use Symfony\Component\HttpFoundation\JsonResponse;
14use Symfony\Component\HttpFoundation\Request;
15use Symfony\Component\Routing\Attribute\Route;
16
17class UsersController extends AbstractController
18{
19    public function __construct(protected UserService $userService)
20    {
21    }
22
23    #[Route(
24        path: '/api/users',
25        name: 'api_users_create',
26        methods: [RequestMethod::METHOD_POST],
27    )]
28    public function createHandler(Request $request): JsonResponse
29    {
30        $jsonData = $request->getPayload()->all();
31        $createUserDto = new CreateUser($jsonData['name'], $jsonData['email']);
32
33        $user = $this
34            ->userService
35            ->create($createUserDto)
36        ;
37
38        return $this->json(new UserResponse($user), StatusCode::STATUS_CREATED);
39    }
40
41    #[Route(
42        path: '/api/users',
43        name: 'api_users_list',
44        methods: [RequestMethod::METHOD_GET],
45    )]
46    public function listHandler(Request $request): JsonResponse
47    {
48        $nameCriteria = $request->get('nameCriteria', null);
49        $emailCriteria = $request->get('emailCriteria', null);
50
51        $users = $this
52            ->userService
53            ->list($nameCriteria, $emailCriteria)
54        ;
55
56        $response = [];
57        foreach ($users as $user) {
58            $response[] = new UserResponse($user);
59        }
60
61        return $this->json($response);
62    }
63
64    #[Route(
65        path: '/api/users/{id}',
66        name: 'api_users_get',
67        requirements: ['id' => '\d+'],
68        methods: [RequestMethod::METHOD_GET],
69    )]
70    public function getHandler(int $id): JsonResponse
71    {
72        $user = $this
73            ->userService
74            ->getById($id)
75        ;
76
77        return $this->json(new UserResponse($user));
78    }
79}