Completed
Push — master ( 712d21...572f95 )
by Valentyn
05:22
created

UserController::getUsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 10
nc 2
nop 2
crap 2
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
     * @param RegisterUserRequest $request
28
     * @param RegisterService $registerService
29
     * @param EventDispatcherInterface $dispatcher
30
     * @param ValidatorInterface $validator
31
     * @return JsonResponse|\Symfony\Component\HttpFoundation\Response
32
     */
33 2
    public function postUsers(RegisterUserRequest $request, RegisterService $registerService, EventDispatcherInterface $dispatcher, ValidatorInterface $validator)
34
    {
35 2
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_ANONYMOUSLY'); // todo (not working)
36
37 2
        $registeredUser = $registerService->registerByRequest($request);
38 2
        $errors = $validator->validate($registeredUser);
39
40 2
        if ($errors && 0 !== $errors->count()) {
41
            return $request->getErrorResponse($errors);
42
        }
43
44 2
        $em = $this->getDoctrine()->getManager();
45 2
        $em->persist($registeredUser);
46 2
        $em->flush();
47
48 2
        $userRegisteredEvent = new UserRegisteredEvent($registeredUser);
49 2
        $dispatcher->dispatch(UserRegisteredEvent::NAME, $userRegisteredEvent);
50
51 2
        return $this->response($registeredUser, 200, [], [
52 2
            'groups' => ['view'],
53
        ]);
54
    }
55
56
    /**
57
     * Confirm email
58
     *
59
     * @Route("/api/confirmEmail", methods={"POST"})
60
     * @param ConfirmEmailRequest $request
61
     * @param ConfirmationTokenRepository $confirmationTokenRepository
62
     * @param TranslatorInterface $translator
63
     * @return JsonResponse
64
     */
65 2
    public function postConfirmEmail(ConfirmEmailRequest $request, ConfirmationTokenRepository $confirmationTokenRepository, TranslatorInterface $translator)
66
    {
67 2
        $token = $request->get('token');
68
69 2
        if (null === $confirmationToken = $confirmationTokenRepository->findByToken($token)) {
70 1
            throw new BadCredentialsException($translator->trans('bad_email_confirmation_token', [
71 1
                'token' => $token,
72 1
            ], 'users'));
73
        }
74
75 1
        $user = $confirmationToken->getUser();
76 1
        $user->confirmEmail();
77
78 1
        $entityManager = $this->getDoctrine()->getManager();
79 1
        $entityManager->persist($user);
80 1
        $entityManager->remove($confirmationToken);
81 1
        $entityManager->flush();
82
83 1
        return new JsonResponse(null, 202);
84
    }
85
86
    /**
87
     * Get single user
88
     *
89
     * @Route("/api/users/{id}", methods={"GET"})
90
     * @param $id
91
     * @param TranslatorInterface $translator
92
     * @return JsonResponse
93
     */
94 3
    public function getUsers($id, TranslatorInterface $translator)
95
    {
96 3
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
97
98
        /**
99
         * @var $userRepository \App\Users\Repository\UserRepository
100
         */
101 2
        $userRepository = $this->getDoctrine()->getRepository(User::class);
102 2
        $user = $userRepository->find($id);
103
104 2
        if ($user === null) {
105 1
            throw new NotFoundHttpException($translator->trans('not_found_by_id', [
106 1
                'user_id' => $id,
107 1
            ], 'users'));
108
        }
109
110 1
        return $this->response($user, 200, [], [
111 1
            'groups' => ['view'],
112
        ]);
113
    }
114
115
    /**
116
     * Get all users
117
     *
118
     * @Route("/api/users", methods={"GET"})
119
     *
120
     * @return User[]
121
     */
122 3
    public function getAll()
123
    {
124 3
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
125
126
        /**
127
         * @var $userRepository UserRepository
128
         */
129 2
        $userRepository = $this->getDoctrine()->getRepository(User::class);
130 2
        $users = $userRepository->findAll();
131
132 2
        return $this->response($users, 200, [], [
133 2
            'groups' => ['list'],
134
        ]);
135
    }
136
}