Failed Conditions
Push — master ( ed3bfb...d044dc )
by Florent
06:10
created

InitialAccessTokenRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A find() 0 8 2
A createInitialAccessTokens() 0 15 1
A save() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\ServerBundle\Tests\TestBundle\Repository;
15
16
use Assert\Assertion;
17
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessToken as InitialAccessTokenInterface;
18
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenId;
19
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
20
use OAuth2Framework\ServerBundle\Tests\TestBundle\Entity\InitialAccessToken;
21
use Psr\Cache\CacheItemPoolInterface;
22
23
final class InitialAccessTokenRepository implements \OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenRepository
24
{
25
    /**
26
     * @var CacheItemPoolInterface
27
     */
28
    private $cache;
29
30
    public function __construct(CacheItemPoolInterface $cache)
31
    {
32
        $this->cache = $cache;
33
        $this->createInitialAccessTokens();
34
    }
35
36
    public function find(InitialAccessTokenId $initialAccessTokenId): ?InitialAccessTokenInterface
37
    {
38
        $item = $this->cache->getItem('InitialAccessToken-'.$initialAccessTokenId->getValue());
39
        if ($item->isHit()) {
40
            return $item->get();
41
        }
42
43
        return null;
44
    }
45
46
    public function save(InitialAccessTokenInterface $initialAccessToken): void
47
    {
48
        Assertion::isInstanceOf($initialAccessToken, InitialAccessToken::class, 'Unsupported entity');
49
        $item = $this->cache->getItem('InitialAccessToken-'.$initialAccessToken->getId()->getValue());
50
        $item->set($initialAccessToken);
51
        $this->cache->save($item);
52
    }
53
54
    private function createInitialAccessTokens()
55
    {
56
        $iat = new InitialAccessToken(
57
            new InitialAccessTokenId('VALID_INITIAL_ACCESS_TOKEN_ID'),
58
            new UserAccountId('john.1'),
59
            new \DateTimeImmutable('now +1 day')
60
        );
61
        $this->save($iat);
62
63
        $iat = new InitialAccessToken(
64
            new InitialAccessTokenId('EXPIRED_INITIAL_ACCESS_TOKEN_ID'),
65
            new UserAccountId('john.1'),
66
            new \DateTimeImmutable('now -1 day')
67
        );
68
        $this->save($iat);
69
    }
70
}
71