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\AuthorizationCodeGrant\AuthorizationCode; |
17
|
|
|
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeId; |
18
|
|
|
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeRepository as AuthorizationCodeRepositoryInterface; |
19
|
|
|
use Symfony\Component\Cache\Adapter\AdapterInterface; |
20
|
|
|
|
21
|
|
|
class AuthorizationCodeRepository implements AuthorizationCodeRepositoryInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var AdapterInterface |
25
|
|
|
*/ |
26
|
|
|
private $cache; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* AuthCodeRepository constructor. |
30
|
|
|
* |
31
|
|
|
* @param AdapterInterface $cache |
32
|
|
|
*/ |
33
|
|
|
public function __construct(AdapterInterface $cache) |
34
|
|
|
{ |
35
|
|
|
$this->cache = $cache; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
|
|
public function find(AuthorizationCodeId $authCodeId): ? AuthorizationCode |
42
|
|
|
{ |
43
|
|
|
$authCode = $this->getFromCache($authCodeId); |
44
|
|
|
|
45
|
|
|
return $authCode; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param AuthorizationCode $authCode |
50
|
|
|
*/ |
51
|
|
|
public function save(AuthorizationCode $authCode) |
52
|
|
|
{ |
53
|
|
|
$authCode->eraseMessages(); |
54
|
|
|
$this->cacheObject($authCode); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param AuthorizationCodeId $authCodeId |
59
|
|
|
* |
60
|
|
|
* @return AuthorizationCode|null |
61
|
|
|
*/ |
62
|
|
|
private function getFromCache(AuthorizationCodeId $authCodeId): ? AuthorizationCode |
63
|
|
|
{ |
64
|
|
|
$itemKey = sprintf('oauth2-auth_code-%s', $authCodeId->getValue()); |
65
|
|
|
$item = $this->cache->getItem($itemKey); |
66
|
|
|
if ($item->isHit()) { |
67
|
|
|
return $item->get(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param AuthorizationCode $authCode |
75
|
|
|
*/ |
76
|
|
|
private function cacheObject(AuthorizationCode $authCode) |
77
|
|
|
{ |
78
|
|
|
$itemKey = sprintf('oauth2-auth_code-%s', $authCode->getTokenId()->getValue()); |
79
|
|
|
$item = $this->cache->getItem($itemKey); |
80
|
|
|
$item->set($authCode); |
81
|
|
|
$item->tag(['oauth2_server', 'auth_code', $itemKey]); |
82
|
|
|
$this->cache->save($item); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|