Passed
Pull Request — master (#11)
by Pol
02:31
created

get_endpoint_uri()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.024

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 4
nop 2
dl 0
loc 21
ccs 6
cts 10
cp 0.6
crap 5.024
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClient;
6
7
use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient;
8
use Facile\OpenIDClient\Exception\RuntimeException;
9
10
/**
11
 * Handle endpoint URI based on auth method
12
 *
13
 * @param OpenIDClient $client
14
 * @param string $endpointMetadata
15
 *
16
 * @return string
17
 */
18
function get_endpoint_uri(OpenIDClient $client, string $endpointMetadata): string
19
{
20
    /** @var string|null $authMethod */
21 1
    $authMethod = $client->getMetadata()->get($endpointMetadata . '_auth_method');
22
23 1
    $endpoint = null;
24
25 1
    if (null !== $authMethod && false !== strpos($authMethod, 'tls_client_auth')) {
26
        $endpoint = $client->getIssuer()
27
            ->getMetadata()
28
            ->getMtlsEndpointAliases()['token_endpoint'] ?? null;
29
    }
30
31
    /** @var string|null $endpoint */
32 1
    $endpoint = $endpoint ?? $client->getIssuer()->getMetadata()->get($endpointMetadata);
33
34 1
    if (! is_string($endpoint)) {
35
        throw new RuntimeException('Unable to retrieve the token endpoint');
36
    }
37
38 1
    return $endpoint;
39
}
40