Completed
Pull Request — master (#17)
by Valentyn
13:24
created

UserController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 39.13%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 113
ccs 9
cts 23
cp 0.3913
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A login() 0 4 1
A getUsers() 0 18 2
A __construct() 0 5 1
A postUsers() 0 6 1
A getAll() 0 12 1
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\User;
6
use App\Repository\UserRepository;
7
use App\Request\User\RegisterUserRequest;
8
use App\Service\User\RegisterService;
9
use FOS\RestBundle\Controller\FOSRestController;
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
11
use Swagger\Annotations as SWG;
12
use Symfony\Component\Routing\Annotation\Route;
13
use Nelmio\ApiDocBundle\Annotation\Model;
14
use Symfony\Component\Translation\TranslatorInterface;
15
16
class UserController extends FOSRestController
17
{
18
    /**
19
     * @var RegisterService
20
     */
21
    private $registerService;
22
23
    private $translator;
24
25 3
    public function __construct(RegisterService $registerService, TranslatorInterface $translator)
26
    {
27 3
        $this->registerService = $registerService;
28 3
        $this->translator = $translator;
29 3
    }
30
31
    /**
32
     * Registration
33
     *
34
     * @Route("/api/users", methods={"POST"})
35
     * @SWG\Parameter(name="registration.username", in="formData", type="string")
36
     * @SWG\Parameter(name="registration.password", in="formData", type="string")
37
     * @SWG\Parameter(name="registration.email", in="formData", type="string")
38
     * @SWG\Response(
39
     *     description="Registration.",
40
     *     response=202,
41
     *     @Model(type=User::class)
42
     * )
43
     * @param $request RegisterUserRequest
44
     * @return User
45
     */
46 1
    public function postUsers(RegisterUserRequest $request)
47
    {
48 1
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_ANONYMOUSLY');
49
50 1
        return $this->registerService->registerByRequest($request);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->registerService->...terByRequest($request); of type Symfony\Component\HttpFo...esponse|App\Entity\User adds the type Symfony\Component\HttpFoundation\JsonResponse to the return on line 50 which is incompatible with the return type documented by App\Controller\UserController::postUsers of type App\Entity\User.
Loading history...
51
    }
52
53
    /**
54
     * Authentication
55
     *
56
     * @Route("/oauth/v2/token", methods={"POST"})
57
     * @SWG\Parameter(name="username", in="formData", type="string")
58
     * @SWG\Parameter(name="password", in="formData", type="string")
59
     * @SWG\Response(
60
     *     description="Authentication.",
61
     *     response=202
62
     * )
63
     */
64
    public function login()
65
    {
66
        throw new NotFoundHttpException($this->translator->trans('wrong_action', [], 'exceptions'));
67
    }
68
69
    /**
70
     * Get single user
71
     *
72
     * @Route("/api/users/{id}", methods={"GET"})
73
     * @SWG\Response(
74
     *     description="REST action which returns user by id.",
75
     *     response=200,
76
     *     @Model(type=User::class)
77
     * )
78
     *
79
     * @param int $id
80
     * @return User
81
     */
82
    public function getUsers($id)
83
    {
84
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
85
86
        /**
87
         * @var $userRepository UserRepository
88
         */
89
        $userRepository = $this->getDoctrine()->getRepository(User::class);
90
        $user = $userRepository->find($id);
91
92
        if ($user === null) {
93
            throw new NotFoundHttpException($this->translator->trans('not_found_by_id', [
94
                'user_id' => $id,
95
            ], 'users'));
96
        }
97
98
        return $user;
99
    }
100
101
    /**
102
     * Get all users
103
     *
104
     * @Route("/api/users", methods={"GET"})
105
     * @SWG\Response(
106
     *     description="REST action which returns user by id.",
107
     *     response=201,
108
     *     @SWG\Schema(
109
     *         type="array",
110
     *         @SWG\Items(ref=@Model(type=User::class, groups={"full"}))
111
     *     )
112
     * )
113
     *
114
     * @return User[]
115
     */
116 1
    public function getAll()
117
    {
118 1
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
119
120
        /**
121
         * @var $userRepository UserRepository
122
         */
123
        $userRepository = $this->getDoctrine()->getRepository(User::class);
124
        $users = $userRepository->findAll();
125
126
        return $users;
127
    }
128
}