Completed
Pull Request — master (#26)
by Valentyn
02:41
created

UserController   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 73.52%

Importance

Changes 0
Metric Value
dl 0
loc 131
c 0
b 0
f 0
wmc 7
lcom 1
cbo 10
ccs 25
cts 34
cp 0.7352
rs 10

5 Methods

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