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

AccessTokenRepository   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 33
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 3 1
A save() 0 6 1
A find() 0 8 2
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\Core\AccessToken\AccessToken as AccessTokenInterface;
18
use OAuth2Framework\Component\Core\AccessToken\AccessTokenId;
19
use OAuth2Framework\Component\Core\AccessToken\AccessTokenRepository as AccessTokenRepositoryInterface;
20
use OAuth2Framework\Component\Core\Client\ClientId;
21
use OAuth2Framework\Component\Core\DataBag\DataBag;
22
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
23
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
24
use OAuth2Framework\ServerBundle\Tests\TestBundle\Entity\AccessToken;
25
use Psr\Cache\CacheItemPoolInterface;
26
27
final class AccessTokenRepository implements AccessTokenRepositoryInterface
28
{
29
    /**
30
     * @var CacheItemPoolInterface
31
     */
32
    private $cache;
33
34
    public function __construct(CacheItemPoolInterface $cache)
35
    {
36
        $this->cache = $cache;
37
    }
38
39
    public function find(AccessTokenId $accessTokenId): ?AccessTokenInterface
40
    {
41
        $item = $this->cache->getItem('AccessToken-'.$accessTokenId->getValue());
42
        if ($item->isHit()) {
43
            return $item->get();
44
        }
45
46
        return null;
47
    }
48
49
    public function save(AccessTokenInterface $accessToken): void
50
    {
51
        Assertion::isInstanceOf($accessToken, AccessToken::class, 'Unsupported access token class');
52
        $item = $this->cache->getItem('AccessToken-'.$accessToken->getId()->getValue());
53
        $item->set($accessToken);
54
        $this->cache->save($item);
55
    }
56
57
    public function create(ClientId $clientId, ResourceOwnerId $resourceOwnerId, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId): AccessTokenInterface
58
    {
59
        return new AccessToken(new AccessTokenId(bin2hex(random_bytes(32))), $clientId, $resourceOwnerId, $expiresAt, $parameter, $metadata, $resourceServerId);
60
    }
61
}
62