Completed
Push — master ( 9d729c...78ea8b )
by Valentyn
03:06
created

UserController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 18

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 18
dl 0
loc 118
ccs 42
cts 42
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A postUsers() 0 22 3
A postConfirmEmail() 0 20 2
A getUsers() 0 18 2
A getAll() 0 14 1
1
<?php
2
3
namespace App\Users\Controller;
4
5
use App\Controller\BaseController;
6
use App\Users\Entity\User;
7
use App\Users\Event\UserRegisteredEvent;
8
use App\Users\Repository\ConfirmationTokenRepository;
9
use App\Users\Repository\UserRepository;
10
use App\Users\Request\ConfirmEmailRequest;
11
use App\Users\Request\RegisterUserRequest;
12
use App\Users\Service\RegisterService;
13
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
14
use Symfony\Component\HttpFoundation\JsonResponse;
15
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
16
use Symfony\Component\Routing\Annotation\Route;
17
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
18
use Symfony\Component\Translation\TranslatorInterface;
19
use Symfony\Component\Validator\Validator\ValidatorInterface;
20
21
class UserController extends BaseController
22
{
23
    /**
24
     * Registration.
25
     *
26
     * @Route("/api/users", methods={"POST"})
27
     *
28
     * @param RegisterUserRequest      $request
29
     * @param RegisterService          $registerService
30
     * @param EventDispatcherInterface $dispatcher
31
     * @param ValidatorInterface       $validator
32
     *
33
     * @return JsonResponse|\Symfony\Component\HttpFoundation\Response
34
     */
35 3
    public function postUsers(RegisterUserRequest $request, RegisterService $registerService, EventDispatcherInterface $dispatcher, ValidatorInterface $validator)
36
    {
37 3
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_ANONYMOUSLY'); // todo (not working)
38
39 3
        $registeredUser = $registerService->registerByRequest($request);
40 3
        $errors = $validator->validate($registeredUser);
41
42 3
        if ($errors && $errors->count() !== 0) {
43 1
            return $request->getErrorResponse($errors);
44
        }
45
46 2
        $em = $this->getDoctrine()->getManager();
47 2
        $em->persist($registeredUser);
48 2
        $em->flush();
49
50 2
        $userRegisteredEvent = new UserRegisteredEvent($registeredUser);
51 2
        $dispatcher->dispatch(UserRegisteredEvent::NAME, $userRegisteredEvent);
52
53 2
        return $this->response($registeredUser, 200, [], [
54 2
            'groups' => ['view'],
55
        ]);
56
    }
57
58
    /**
59
     * Confirm email.
60
     *
61
     * @Route("/api/confirmEmail", methods={"POST"})
62
     *
63
     * @param ConfirmEmailRequest         $request
64
     * @param ConfirmationTokenRepository $confirmationTokenRepository
65
     * @param TranslatorInterface         $translator
66
     *
67
     * @return JsonResponse
68
     */
69 2
    public function postConfirmEmail(ConfirmEmailRequest $request, ConfirmationTokenRepository $confirmationTokenRepository, TranslatorInterface $translator)
70
    {
71 2
        $token = $request->get('token');
72
73 2
        if (null === $confirmationToken = $confirmationTokenRepository->findByToken($token)) {
74 1
            throw new BadCredentialsException($translator->trans('bad_email_confirmation_token', [
75 1
                'token' => $token,
76 1
            ], 'users'));
77
        }
78
79 1
        $user = $confirmationToken->getUser();
80 1
        $user->confirmEmail();
81
82 1
        $entityManager = $this->getDoctrine()->getManager();
83 1
        $entityManager->persist($user);
84 1
        $entityManager->remove($confirmationToken);
85 1
        $entityManager->flush();
86
87 1
        return new JsonResponse(null, 202);
88
    }
89
90
    /**
91
     * Get single user.
92
     *
93
     * @Route("/api/users/{id}", methods={"GET"})
94
     *
95
     * @param $id
96
     * @param TranslatorInterface $translator
97
     *
98
     * @return JsonResponse
99
     */
100 3
    public function getUsers($id, TranslatorInterface $translator)
101
    {
102 3
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
103
104
        /** @var $userRepository \App\Users\Repository\UserRepository */
105 2
        $userRepository = $this->getDoctrine()->getRepository(User::class);
106 2
        $user = $userRepository->find($id);
107
108 2
        if ($user === null) {
109 1
            throw new NotFoundHttpException($translator->trans('not_found_by_id', [
110 1
                'user_id' => $id,
111 1
            ], 'users'));
112
        }
113
114 1
        return $this->response($user, 200, [], [
115 1
            'groups' => ['view'],
116
        ]);
117
    }
118
119
    /**
120
     * Get all users.
121
     *
122
     * @Route("/api/users", methods={"GET"})
123
     */
124 3
    public function getAll()
125
    {
126 3
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
127
128
        /**
129
         * @var UserRepository
130
         */
131 2
        $userRepository = $this->getDoctrine()->getRepository(User::class);
132 2
        $users = $userRepository->findAll();
133
134 2
        return $this->response($users, 200, [], [
135 2
            'groups' => ['list'],
136
        ]);
137
    }
138
}
139