RefreshTokenRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
namespace CodexShaper\DBM\MongoDB\Passport\Bridge;
4
5
use Illuminate\Contracts\Events\Dispatcher;
6
use Illuminate\Database\Connection;
7
use Laravel\Passport\Events\RefreshTokenCreated;
8
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
9
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
10
11
class RefreshTokenRepository implements RefreshTokenRepositoryInterface
12
{
13
    /**
14
     * The database connection.
15
     *
16
     * @var \Illuminate\Database\Connection
17
     */
18
    protected $database;
19
20
    /**
21
     * The event dispatcher instance.
22
     *
23
     * @var \Illuminate\Contracts\Events\Dispatcher
24
     */
25
    protected $events;
26
27
    /**
28
     * Create a new repository instance.
29
     *
30
     * @param  \Illuminate\Database\Connection  $database
31
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
32
     * @return void
33
     */
34
    public function __construct(Connection $database, Dispatcher $events)
35
    {
36
        $this->events = $events;
37
        $this->database = $database;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getNewRefreshToken()
44
    {
45
        return new RefreshToken;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity)
52
    {
53
        $this->database->table('oauth_refresh_tokens')->insert([
54
            'id'              => $id = $refreshTokenEntity->getIdentifier(),
55
            'access_token_id' => $accessTokenId = $refreshTokenEntity->getAccessToken()->getIdentifier(),
56
            'revoked'         => false,
57
            'expires_at'      => $refreshTokenEntity->getExpiryDateTime(),
58
        ]);
59
60
        $this->events->dispatch(new RefreshTokenCreated($id, $accessTokenId));
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function revokeRefreshToken($tokenId)
67
    {
68
        $this->database->table('oauth_refresh_tokens')
69
            ->where('id', $tokenId)->update(['revoked' => true]);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function isRefreshTokenRevoked($tokenId)
76
    {
77
        return $this->database->table('oauth_refresh_tokens')
78
            ->where('id', $tokenId)->where('revoked', true)->exists();
79
    }
80
}
81