Passed
Push — master ( 017734...6e92fd )
by Daniel
06:25
created

DoctrineRefreshTokenStorage::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 10
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\RefreshToken\RefreshToken;
20
use Silverback\ApiComponentsBundle\Repository\Core\RefreshTokenRepository;
21
use Symfony\Component\Security\Core\User\UserInterface;
22
23
/**
24
 * @author Vincent Chalamon <[email protected]>
25
 */
26
final class DoctrineRefreshTokenStorage implements RefreshTokenStorageInterface
27
{
28
    private ManagerRegistry $registry;
29
    private int $ttl;
30
    private string $className;
31
32 7
    public function __construct(ManagerRegistry $registry, int $ttl, array $options)
33
    {
34 7
        $this->registry = $registry;
35 7
        $this->ttl = $ttl;
36
37 7
        if (!isset($options['class'])) {
38
            throw new \InvalidArgumentException('You must specify silverback_api_components.refresh.token.options.class option.');
39
        }
40
41 7
        $this->className = $options['class'];
42 7
    }
43
44
    public function findOneByUser(UserInterface $user): ?RefreshToken
45
    {
46
        $repository = $this->getEntityManager()->getRepository($this->className);
47
        if (!$repository instanceof RefreshTokenRepository) {
48
            throw new \InvalidArgumentException('RefreshToken entity repository must be instance of ' . RefreshTokenRepository::class);
49
        }
50
51
        return $repository->findOneByUser($user);
52
    }
53
54
    public function create(UserInterface $user): void
55
    {
56
        $className = $this->className;
57
        /** @var RefreshToken $refreshToken */
58
        $refreshToken = new $className();
59
        $refreshToken->setCreatedAt(new \DateTimeImmutable());
60
        $refreshToken->setExpiresAt(new \DateTimeImmutable("$this->ttl seconds"));
61
        $refreshToken->setUser($user);
62
63
        $em = $this->getEntityManager();
64
        $em->persist($refreshToken);
65
        $em->flush($refreshToken);
66
    }
67
68
    public function expireAll(?UserInterface $user): void
69
    {
70
        $em = $this->getEntityManager();
71
        $repository = $em->getRepository($this->className);
72
        $refreshTokens = $user ? $repository->findBy(['user' => $user]) : $repository->findAll();
73
74
        foreach ($refreshTokens as $refreshToken) {
75
            $refreshToken->setExpiresAt(new \DateTimeImmutable());
76
            $em->persist($refreshToken);
77
        }
78
79
        $em->flush();
80
    }
81
82
    private function getEntityManager(): EntityManager
83
    {
84
        /** @var EntityManager $em */
85
        $em = $this->registry->getManagerForClass($this->className);
86
        if (!$em) {
0 ignored issues
show
introduced by
$em is of type Doctrine\ORM\EntityManager, thus it always evaluated to true.
Loading history...
87
            throw new EntityNotFoundException('No entity found for class RefreshToken::class.');
88
        }
89
90
        return $em;
91
    }
92
}
93