RevocationService   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 41
ccs 0
cts 15
cp 0
rs 10
c 2
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A revoke() 0 20 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClient\Service;
6
7
use function Facile\OpenIDClient\check_server_response;
8
use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient;
9
use Facile\OpenIDClient\Exception\RuntimeException;
10
use function Facile\OpenIDClient\get_endpoint_uri;
11
use Psr\Http\Client\ClientExceptionInterface;
12
use Psr\Http\Client\ClientInterface;
13
use Psr\Http\Message\RequestFactoryInterface;
14
15
/**
16
 * RFC 7009 Token Revocation
17
 *
18
 * @link https://tools.ietf.org/html/rfc7009 RFC 7009
19
 */
20
final class RevocationService
21
{
22
    /** @var ClientInterface */
23
    private $client;
24
25
    /** @var RequestFactoryInterface */
26
    private $requestFactory;
27
28
    public function __construct(
29
        ClientInterface $client,
30
        RequestFactoryInterface $requestFactory
31
    ) {
32
        $this->client = $client;
33
        $this->requestFactory = $requestFactory;
34
    }
35
36
    /**
37
     * @param OpenIDClient $client
38
     * @param string $token
39
     * @param array<string, mixed> $params
40
     */
41
    public function revoke(OpenIDClient $client, string $token, array $params = []): void
42
    {
43
        $endpointUri = get_endpoint_uri($client, 'revocation_endpoint');
44
45
        $authMethod = $client->getAuthMethodFactory()
46
            ->create($client->getMetadata()->getRevocationEndpointAuthMethod());
47
48
        $tokenRequest = $this->requestFactory->createRequest('POST', $endpointUri)
49
            ->withHeader('content-type', 'application/x-www-form-urlencoded');
50
51
        $params['token'] = $token;
52
        $tokenRequest = $authMethod->createRequest($tokenRequest, $client, $params);
53
54
        $httpClient = $client->getHttpClient() ?? $this->client;
55
56
        try {
57
            $response = $httpClient->sendRequest($tokenRequest);
58
            check_server_response($response, 200);
59
        } catch (ClientExceptionInterface $e) {
60
            throw new RuntimeException('Unable to get revocation response', 0, $e);
61
        }
62
    }
63
}
64