Passed
Push — master ( 904589...078d26 )
by Thomas Mauro
02:52
created

DiscoveryMetadataProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 15
dl 0
loc 35
ccs 12
cts 15
cp 0.8
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 3
A discovery() 0 19 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\OpenIdClient\Provider;
6
7
use Http\Discovery\Psr17FactoryDiscovery;
8
use Http\Discovery\Psr18ClientDiscovery;
9
use Psr\Http\Client\ClientExceptionInterface;
10
use Psr\Http\Client\ClientInterface;
11
use Psr\Http\Message\RequestFactoryInterface;
12
use TMV\OpenIdClient\Exception\RuntimeException;
13
use function TMV\OpenIdClient\parseMetadataResponse;
14
15
class DiscoveryMetadataProvider implements DiscoveryMetadataProviderInterface
16
{
17
    /** @var ClientInterface */
18
    private $client;
19
20
    /** @var RequestFactoryInterface */
21
    private $requestFactory;
22
23 1
    public function __construct(
24
        ?ClientInterface $client = null,
25
        ?RequestFactoryInterface $requestFactory = null
26
    ) {
27 1
        $this->client = $client ?: Psr18ClientDiscovery::find();
28 1
        $this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory();
29 1
    }
30
31 1
    public function discovery(string $uri): array
32
    {
33 1
        $request = $this->requestFactory->createRequest('GET', $uri)
34 1
            ->withHeader('accept', 'application/json')
35 1
            ->withHeader('content-type', 'application/json');
36
37
        try {
38 1
            $response = $this->client->sendRequest($request);
39
        } catch (ClientExceptionInterface $e) {
40
            throw new RuntimeException('Unable to fetch provider metadata', 0, $e);
41
        }
42
43 1
        $data = parseMetadataResponse($response, 200);
44
45 1
        if (! \array_key_exists('issuer', $data)) {
46
            throw new RuntimeException('Invalid metadata content, no "issuer" key found');
47
        }
48
49 1
        return $data;
50
    }
51
}
52