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

RefreshTokenRepository::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 3
nop 0
dl 0
loc 16
rs 9.8666
c 0
b 0
f 0
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\Client\ClientId;
18
use OAuth2Framework\Component\Core\DataBag\DataBag;
19
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwnerId;
20
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
21
use OAuth2Framework\Component\Core\UserAccount\UserAccountId;
22
use OAuth2Framework\Component\RefreshTokenGrant\RefreshToken as RefreshTokenInterface;
23
use OAuth2Framework\Component\RefreshTokenGrant\RefreshTokenId;
24
use OAuth2Framework\Component\RefreshTokenGrant\RefreshTokenRepository as RefreshTokenRepositoryInterface;
25
use OAuth2Framework\ServerBundle\Tests\TestBundle\Entity\RefreshToken;
26
use Psr\Cache\CacheItemPoolInterface;
27
28
final class RefreshTokenRepository implements RefreshTokenRepositoryInterface
29
{
30
    /**
31
     * @var CacheItemPoolInterface
32
     */
33
    private $cache;
34
35
    public function __construct(CacheItemPoolInterface $cache)
36
    {
37
        $this->cache = $cache;
38
        $this->load();
39
    }
40
41
    public function find(RefreshTokenId $refreshTokenId): ?RefreshTokenInterface
42
    {
43
        $item = $this->cache->getItem('RefreshToken-'.$refreshTokenId->getValue());
44
        if ($item->isHit()) {
45
            return $item->get();
46
        }
47
48
        return null;
49
    }
50
51
    public function save(RefreshTokenInterface $refreshToken): void
52
    {
53
        Assertion::isInstanceOf($refreshToken, RefreshToken::class, 'Unsupported refresh token class');
54
        $item = $this->cache->getItem('RefreshToken-'.$refreshToken->getId()->getValue());
55
        $item->set($refreshToken);
56
        $this->cache->save($item);
57
    }
58
59
    public function create(ClientId $clientId, ResourceOwnerId $resourceOwnerId, \DateTimeImmutable $expiresAt, DataBag $parameter, DataBag $metadata, ?ResourceServerId $resourceServerId): RefreshTokenInterface
60
    {
61
        return new RefreshToken(
62
            new RefreshTokenId(bin2hex(random_bytes(32))),
63
            $clientId,
64
            $resourceOwnerId,
65
            $expiresAt,
66
            $parameter,
67
            $metadata,
68
            $resourceServerId
69
        );
70
    }
71
72
    private function load(): void
73
    {
74
        foreach ($this->getData() as $datum) {
75
            $refreshToken = new RefreshToken(
76
                new RefreshTokenId($datum['refresh_token_id']),
77
                new ClientId($datum['client_id']),
78
                $datum['resource_owner_id'],
79
                $datum['expires_at'],
80
                new DataBag($datum['parameter']),
81
                new DataBag($datum['metadata']),
82
                $datum['resource_server_id']
83
            );
84
            if ($datum['is_revoked']) {
85
                $refreshToken->markAsRevoked();
86
            }
87
            $this->save($refreshToken);
88
        }
89
    }
90
91
    private function getData(): array
92
    {
93
        return [
94
            [
95
                'refresh_token_id' => 'VALID_REFRESH_TOKEN',
96
                'client_id' => 'CLIENT_ID_3',
97
                'resource_owner_id' => new UserAccountId('john.1'),
98
                'expires_at' => new \DateTimeImmutable('now +1 day'),
99
                'parameter' => [],
100
                'metadata' => [],
101
                'resource_server_id' => null,
102
                'is_revoked' => false,
103
            ],
104
            [
105
                'refresh_token_id' => 'REVOKED_REFRESH_TOKEN',
106
                'client_id' => 'CLIENT_ID_3',
107
                'resource_owner_id' => new UserAccountId('john.1'),
108
                'expires_at' => new \DateTimeImmutable('now +1 day'),
109
                'parameter' => [],
110
                'metadata' => [],
111
                'resource_server_id' => null,
112
                'is_revoked' => true,
113
            ],
114
            [
115
                'refresh_token_id' => 'EXPIRED_REFRESH_TOKEN',
116
                'client_id' => 'CLIENT_ID_3',
117
                'resource_owner_id' => new UserAccountId('john.1'),
118
                'expires_at' => new \DateTimeImmutable('now -1 day'),
119
                'parameter' => [],
120
                'metadata' => [],
121
                'resource_server_id' => null,
122
                'is_revoked' => false,
123
            ],
124
        ];
125
    }
126
}
127