Passed
Push — master ( dd8a55...c32c8a )
by Daniel
04:48
created

RefreshTokenRepository   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
c 2
b 0
f 0
dl 0
loc 20
ccs 0
cts 11
cp 0
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A findByUser() 0 7 1
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\Repository\Core;
15
16
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
17
use Doctrine\Persistence\ManagerRegistry;
18
use Silverback\ApiComponentsBundle\Entity\Core\AbstractRefreshToken;
19
use Silverback\ApiComponentsBundle\Exception\InvalidArgumentException;
20
use Symfony\Component\Security\Core\User\UserInterface;
21
22
/**
23
 * @author Daniel West <[email protected]>
24
 *
25
 * @method AbstractRefreshToken|null find($id, $lockMode = null, $lockVersion = null)
26
 * @method AbstractRefreshToken|null findOneBy(array $criteria, array $orderBy = null)
27
 * @method AbstractRefreshToken[]    findAll()
28
 * @method AbstractRefreshToken[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
29
 */
30
class RefreshTokenRepository extends ServiceEntityRepository
31
{
32
    public function __construct(ManagerRegistry $registry, string $entityClass)
33
    {
34
        if (!is_subclass_of($entityClass, AbstractRefreshToken::class)) {
35
            throw new InvalidArgumentException(sprintf('The entity class `%s` used for the repository `%s` must be a subclass of `%s`', $entityClass, __CLASS__, AbstractRefreshToken::class));
36
        }
37
        parent::__construct($registry, $entityClass);
38
    }
39
40
    /**
41
     * @return AbstractRefreshToken[]
42
     */
43
    public function findByUser(UserInterface $user): array
44
    {
45
        return $this->createQueryBuilder('rt')
46
            ->andWhere('rt.user = :user')->setParameter('user', $user)
47
            ->andWhere('rt.expiresAt > :now')->setParameter('now', new \DateTimeImmutable())
48
            ->getQuery()
49
            ->getResult();
50
    }
51
}
52