Passed
Push — master ( 401cce...21cdfe )
by Derek Stephen
03:23
created

AuthCodeRepository::persistNewAuthCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 12
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
c 0
b 0
f 0
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 1
    public function getNewAuthCode()
19
    {
20 1
        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 1
    public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
30
    {
31 1
        $date = new DateTime();
32 1
        $date->modify('+24 hours');
33 1
        $authCodeEntity->setExpiryDateTime($date);
34
        /** @var Client $client */
35 1
        $client = $this->_em->getRepository(Client::class)
36 1
                    ->findOneBy(['identifier' => $authCodeEntity->getClient()->getIdentifier()]);
37 1
        $authCodeEntity->setClient($client);
38 1
        $this->_em->persist($authCodeEntity);
39 1
        $this->_em->flush();
40 1
        return;
41
    }
42
43
    /**
44
     * @param string $codeId
45
     * @throws Exception
46
     */
47 1
    public function revokeAuthCode($codeId)
48
    {
49
        /** @var AuthCode $token */
50 1
        $code = $this->findOneBy(['identifier' => $codeId]);
51 1
        if(!$code) {
52
            throw new Exception('Token not found', 404);
53
        }
54 1
        $code->setRevoked(true);
55 1
        $this->_em->flush($code);
56 1
    }
57
58
    /**
59
     * @param string $codeId
60
     * @return mixed
61
     */
62 1
    public function isAuthCodeRevoked($codeId)
63
    {
64
        /** @var AuthCode $code */
65 1
        $code = $this->findOneBy(['identifier' => $codeId]);
66 1
        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 1
        return false;
70
    }
71
}