Passed
Pull Request — master (#26)
by
unknown
03:27
created

AuthController::logout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Auth;
6
7
use App\User\UserRequest;
8
use App\User\UserService;
9
use Psr\Http\Message\ResponseInterface;
10
use Yiisoft\DataResponse\DataResponseFactoryInterface as ResponseFactory;
11
use OpenApi\Annotations as OA;
12
13
/**
14
 * @OA\Tag(
15
 *     name="auth",
16
 *     description="Authentication"
17
 * )
18
 */
19
final class AuthController
20
{
21
    private ResponseFactory $responseFactory;
22
    private UserService $userService;
23
24 2
    public function __construct(
25
        ResponseFactory $responseFactory,
26
        UserService $userService
27
    ) {
28 2
        $this->responseFactory = $responseFactory;
29 2
        $this->userService = $userService;
30 2
    }
31
32
    /**
33
     * @OA\Post(
34
     *     tags={"auth"},
35
     *     path="/auth/",
36
     *     summary="Authenticate by params",
37
     *     description="",
38
     *     @OA\Response(
39
     *          response="200",
40
     *          description="Success",
41
     *          @OA\JsonContent(
42
     *              allOf={
43
     *                  @OA\Schema(ref="#/components/schemas/Response"),
44
     *                  @OA\Schema(
45
     *                      @OA\Property(
46
     *                          property="data",
47
     *                          type="object",
48
     *                          @OA\Property(property="token", format="string", example="uap4X5Bd7078lxIFvxAflcGAa5D95iSSZkNjg3XFrE2EBRBlbj"),
49
     *                      ),
50
     *                  ),
51
     *              },
52
     *          )
53
     *    ),
54
     *    @OA\Response(
55
     *          response="400",
56
     *          description="Bad request",
57
     *          @OA\JsonContent(ref="#/components/schemas/BadResponse")
58
     *     ),
59
     *     @OA\RequestBody(
60
     *          required=true,
61
     *          @OA\MediaType(
62
     *              mediaType="application/json",
63
     *              @OA\Schema(ref="#/components/schemas/AuthRequest"),
64
     *          ),
65
     *     ),
66
     * )
67
     */
68 1
    public function login(AuthRequest $request): ResponseInterface
69
    {
70 1
        return $this->responseFactory->createResponse(
71
            [
72 1
                'token' => $this->userService->login(
73 1
                    $request->getLogin(),
74 1
                    $request->getPassword()
75 1
                )->getToken(),
0 ignored issues
show
Bug introduced by
The method getToken() does not exist on Yiisoft\Auth\IdentityInterface. It seems like you code against a sub-type of Yiisoft\Auth\IdentityInterface such as App\User\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

75
                )->/** @scrutinizer ignore-call */ getToken(),
Loading history...
76
            ]
77
        );
78
    }
79
80
    /**
81
     * @OA\Post(
82
     *     tags={"auth"},
83
     *     path="/logout/",
84
     *     summary="Logout",
85
     *     description="",
86
     *     @OA\Response(
87
     *          response="200",
88
     *          description="Success",
89
     *          @OA\JsonContent(ref="#/components/schemas/Response")
90
     *    ),
91
     *    @OA\Response(
92
     *          response="400",
93
     *          description="Bad request",
94
     *          @OA\JsonContent(ref="#/components/schemas/BadResponse")
95
     *     ),
96
     * )
97
     */
98 1
    public function logout(UserRequest $request): ResponseInterface
99
    {
100 1
        $this->userService->logout($request->getUser());
101 1
        return $this->responseFactory->createResponse();
102
    }
103
}
104