-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDoctrineORMTokenRepository.php
More file actions
114 lines (97 loc) · 2.99 KB
/
Copy pathDoctrineORMTokenRepository.php
File metadata and controls
114 lines (97 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
declare(strict_types=1);
namespace Yokai\SecurityTokenBundle\Repository;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Yokai\SecurityTokenBundle\Entity\Token;
use Yokai\SecurityTokenBundle\Exception\TokenConsumedException;
use Yokai\SecurityTokenBundle\Exception\TokenExpiredException;
use Yokai\SecurityTokenBundle\Exception\TokenNotFoundException;
/**
* Doctrine ORM token repository;
*
* @author Yann Eugoné <eugone.yann@gmail.com>
*/
final class DoctrineORMTokenRepository implements TokenRepositoryInterface
{
/**
* @var EntityManager
*/
private $manager;
/**
* @var EntityRepository<Token>
*/
private $repository;
/**
* @param EntityManager $manager The token entity manager
* @param EntityRepository<Token> $repository The token entity repository
*/
public function __construct(EntityManager $manager, EntityRepository $repository)
{
$this->manager = $manager;
$this->repository = $repository;
}
public function get(string $value, string $purpose): Token
{
$token = $this->repository->findOneBy(
[
'value' => $value,
'purpose' => $purpose,
],
);
if (!$token instanceof Token) {
throw TokenNotFoundException::create($value, $purpose);
}
if ($token->isExpired()) {
throw TokenExpiredException::create($value, $purpose, $token->getExpiresAt());
}
if ($token->isConsumed()) {
throw TokenConsumedException::create($value, $purpose, $token->getCountUsages());
}
return $token;
}
public function findExisting(string $userClass, string $userId, string $purpose): Token|null
{
$token = $this->repository->findOneBy(
[
'userClass' => $userClass,
'userId' => $userId,
'purpose' => $purpose,
],
);
if (!$token instanceof Token) {
return null;
}
if ($token->isConsumed() || $token->isExpired()) {
return null;
}
return $token;
}
public function create(Token $token): void
{
$this->save($token);
}
public function update(Token $token): void
{
$this->save($token);
}
public function exists(string $value, string $purpose): bool
{
$builder = $this->repository->createQueryBuilder('token');
$builder
->select('COUNT(token.id)')
->where('token.value = :value')
->andWhere('token.purpose = :purpose')
->setParameter('value', $value)
->setParameter('purpose', $purpose)
;
/** @var string|int $result */
$result = $builder->getQuery()->getSingleScalarResult();
return \intval($result) > 0;
}
private function save(Token $token): void
{
$this->manager->persist($token);
$this->manager->flush();
}
}