Lines 100.00% 31 / 31
Methods 100.00% 7 / 7
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 getNewRefreshToken 100.00% 1 / 1 100.00% 1 / 1 1
 persistNewRefreshToken 100.00% 11 / 11 100.00% 1 / 1 2
 revokeRefreshToken 100.00% 5 / 5 100.00% 1 / 1 1
 isRefreshTokenRevoked 100.00% 11 / 11 100.00% 1 / 1 3
 [phpMyFAQ\Auth\OAuth2\Repository\AbstractRepository] __construct 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Auth\OAuth2\Repository\AbstractRepository] db 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Auth\OAuth2\Repository\AbstractRepository] table 100.00% 1 / 1 100.00% 1 / 1 1
27final class RefreshTokenRepository extends AbstractRepository implements RefreshTokenRepositoryInterface
28{
29    public function getNewRefreshToken(): ?RefreshTokenEntityInterface
30    {
31        return new RefreshTokenEntity();
32    }
33
34    public function persistNewRefreshToken(RefreshTokenEntityInterface $refreshTokenEntity): void
35    {
36        $insert = sprintf(
37            "INSERT INTO %s (identifier, access_token_identifier, revoked, expires_at, created)
38             VALUES ('%s', '%s', 0, '%s', %s)",
39            $this->table('faqoauth_refresh_tokens'),
40            $this->db()->escape($refreshTokenEntity->getIdentifier()),
41            $this->db()->escape($refreshTokenEntity->getAccessToken()->getIdentifier()),
42            $refreshTokenEntity->getExpiryDateTime()->format('Y-m-d H:i:s'),
43            $this->db()->now(),
44        );
45
46        if ($this->db()->query($insert) === false) {
47            throw UniqueTokenIdentifierConstraintViolationException::create();
48        }
49    }
50
51    public function revokeRefreshToken(string $tokenId): void
52    {
53        $this->db()->query(sprintf(
54            "UPDATE %s SET revoked = 1 WHERE identifier = '%s'",
55            $this->table('faqoauth_refresh_tokens'),
56            $this->db()->escape($tokenId),
57        ));
58    }
59
60    public function isRefreshTokenRevoked(string $tokenId): bool
61    {
62        $result = $this->db()->query(sprintf(
63            "SELECT revoked FROM %s WHERE identifier = '%s'",
64            $this->table('faqoauth_refresh_tokens'),
65            $this->db()->escape($tokenId),
66        ));
67
68        if ($result === false) {
69            return true;
70        }
71
72        $row = $this->db()->fetchObject($result);
73        if (!is_object($row)) {
74            return true;
75        }
76
77        return (int) ($row->revoked ?? 1) === 1;
78    }
79}

Inherited from phpMyFAQ\Auth\OAuth2\Repository\AbstractRepository

28    public function __construct(
29        protected readonly Configuration $configuration,
30    ) {
31    }
33    protected function db(): DatabaseDriver
34    {
35        return $this->configuration->getDb();
36    }
38    protected function table(string $tableName): string
39    {
40        return Database::getTablePrefix() . $tableName;
41    }