Completed
Pull Request — master (#18)
by Valentyn
02:52
created

AuthController   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 3
dl 0
loc 53
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A login() 0 8 1
1
<?php
2
3
namespace App\Controller;
4
5
use App\Entity\User;
6
use App\Repository\UserRepository;
7
use App\Request\User\AuthUserRequest;
8
use App\Request\User\RegisterUserRequest;
9
use App\Service\User\AuthService;
10
use App\Service\User\RegisterService;
11
use FOS\RestBundle\Controller\FOSRestController;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
14
use Swagger\Annotations as SWG;
15
use Symfony\Component\Routing\Annotation\Route;
16
use Nelmio\ApiDocBundle\Annotation\Model;
17
use Symfony\Component\Translation\TranslatorInterface;
18
19
class AuthController extends FOSRestController
20
{
21
    /**
22
     * Endpoint action to get Access Token for authentication.
23
     *
24
     * @Route("/api/auth/login", methods={"POST"});
25
     * @SWG\Parameter(
26
     *      name="credentials",
27
     *      in="formData",
28
     *      type="string",
29
     *      description="Credentials",
30
     *      required=true,
31
     *      @SWG\Schema(
32
     *          type="object",
33
     *          example={"username": "username", "password": "password"}
34
     *      )
35
     *  )
36
     * @SWG\Response(
37
     *      response=200,
38
     *      description="API Token for user",
39
     *      @SWG\Schema(
40
     *          type="object",
41
     *          example={"api_token": "_api_token_"},
42
     *          @SWG\Property(property="api_token", type="string", description="Api Access Token"),
43
     *      ),
44
     *  )
45
     * @SWG\Response(
46
     *      response=400,
47
     *      description="Bad Request",
48
     *      @SWG\Schema(
49
     *          type="object",
50
     *          example={"message": "message", "errors": "array of errors"},
51
     *          @SWG\Property(property="message", type="integer", description="Error description"),
52
     *          @SWG\Property(property="errors", type="array", description="Errors list", @SWG\Items),
53
     *      ),
54
     *  )
55
     * @SWG\Tag(name="Authentication")
56
     *
57
     * @throws \LogicException
58
     * @throws \Symfony\Component\HttpKernel\Exception\HttpException
59
     * @param $authUserRequest AuthUserRequest
60
     * @param $authService AuthService
61
     * @return array
62
     */
63 4
    public function login(AuthUserRequest $authUserRequest, AuthService $authService)
64
    {
65 4
        $this->denyAccessUnlessGranted('IS_AUTHENTICATED_ANONYMOUSLY');
66
67 4
        $apiToken = $authService->getTokenByRequest($authUserRequest);
68
69 2
        return ['api_token' => $apiToken->getToken()];
70
    }
71
}