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

AuthCodeRepository   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
eloc 20
dl 0
loc 57
ccs 0
cts 24
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A revokeAuthCode() 0 9 2
A getNewAuthCode() 0 3 1
A persistNewAuthCode() 0 12 1
A isAuthCodeRevoked() 0 8 4
1
<?php
2
3
namespace OAuth\Repository;
4
5
use DateTime;
6
use Doctrine\ORM\EntityRepository;
7
use Exception;
8
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
9
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
10
use OAuth\AuthCode;
11
use OAuth\Client;
12
13
class AuthCodeRepository extends EntityRepository implements AuthCodeRepositoryInterface
14
{
15
    /**
16
     * @return AuthCode
17
     */
18
    public function getNewAuthCode()
19
    {
20
        return new AuthCode();
21
    }
22
23
    /**
24
     * @param AuthCodeEntityInterface $authCodeEntity
25
     * @throws \Doctrine\ORM\ORMException
26
     * @throws \Doctrine\ORM\OptimisticLockException
27
     * @throws \Doctrine\ORM\TransactionRequiredException
28
     */
29
    public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
30
    {
31
        $date = new DateTime();
32
        $date->modify('+24 hours');
33
        $authCodeEntity->setExpiryDateTime($date);
34
        /** @var Client $client */
35
        $client = $this->_em->getRepository(Client::class)
36
                    ->findOneBy(['identifier' => $authCodeEntity->getClient()->getIdentifier()]);
37
        $authCodeEntity->setClient($client);
38
        $this->_em->persist($authCodeEntity);
39
        $this->_em->flush();
40
        return;
41
    }
42
43
    /**
44
     * @param string $codeId
45
     * @throws Exception
46
     */
47
    public function revokeAuthCode($codeId)
48
    {
49
        /** @var AuthCode $token */
50
        $code = $this->findOneBy(['identifier' => $codeId]);
51
        if(!$code) {
52
            throw new Exception('Token not found', 404);
53
        }
54
        $code->setRevoked(true);
55
        $this->_em->flush($code);
56
    }
57
58
    /**
59
     * @param string $codeId
60
     * @return mixed
61
     */
62
    public function isAuthCodeRevoked($codeId)
63
    {
64
        /** @var AuthCode $code */
65
        $code = $this->findOneBy(['identifier' => $codeId]);
66
        if (!$code || $code->getExpiryDateTime() < new DateTime() || $code->isRevoked()) {
0 ignored issues
show
introduced by
$code is of type OAuth\AuthCode, thus it always evaluated to true.
Loading history...
67
            return true;
68
        }
69
        return false;
70
    }
71
}