Completed
Push — develop ( 5503b1...6a0d81 )
by Abdelrahman
19:31 queued 18:12
created

AuthCodeRepository   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 3
dl 0
loc 47
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewAuthCode() 0 4 1
A persistNewAuthCode() 0 17 3
A revokeAuthCode() 0 4 1
A isAuthCodeRevoked() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Rinvex\Oauth\Repositories;
6
7
use Rinvex\Oauth\Bridge\AuthCode;
8
use League\OAuth2\Server\Entities\AuthCodeEntityInterface;
9
use League\OAuth2\Server\Repositories\AuthCodeRepositoryInterface;
10
11
class AuthCodeRepository implements AuthCodeRepositoryInterface
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public function getNewAuthCode()
17
    {
18
        return new AuthCode();
19
    }
20
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function persistNewAuthCode(AuthCodeEntityInterface $authCodeEntity)
25
    {
26
        $clientId = $authCodeEntity->getClient()->getIdentifier();
27
        [$userType, $userId] = explode(':', $authCodeEntity->getUserIdentifier());
0 ignored issues
show
Bug introduced by
The variable $userType does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $userId seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
28
29
        $userId = method_exists($user = app('cortex.auth.'.$userType), 'unhashId') ? $user->unhashId($userId) : $userId;
0 ignored issues
show
Bug introduced by
The variable $userId seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
30
        $clientId = method_exists($client = app('rinvex.oauth.client'), 'unhashId') ? $client->unhashId($clientId) : $clientId;
31
32
        app('rinvex.oauth.auth_code')->create([
33
            'identifier' => $authCodeEntity->getIdentifier(),
34
            'user_id' => $userId,
35
            'user_type' => $userType,
36
            'client_id' => $clientId,
37
            'is_revoked' => false,
38
            'expires_at' => $authCodeEntity->getExpiryDateTime(),
39
        ]);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function revokeAuthCode($codeId)
46
    {
47
        app('rinvex.oauth.auth_code')->where('identifier', $codeId)->update(['is_revoked' => true]);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function isAuthCodeRevoked($codeId)
54
    {
55
        return app('rinvex.oauth.auth_code')->where('identifier', $codeId)->where('is_revoked', true)->exists();
56
    }
57
}
58