Completed
Branch develop (8ddc4a)
by Florent
02:46
created

JWSLoader::populateProtectedHeaders()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 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\Util;
13
14
use Base64Url\Base64Url;
15
use Jose\Object\JWS;
16
use Jose\Object\JWSInterface;
17
18
final class JWSLoader
19
{
20
    /**
21
     * @param array $data
22
     *
23
     * @return \Jose\Object\JWSInterface
24
     */
25
    public static function loadSerializedJsonJWS(array $data)
26
    {
27
        $jws = new JWS();
28
29
        self::populatePayload($jws, $data);
30
31
        foreach ($data['signatures'] as $signature) {
32
            $bin_signature = Base64Url::decode($signature['signature']);
33
            $protected_headers = self::getProtectedHeaders($signature);
34
            $headers = self::getHeaders($signature);
35
36
            $jws = $jws->addSignature(null, $protected_headers, $headers, $bin_signature);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a object<Jose\Object\JWKInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
        }
38
39
        return $jws;
40
    }
41
42
    /**
43
     * @param array $data
44
     *
45
     * @return array
46
     */
47
    private static function getProtectedHeaders(array $data)
48
    {
49
        if (array_key_exists('protected', $data)) {
50
            return json_decode(Base64Url::decode($data['protected']), true);
51
        }
52
53
        return [];
54
    }
55
56
    /**
57
     * @param array $data
58
     *
59
     * @return array
60
     */
61
    private static function getHeaders(array $data)
62
    {
63
        if (array_key_exists('header', $data)) {
64
            return $data['header'];
65
        }
66
67
        return [];
68
    }
69
70
    /**
71
     * @param \Jose\Object\JWSInterface $jws
72
     * @param array                     $data
73
     */
74
    private static function populatePayload(JWSInterface &$jws, array $data)
75
    {
76
        if (array_key_exists('payload', $data)) {
77
            $payload = Base64Url::decode($data['payload']);
78
            $json = json_decode($payload, true);
79
            if (null !== $json && !empty($payload)) {
80
                $payload = $json;
81
            }
82
            $jws = $jws->withPayload($payload);
83
        }
84
    }
85
}
86