Passed
Push — master ( e9c9be...dec7df )
by Derek Stephen
03:02
created

RefreshTokenRepository::isRefreshTokenRevoked()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace OAuth\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\UnitOfWork;
7
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
8
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
9
use OAuth\AccessToken;
10
use OAuth\RefreshToken;
11
12
/**
13
 * Class RefreshTokenRepository
14
 * @package OAuth\Repository
15
 */
16
class RefreshTokenRepository extends EntityRepository implements RefreshTokenRepositoryInterface
17
{
18
    /**
19
     * @return RefreshToken
20
     */
21
    public function getNewRefreshToken()
22
    {
23
        return new RefreshToken();
24
    }
25
26
    /**
27
     * @param RefreshTokenEntityInterface $refreshTokenEntity
28
     * @return RefreshTokenEntityInterface
29
     * @throws \Doctrine\ORM\OptimisticLockException
30
     */
31
    public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity)
32
    {
33
        $accessToken = $refreshTokenEntity->getAccessToken();
34
        if ($this->_em->getUnitOfWork()->getEntityState($accessToken) !== UnitOfWork::STATE_MANAGED) {
35
            /** @var AccessToken $accessToken */
36
            $accessToken = $this->_em->merge($accessToken);
37
            $refreshTokenEntity->setAccessToken($accessToken);
38
        }
39
        $this->_em->persist($refreshTokenEntity);
40
        $this->_em->flush();
41
42
        return $refreshTokenEntity;
43
    }
44
45
    /**
46
     * @param string $tokenId
47
     * @return bool
48
     * @throws \Doctrine\ORM\ORMException
49
     * @throws \Doctrine\ORM\OptimisticLockException
50
     * @throws \Doctrine\ORM\TransactionRequiredException
51
     */
52
    public function revokeRefreshToken($tokenId)
53
    {
54
        $token = $this->_em->find(RefreshToken::class, $tokenId);
55
        if ($token instanceof RefreshTokenEntityInterface) {
0 ignored issues
show
introduced by
$token is always a sub-type of League\OAuth2\Server\Ent...eshTokenEntityInterface.
Loading history...
56
            $this->_em->remove($token);
57
            $this->_em->flush();
58
59
            return true;
60
        }
61
62
        return false;
63
    }
64
65
    /**
66
     * @param string $tokenId
67
     * @return bool
68
     * @throws \Doctrine\ORM\ORMException
69
     * @throws \Doctrine\ORM\OptimisticLockException
70
     * @throws \Doctrine\ORM\TransactionRequiredException
71
     */
72
    public function isRefreshTokenRevoked($tokenId)
73
    {
74
        $token = $this->_em->find(RefreshToken::class, $tokenId);
75
        if ($token instanceof RefreshTokenEntityInterface) {
0 ignored issues
show
introduced by
$token is always a sub-type of League\OAuth2\Server\Ent...eshTokenEntityInterface.
Loading history...
76
77
            return false;
78
        }
79
80
        return true;
81
    }
82
}