InMemoryTokensRepository.php 740 Bytes
Newer Older
Jacob Priddy's avatar
Jacob Priddy committed
1
2
3
4
5
6
<?php


namespace Source\Gateways\Tokens;


Jacob Priddy's avatar
Jacob Priddy committed
7
use Carbon\Carbon;
Jacob Priddy's avatar
Jacob Priddy committed
8
9
10
11
12
13
14
15
use Source\Entities\Token;

class InMemoryTokensRepository implements TokensRepository {
    /**
     * @var Token[]
     */
    protected array $tokens = [];

Jacob Priddy's avatar
Jacob Priddy committed
16
17
    protected static int $id = 1;

Jacob Priddy's avatar
Jacob Priddy committed
18
19
    /** @inheritDoc */
    public function create(Token $token): Token {
Jacob Priddy's avatar
Jacob Priddy committed
20
        $token->setId(static::$id++);
Jacob Priddy's avatar
Jacob Priddy committed
21
22
23
24
25
26
        $this->tokens[] = $token;
        return $token;
    }


    /** @inheritDoc */
Jacob Priddy's avatar
Jacob Priddy committed
27
    public function findValidToken(string $tokenToMatch): ?Token {
Jacob Priddy's avatar
Jacob Priddy committed
28
        foreach ($this->tokens as $token) {
Jacob Priddy's avatar
Jacob Priddy committed
29
           if ($token->matches($tokenToMatch) && $token->isValidAtTime(Carbon::now())) {
Jacob Priddy's avatar
Jacob Priddy committed
30
31
32
33
34
35
36
               return $token;
           }
        }

        return null;
    }
}