Authenticate.php 1005 Bytes
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php

namespace Source\UseCases\Token\Authenticate;

use Source\Entities\User;
use Source\Gateways\Users\UsersRepository;
use Source\Gateways\Tokens\TokensRepository;

class Authenticate implements AuthenticateUseCase {
    protected TokensRepository $tokens;

    protected UsersRepository $users;

    /**
     * @param TokensRepository $tokens
     * @param UsersRepository  $users
     */
    public function __construct(TokensRepository $tokens, UsersRepository $users) {
        $this->tokens = $tokens;
        $this->users = $users;
    }


    /**
     * @inheritDoc
     */
    public function check(Presenter $presenter, ?string $token): void {
        if (!$token) {
            return;
        }

Jacob Priddy's avatar
Jacob Priddy committed
32
        $found = $this->tokens->findValidToken($token);
Jacob Priddy's avatar
Jacob Priddy committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46

        if (!$found) {
            return;
        }

        $response = new ResponseModel();

        $user = $this->users->get($found->getUserId());

        $response->setUser($user);

        $presenter->present($response);
    }
}