Login::login()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 24
ccs 15
cts 15
cp 1
crap 4
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Service\User;
6
7
use Firebase\JWT\JWT;
8
9
final class Login extends Base
10
{
11
    public function login(array $input): string
12 6
    {
13
        $data = json_decode((string) json_encode($input), false);
14 6
        if (!isset($data->email)) {
15 6
            throw new \App\Exception\UserException('The field "email" is required.', 400);
16 1
        }
17
        if (!isset($data->password)) {
18 5
            throw new \App\Exception\UserException('The field "password" is required.', 400);
19 1
        }
20
        $password = hash('sha512', $data->password);
21 4
        $user = $this->userRepository->login($data->email, $password);
22 4
        $token = [
23
            'sub' => $user->getId(),
24 3
            'email' => $user->getEmail(),
25 3
            'name' => $user->getName(),
26 3
            'iat' => time(),
27 3
            'exp' => time() + (7 * 24 * 60 * 60),
28 3
        ];
29
30
        if (self::isLoggerEnabled() === true) {
31 3
            $this->loggerService->setInfo('The user with the ID ' . $user->getId() . ' has logged in successfully.');
32 3
        }
33
34
        return JWT::encode($token, $_SERVER['SECRET_KEY']);
0 ignored issues
show
Bug introduced by
The call to Firebase\JWT\JWT::encode() has too few arguments starting with alg. ( Ignorable by Annotation )

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

34
        return JWT::/** @scrutinizer ignore-call */ encode($token, $_SERVER['SECRET_KEY']);

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
35 3
    }
36
}
37