Passed
Push — master ( f80f70...56399a )
by Thomas Mauro
03:05 queued 10s
created

AggregateParser::unpack()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 29
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5.0406

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 16
nc 6
nop 2
dl 0
loc 29
ccs 15
cts 17
cp 0.8824
crap 5.0406
rs 9.4222
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 Facile\OpenIDClient\Client\ClientInterface;
9
use function is_array;
10
use Throwable;
11
12
/**
13
 * @psalm-import-type TokenSetClaimsType from \Facile\OpenIDClient\Token\TokenSetInterface
14
 */
15
final class AggregateParser extends AbstractClaims implements AggregatedParserInterface
16
{
17 4
    public function unpack(ClientInterface $client, array $claims): array
18
    {
19 4
        $claimSources = $claims['_claim_sources'] ?? null;
20 4
        $claimNames = $claims['_claim_names'] ?? null;
21
22 4
        if (! is_array($claimSources)) {
23 1
            return $claims;
24
        }
25
26 3
        if (! is_array($claimNames)) {
27 1
            return $claims;
28
        }
29
30
        /** @var array<string, array{JWT: string}> $aggregatedSources */
31 2
        $aggregatedSources = array_filter($claimSources, static function ($value): bool {
32 2
            return is_string($value['JWT'] ?? null);
33 2
        });
34
35 2
        $claimPayloads = [];
36 2
        foreach ($aggregatedSources as $sourceName => $source) {
37
            try {
38 2
                $claimPayloads[$sourceName] = $this->claimJWT($client, $source['JWT']);
39 2
                unset($claims['_claim_sources'][$sourceName]);
40
            } catch (Throwable $e) {
41
                throw $e;
42
            }
43
        }
44
45 2
        return $this->cleanClaims($this->assignClaims($claims, $claimNames, $claimPayloads));
46
    }
47
}
48