1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Lookyman\NetteOAuth2Server\Storage\Doctrine\AuthCode; |
5
|
|
|
|
6
|
|
|
use Doctrine\ORM\OptimisticLockException; |
7
|
|
|
use Doctrine\ORM\ORMInvalidArgumentException; |
8
|
|
|
use Kdyby\Doctrine\InvalidStateException; |
9
|
|
|
use Kdyby\Doctrine\QueryException; |
10
|
|
|
use Kdyby\Doctrine\Registry; |
11
|
|
|
use League\OAuth2\Server\Entities\AuthCodeEntityInterface; |
12
|
|
|
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface; |
13
|
|
|
|
14
|
|
View Code Duplication |
class AuthCodeRepository implements AuthCodeRepositoryInterface |
|
|
|
|
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Registry |
18
|
|
|
*/ |
19
|
|
|
private $registry; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Registry $registry |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Registry $registry) |
25
|
|
|
{ |
26
|
|
|
$this->registry = $registry; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return AuthCodeEntity |
31
|
|
|
*/ |
32
|
|
|
public function getNewAuthCode() |
33
|
|
|
{ |
34
|
|
|
return new AuthCodeEntity(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param AuthCodeEntityInterface $authCodeEntity |
39
|
|
|
* @throws ORMInvalidArgumentException |
40
|
|
|
* @throws OptimisticLockException |
41
|
|
|
*/ |
42
|
|
|
public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity) |
43
|
|
|
{ |
44
|
|
|
if ($authCodeEntity instanceof AuthCodeEntity) { |
45
|
|
|
$manager = $this->registry->getManager(); |
46
|
|
|
$manager->persist($authCodeEntity); |
47
|
|
|
$manager->flush(); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $codeId |
53
|
|
|
* @throws InvalidStateException |
54
|
|
|
* @throws QueryException |
55
|
|
|
* @throws OptimisticLockException |
56
|
|
|
*/ |
57
|
|
|
public function revokeAuthCode($codeId) |
58
|
|
|
{ |
59
|
|
|
$manager = $this->registry->getManager(); |
60
|
|
|
/** @var AuthCodeEntity|null $authCodeEntity */ |
61
|
|
|
if ($authCodeEntity = $manager->getRepository(AuthCodeEntity::class)->fetchOne($this->createQuery()->byIdentifier($codeId))) { |
62
|
|
|
$authCodeEntity->setRevoked(true); |
63
|
|
|
$manager->flush(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $codeId |
69
|
|
|
* @return bool |
70
|
|
|
* @throws InvalidStateException |
71
|
|
|
* @throws QueryException |
72
|
|
|
*/ |
73
|
|
|
public function isAuthCodeRevoked($codeId) |
74
|
|
|
{ |
75
|
|
|
/** @var AuthCodeEntity|null $authCodeEntity */ |
76
|
|
|
$authCodeEntity = $this->registry->getManager()->getRepository(AuthCodeEntity::class)->fetchOne($this->createQuery()->byIdentifier($codeId)); |
77
|
|
|
return $authCodeEntity ? $authCodeEntity->isRevoked() : true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return AuthCodeQuery |
82
|
|
|
*/ |
83
|
|
|
protected function createQuery(): AuthCodeQuery |
84
|
|
|
{ |
85
|
|
|
return new AuthCodeQuery(); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.