1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 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\Bundle\Tests\TestBundle\Entity; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessToken; |
17
|
|
|
use OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenId; |
18
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
19
|
|
|
|
20
|
|
|
class InitialAccessTokenRepository implements \OAuth2Framework\Component\ClientRegistrationEndpoint\InitialAccessTokenRepository |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var AdapterInterface |
24
|
|
|
*/ |
25
|
|
|
private $cache; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* InitialAccessTokenRepository constructor. |
29
|
|
|
* |
30
|
|
|
* @param AdapterInterface $cache |
31
|
|
|
*/ |
32
|
|
|
public function __construct(AdapterInterface $cache) |
33
|
|
|
{ |
34
|
|
|
$this->cache = $cache; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function find(InitialAccessTokenId $initialAccessTokenId): ? InitialAccessToken |
41
|
|
|
{ |
42
|
|
|
$initialAccessToken = $this->getFromCache($initialAccessTokenId); |
43
|
|
|
|
44
|
|
|
return $initialAccessToken; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
public function save(InitialAccessToken $initialAccessToken) |
51
|
|
|
{ |
52
|
|
|
$initialAccessToken->eraseMessages(); |
53
|
|
|
$this->cacheObject($initialAccessToken); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param InitialAccessTokenId $initialAccessTokenId |
58
|
|
|
* |
59
|
|
|
* @return InitialAccessToken|null |
60
|
|
|
*/ |
61
|
|
|
private function getFromCache(InitialAccessTokenId $initialAccessTokenId): ? InitialAccessToken |
62
|
|
|
{ |
63
|
|
|
$itemKey = sprintf('oauth2-initial_access_token-%s', $initialAccessTokenId->getValue()); |
64
|
|
|
$item = $this->cache->getItem($itemKey); |
65
|
|
|
if ($item->isHit()) { |
66
|
|
|
return $item->get(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param InitialAccessToken $initialAccessToken |
74
|
|
|
*/ |
75
|
|
|
private function cacheObject(InitialAccessToken $initialAccessToken) |
76
|
|
|
{ |
77
|
|
|
$itemKey = sprintf('oauth2-initial_access_token-%s', $initialAccessToken->getUserAccountId()->getValue()); |
78
|
|
|
$item = $this->cache->getItem($itemKey); |
79
|
|
|
$item->set($initialAccessToken); |
80
|
|
|
$item->tag(['oauth2_server', 'initial_access_token', $itemKey]); |
81
|
|
|
$this->cache->save($item); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|