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

Authentications::jwt()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 2
nop 0
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
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