UserController::user()   A
last analyzed

Complexity

Conditions 2
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 9
c 0
b 0
f 0
rs 10
cc 2
nc 3
nop 1
1
<?php
2
3
namespace App\Project\Http\Controller;
4
5
use App\Project\App\Support\FractalService;
6
use App\Project\Domain\User\UserService;
7
use Doctrine\ORM\EntityNotFoundException;
8
use Symfony\Component\HttpFoundation\JsonResponse;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\Response;
11
use Symfony\Component\Routing\RouterInterface;
12
use Swagger\Annotations as SWG;
13
14
15
class UserController
16
{
17
18
    /**
19
     * @var UserService
20
     */
21
    private $userService;
22
    /**
23
     * @var RouterInterface
24
     */
25
    private $router;
26
    /**
27
     * @var FractalService
28
     */
29
    private $fractalService;
30
31
    public function __construct(
32
        UserService $userService,
33
        RouterInterface $router,
34
        FractalService $fractalService
35
    )
36
    {
37
38
        $this->userService = $userService;
39
        $this->router = $router;
40
        $this->fractalService = $fractalService;
41
    }
42
43
44
    /**
45
     * @param Request $request
46
     * @return JsonResponse
47
     * @SWG\Response(
48
     *     response=200,
49
     *     description="Returns users collection with pagination"
50
     * )
51
     * @SWG\Response(
52
     *     response=500,
53
     *     description="Returns error"
54
     * )
55
     * @SWG\Parameter(
56
     *     name="page",
57
     *     in="query",
58
     *     type="integer",
59
     *     description="current page",
60
     *     default="1"
61
     * )
62
     * @SWG\Parameter(
63
     *     name="per_page",
64
     *     in="query",
65
     *     type="integer",
66
     *     description="limit per page",
67
     *     default="10"
68
     * )
69
     * @SWG\Tag(name="users")
70
     */
71
    public function index(Request $request)
72
    {
73
        $resource = $this->userService->listUsers($request, $this->router);
74
75
        return new JsonResponse($this->fractalService->transform($resource));
76
    }
77
78
    /**
79
     * @param $id
80
     * @return JsonResponse
81
     * @SWG\Response(
82
     *     response=200,
83
     *     description="Returns single user Item"
84
     * )
85
     * @SWG\Response(
86
     *     response=204,
87
     *     description="Returns not found user"
88
     * )
89
     * @SWG\Tag(name="users")
90
     */
91
    public function user($id)
92
    {
93
94
        try {
95
            $user = $this->userService->getUserById($id);
96
            return new JsonResponse($this->fractalService->transform($user));
97
98
        }catch (EntityNotFoundException $exception) {
99
            return new JsonResponse($this->fractalService->transform($exception->getMessage(), false), Response::HTTP_NO_CONTENT);
100
        }
101
    }
102
}