Completed
Push — master ( 53a7e1...12250a )
by Florent
02:47
created

JWKSetConverter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2015 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Payload;
13
14
use Jose\Behaviour\HasJWKSetManager;
15
use Jose\JWKSetInterface;
16
17
/**
18
 * Trait used to convert payload.
19
 */
20
final class JWKSetConverter implements PayloadConverterInterface
21
{
22
    use HasJWKSetManager;
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function isPayloadToStringSupported(array $header, $payload)
27
    {
28
        return $payload instanceof JWKSetInterface;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function isStringToPayloadSupported(array $header, $content)
35
    {
36
        return array_key_exists('cty', $header) && $header['cty'] === 'jwkset+json';
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function convertPayloadToString(array &$header, $payload)
43
    {
44
        $header['cty'] = 'jwkset+json';
45
46
        return json_encode($payload);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function convertStringToPayload(array $header, $content)
53
    {
54
        $jwk = json_decode($content, true);
55
        if (!is_array($jwk) || !array_key_exists('keys', $jwk)) {
56
            throw new \InvalidArgumentException('The content type claims content is a JWKSet, but cannot be converted into JWKSet');
57
        }
58
59
        return $this->getJWKSetManager()->createJWKSet($jwk);
60
    }
61
}
62