LoginApplication::run()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 46
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 35
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 46
ccs 35
cts 35
cp 1
rs 9.456
c 0
b 0
f 0
cc 2
nc 2
nop 3
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Uxmp\Core\Api\Common;
6
7
use DateTimeImmutable;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Uxmp\Core\Api\AbstractApiApplication;
11
use Uxmp\Core\Api\Lib\Message\JsonEnabledResponseInterface;
12
use Uxmp\Core\Api\Lib\SchemaValidatorInterface;
13
use Uxmp\Core\Component\Authentication\JwtManagerInterface;
14
use Uxmp\Core\Component\Authentication\SessionManagerInterface;
15
use Uxmp\Core\Component\Config\ConfigProviderInterface;
16
17
final class LoginApplication extends AbstractApiApplication
18
{
19
    /**
20
     * @param SchemaValidatorInterface<array{username: string, password: string}> $schemaValidator
21
     */
22 2
    public function __construct(
23
        private readonly JwtManagerInterface $jwtManager,
24
        private readonly ConfigProviderInterface $configProvider,
25
        private readonly SessionManagerInterface $sessionManager,
26
        private readonly SchemaValidatorInterface $schemaValidator,
27
    ) {
28 2
    }
29
30 2
    protected function run(
31
        ServerRequestInterface $request,
32
        JsonEnabledResponseInterface $response,
33
        array $args
34
    ): ResponseInterface {
35 2
        $body = $this->schemaValidator->getValidatedBody(
36 2
            $request,
37 2
            'Login.json'
38 2
        );
39
40 2
        $session = $this->sessionManager->login(
41 2
            $body['username'],
42 2
            $body['password'],
43 2
        );
44
45 2
        if ($session === null) {
46 1
            return $response->withJson(
47 1
                ['data' => ['msg' => 'Username or password wrong']]
48 1
            );
49
        }
50
51 1
        $tokenDate = new DateTimeImmutable();
52 1
        $expiryDate = new DateTimeImmutable(sprintf('@%d', time() + $this->configProvider->getTokenLifetime()));
53
54 1
        $token = $this->jwtManager->encode($tokenDate, $expiryDate, (string) $session->getId());
55
56 1
        $user = $session->getUser();
57
58 1
        return $response
59 1
            ->withHeader(
60 1
                'Set-Cookie',
61 1
                sprintf(
62 1
                    '%s=%s; path=%s/play; Expires=%s',
63 1
                    $this->configProvider->getCookieName(),
64 1
                    $token,
65 1
                    $this->configProvider->getApiBasePath(),
66 1
                    $expiryDate->format(DATE_RFC1123),
67 1
                )
68 1
            )
69 1
            ->withJson([
70 1
                'data' => [
71 1
                    'token' => $token,
72 1
                    'user' => [
73 1
                        'id' => $user->getId(),
74 1
                        'name' => $user->getName(),
75 1
                        'language' => $user->getLanguage(),
76 1
                    ],
77 1
                ],
78 1
            ]);
79
    }
80
}
81