Passed
Push — master ( 4a5ade...edff29 )
by Guido
20:51
created

Authentications   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A jwt() 0 21 3
1
<?php
2
3
namespace Gvera\Controllers;
4
5
use Gvera\Helpers\http\JSONResponse;
6
use Gvera\Helpers\http\Response;
7
use Gvera\Models\User;
8
use Gvera\Services\JWTService;
9
10
class Authentications extends GvController
11
{
12
    /**
13
     * @httpMethod("POST")
14
     */
15
    public function jwt()
16
    {
17
        $username = $this->httpRequest->getParameter('username');
18
        $password = $this->httpRequest->getParameter('password');
19
20
        $user = $this->getEntityManager()->getRepository(User::class)->findOneBy(['username' => $username]);
21
        $userService = $this->getUserService();
22
        if (null === $user || !$userService->validatePassword($password, $user->getPassword())) {
23
            $this->httpResponse->response(
24
                new JSONResponse(
25
                    [],
26
                    Response::HTTP_RESPONSE_UNAUTHORIZED,
27
                    Response::BEARER_AUTH_ACCESS_DENIED
28
                )
29
            );
30
            return;
31
        }
32
33
        $service = $this->getJwtService();
34
        $this->httpResponse->response(new JSONResponse(['token' => $service->createToken($user)]));
35
    }
36
}
37