DistributedParser::fetch()   B
last analyzed

Complexity

Conditions 8
Paths 27

Size

Total Lines 46
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 8.0036

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 26
nc 27
nop 3
dl 0
loc 46
ccs 25
cts 26
cp 0.9615
crap 8.0036
rs 8.4444
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClient\Claims;
6
7
use function array_filter;
8
use function Facile\OpenIDClient\check_server_response;
9
use Facile\OpenIDClient\Client\ClientInterface as OpenIDClient;
10
use Facile\OpenIDClient\Issuer\IssuerBuilderInterface;
11
use Http\Discovery\Psr17FactoryDiscovery;
12
use Http\Discovery\Psr18ClientDiscovery;
13
use function is_array;
14
use Jose\Component\Core\AlgorithmManager;
15
use Jose\Component\Signature\JWSVerifier;
16
use Jose\Component\Signature\Serializer\JWSSerializer;
17
use Psr\Http\Client\ClientInterface;
18
use Psr\Http\Message\RequestFactoryInterface;
19
use Psr\Http\Message\ResponseInterface;
20
use Throwable;
21
22
final class DistributedParser extends AbstractClaims implements DistributedParserInterface
23
{
24
    /** @var ClientInterface */
25
    private $client;
26
27
    /** @var RequestFactoryInterface */
28
    private $requestFactory;
29
30 4
    public function __construct(
31
        ?IssuerBuilderInterface $issuerBuilder = null,
32
        ?ClientInterface $client = null,
33
        ?RequestFactoryInterface $requestFactory = null,
34
        ?AlgorithmManager $algorithmManager = null,
35
        ?JWSVerifier $JWSVerifier = null,
36
        ?JWSSerializer $serializer = null
37
    ) {
38 4
        parent::__construct($issuerBuilder, $algorithmManager, $JWSVerifier, $serializer);
39
40 4
        $this->client = $client ?? Psr18ClientDiscovery::find();
41 4
        $this->requestFactory = $requestFactory ?? Psr17FactoryDiscovery::findRequestFactory();
42 4
    }
43
44 4
    public function fetch(OpenIDClient $client, array $claims, array $accessTokens = []): array
45
    {
46 4
        $claimSources = $claims['_claim_sources'] ?? null;
47 4
        $claimNames = $claims['_claim_names'] ?? null;
48
49 4
        if (! is_array($claimSources)) {
50 1
            return $claims;
51
        }
52
53 3
        if (! is_array($claimNames)) {
54 1
            return $claims;
55
        }
56
57
        /** @var array<string, array{endpoint: string}> $distributedSources */
58 2
        $distributedSources = array_filter($claimSources, static function ($value): bool {
59 2
            return null !== ($value['endpoint'] ?? null);
60 2
        });
61
62
        /** @var array<string, ResponseInterface> $responses */
63 2
        $responses = [];
64 2
        foreach ($distributedSources as $sourceName => $source) {
65 2
            $request = $this->requestFactory->createRequest('GET', $source['endpoint'])
66 2
                ->withHeader('accept', 'application/jwt');
67
68 2
            $accessToken = $source['access_token'] ?? ($accessTokens[$sourceName] ?? null);
69 2
            if ($accessToken) {
70 2
                $request = $request->withHeader('authorization', 'Bearer ' . $accessToken);
71
            }
72
73
            try {
74 2
                $responses[$sourceName] = $this->client->sendRequest($request);
75
            } catch (Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
76
            }
77
        }
78
79 2
        $claimPayloads = [];
80 2
        foreach ($responses as $sourceName => $response) {
81
            try {
82 2
                check_server_response($response);
83 1
                $claimPayloads[$sourceName] = $this->claimJWT($client, (string) $response->getBody());
84 1
                unset($claims['_claim_sources'][$sourceName]);
85 1
            } catch (Throwable $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
86
            }
87
        }
88
89 2
        return $this->cleanClaims($this->assignClaims($claims, $claimNames, $claimPayloads));
90
    }
91
}
92