DbalRefreshTokenStorage::findByToken()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 58
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 58
rs 8.7274
cc 5
eloc 41
nc 3
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Bgy\OAuth2Server\DbalStorage;
4
5
use Bgy\OAuth2\AccessToken;
6
use Bgy\OAuth2\RefreshToken;
7
use Bgy\OAuth2\ResourceOwner;
8
use Bgy\OAuth2\Storage\RefreshTokenNotFound;
9
use Bgy\OAuth2\Storage\RefreshTokenStorage;
10
use Doctrine\DBAL\Connection;
11
12
/**
13
 * @author Boris Guéry <[email protected]>
14
 */
15
class DbalRefreshTokenStorage implements RefreshTokenStorage
16
{
17
    private $dbalConnection;
18
    private $tableConfiguration;
19
20
    public function __construct(Connection $dbalConnection, TableConfiguration $tableConfiguration)
21
    {
22
        $this->dbalConnection     = $dbalConnection;
23
        $this->tableConfiguration = $tableConfiguration;
24
    }
25
26
    public function save(RefreshToken $refreshToken)
27
    {
28
        $revokedAt = $refreshToken->getRevokedAt()
0 ignored issues
show
Bug introduced by
The method getRevokedAt() does not seem to exist on object<Bgy\OAuth2\RefreshToken>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
            ? substr(
30
                $refreshToken->getRevokedAt()->format(\DateTime::ISO8601),
0 ignored issues
show
Bug introduced by
The method getRevokedAt() does not seem to exist on object<Bgy\OAuth2\RefreshToken>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
31
                0,
32
                -5
33
            )
34
            : null
35
        ;
36
37
        try {
38
            $this->findByToken($refreshToken->getToken());
39
            $this->dbalConnection->update(
40
                $this->tableConfiguration->getRefreshTokenTableName(),
41
                [
42
                    'associated_access_token' => $refreshToken->getAssociatedAccessToken()->getToken(),
43
                    'expires_at' => substr(
44
                        $refreshToken->getExpiresAt()->format(\DateTime::ISO8601),
45
                        0,
46
                        -5
47
                    ),
48
                    'revoked_at' => $revokedAt
49
                ],
50
                [
51
                    'refresh_token' => $refreshToken->getToken(),
52
                ]
53
            );
54
        } catch (RefreshTokenNotFound $e) {
55
            $this->dbalConnection->insert(
56
                $this->tableConfiguration->getRefreshTokenTableName(),
57
                [
58
                    'refresh_token' => $refreshToken->getToken(),
59
                    'associated_access_token' => $refreshToken->getAssociatedAccessToken()->getToken(),
60
                    'expires_at' => substr(
61
                        $refreshToken->getExpiresAt()->format(\DateTime::ISO8601),
62
                        0,
63
                        -5
64
                    ),
65
                    'revoked_at' => $revokedAt
66
                ]
67
            );
68
        }
69
    }
70
71
    public function delete(RefreshToken $refreshToken)
72
    {
73
        $this->dbalConnection->delete(
74
            $this->tableConfiguration->getRefreshTokenTableName(),
75
            [
76
                'refresh_token' => $refreshToken->getToken(),
77
            ]
78
        );
79
    }
80
81
    public function findByToken($refreshTokenId)
82
    {
83
        $qb = $this->dbalConnection->createQueryBuilder();
84
        $stmt = $qb->select('*, r.expires_at AS refresh_token_expires_at')
85
            ->from($this->tableConfiguration->getRefreshTokenTableName(), 'r')
86
            ->innerJoin(
87
                'r',
88
                $this->tableConfiguration->getAccessTokenTableName(),
89
                'a',
90
                'a.access_token = r.associated_access_token'
91
            )
92
            ->where(
93
                $qb->expr()->like('refresh_token', ':refreshToken')
94
            )
95
            ->setMaxResults(1)
96
            ->setParameter('refreshToken', $refreshTokenId)
97
            ->execute()
98
        ;
99
100
        $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
101
102
        if (1 !== count($rows)) {
103
104
            throw new RefreshTokenNotFound($refreshTokenId);
105
        }
106
107
        $revokedAt = (null !== $rows[0]['revoked_at'])
108
            ? \DateTimeImmutable::createFromFormat(
109
                'Y-m-d H:i:s',
110
                $rows[0]['revoked_at'],
111
                new \DateTimeZone('UTC')
112
            )
113
            : null
114
        ;
115
116
        return new RefreshToken(
117
            $rows[0]['refresh_token'],
118
            new AccessToken(
119
                $rows[0]['access_token'],
120
                \DateTimeImmutable::createFromFormat(
121
                    'Y-m-d H:i:s',
122
                    $rows[0]['expires_at'],
123
                    new \DateTimeZone('UTC')
124
                ),
125
                $rows[0]['client_id'],
126
                ($rows[0]['resource_owner_id'] && $rows[0]['resource_owner_type'])
127
                    ? new ResourceOwner($rows[0]['resource_owner_id'], $rows[0]['resource_owner_type'])
128
                    : null,
129
                explode(',', $rows[0]['scopes'])
130
            ),
131
            \DateTimeImmutable::createFromFormat(
132
                'Y-m-d H:i:s',
133
                $rows[0]['refresh_token_expires_at'],
134
                new \DateTimeZone('UTC')
135
            ),
136
            $revokedAt
0 ignored issues
show
Unused Code introduced by
The call to RefreshToken::__construct() has too many arguments starting with $revokedAt.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
137
        );
138
    }
139
}
140