Passed
Push — master ( 106006...c1ddb5 )
by Thomas Mauro
06:38 queued 11s
created

get_endpoint_uri()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6.6

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 4
nop 2
dl 0
loc 19
ccs 6
cts 10
cp 0.6
crap 6.6
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\OpenIdClient;
6
7
use TMV\OpenIdClient\Client\ClientInterface as OpenIDClient;
8
use TMV\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 1
    $authMethod = $client->getMetadata()->get($endpointMetadata . '_auth_method');
21
22 1
    $endpoint = null;
23
24 1
    if (null !== $authMethod && false !== strpos($authMethod, 'tls_client_auth')) {
25
        $endpoint = $client->getIssuer()
26
            ->getMetadata()
27
            ->getMtlsEndpointAliases()['token_endpoint'] ?? null;
28
    }
29
30 1
    $endpoint = $endpoint ?: $client->getIssuer()->getMetadata()->get($endpointMetadata);
31
32 1
    if (! is_string($endpoint)) {
33
        throw new RuntimeException('Unable to retrieve the token endpoint');
34
    }
35
36 1
    return $endpoint;
37
}
38