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

DiscoveryMetadataProvider::discovery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.1825

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 19
ccs 8
cts 11
cp 0.7272
rs 9.9
c 1
b 0
f 0
cc 3
nc 3
nop 1
crap 3.1825
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