Passed
Push — master ( 84c2d4...3cbaa9 )
by Daniel
05:20
created

DoctrineRefreshTokenStorage::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 12
ccs 0
cts 9
cp 0
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Silverback API Components Bundle Project
5
 *
6
 * (c) Daniel West <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Silverback\ApiComponentsBundle\RefreshToken\Storage;
15
16
use Doctrine\ORM\EntityManager;
17
use Doctrine\ORM\EntityNotFoundException;
18
use Doctrine\Persistence\ManagerRegistry;
19
use Silverback\ApiComponentsBundle\Entity\Core\AbstractRefreshToken;
20
use Silverback\ApiComponentsBundle\RefreshToken\RefreshToken;
21
use Silverback\ApiComponentsBundle\Repository\Core\RefreshTokenRepository;
22
use Symfony\Component\Security\Core\User\UserInterface;
23
24
/**
25
 * @author Vincent Chalamon <[email protected]>
26
 */
27
final class DoctrineRefreshTokenStorage implements RefreshTokenStorageInterface
28
{
29
    private ManagerRegistry $registry;
30
    private int $ttl;
31
    private string $className;
32
33 7
    public function __construct(ManagerRegistry $registry, int $ttl, array $options)
34
    {
35 7
        $this->registry = $registry;
36 7
        $this->ttl = $ttl;
37
38 7
        if (!isset($options['class'])) {
39
            throw new \InvalidArgumentException('You must specify silverback_api_components.refresh.token.options.class option.');
40
        }
41
42 7
        $this->className = $options['class'];
43 7
    }
44
45
    public function findOneByUser(UserInterface $user): ?RefreshToken
46
    {
47
        $repository = $this->getEntityManager()->getRepository($this->className);
48
        if (!$repository instanceof RefreshTokenRepository) {
49
            throw new \InvalidArgumentException('RefreshToken entity repository must be instance of ' . RefreshTokenRepository::class);
50
        }
51
52
        return $repository->findOneByUser($user);
53
    }
54
55
    public function create(UserInterface $user): void
56
    {
57
        $className = $this->className;
58
        /** @var RefreshToken $refreshToken */
59
        $refreshToken = new $className();
60
        $refreshToken->setCreatedAt(new \DateTimeImmutable());
61
        $refreshToken->setExpiresAt(new \DateTimeImmutable("$this->ttl seconds"));
62
        $refreshToken->setUser($user);
63
64
        $em = $this->getEntityManager();
65
        $em->persist($refreshToken);
66
        $em->flush($refreshToken);
67
    }
68
69
    public function expireAll(?UserInterface $user): void
70
    {
71
        $em = $this->getEntityManager();
72
        $repository = $em->getRepository($this->className);
73
        $refreshTokens = $user ? $repository->findBy(['user' => $user]) : $repository->findAll();
74
75
        foreach ($refreshTokens as $refreshToken) {
76
            /* @var AbstractRefreshToken $refreshToken */
77
            $this->expireToken($refreshToken, false);
78
        }
79
80
        $em->flush();
81
    }
82
83
    public function expireToken(AbstractRefreshToken $refreshToken, bool $flush = true): void
84
    {
85
        $em = $this->getEntityManager();
86
        if (!$refreshToken->isExpired()) {
87
            $refreshToken->setExpiresAt(new \DateTimeImmutable());
88
        }
89
        if ($flush) {
90
            $em->flush();
91
        }
92
    }
93
94
    private function getEntityManager(): EntityManager
95
    {
96
        /** @var EntityManager|null $em */
97
        $em = $this->registry->getManagerForClass($this->className);
98
        if (!$em) {
0 ignored issues
show
introduced by
$em is of type Doctrine\ORM\EntityManager, thus it always evaluated to true.
Loading history...
99
            throw new EntityNotFoundException('No entity found for class RefreshToken::class.');
100
        }
101
102
        return $em;
103
    }
104
}
105