Completed
Push — develop ( 8ddc4a...d84806 )
by Florent
02:34
created

JWELoader::populateRecipientHeaders()   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\JWE;
16
use Jose\Object\JWEInterface;
17
18
final class JWELoader
19
{
20
    /**
21
     * @param array $data
22
     *
23
     * @return \Jose\Object\JWEInterface
24
     */
25
    public static function loadSerializedJsonJWE(array $data)
26
    {
27
        $jwe = new JWE();
28
        $jwe = $jwe->withCiphertext(Base64Url::decode($data['ciphertext']));
29
30
        self::populateIV($jwe, $data);
31
        self::populateAAD($jwe, $data);
32
        self::populateTag($jwe, $data);
33
        self::populateSharedProtectedHeaders($jwe, $data);
34
        self::populateSharedHeaders($jwe, $data);
35
36
        foreach ($data['recipients'] as $recipient) {
37
            $encrypted_key = self::getRecipientEncryptedKey($recipient);
38
            $recipient_headers = self::getRecipientHeaders($recipient);
39
40
            $jwe = $jwe->addRecipientWithEncryptedKey($encrypted_key, $recipient_headers);
0 ignored issues
show
Bug introduced by
It seems like $encrypted_key defined by self::getRecipientEncryptedKey($recipient) on line 37 can also be of type string; however, Jose\Object\JWEInterface...pientWithEncryptedKey() does only seem to accept null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
41
        }
42
43
        return $jwe;
44
    }
45
46
    /**
47
     * @param \Jose\Object\JWEInterface $jwe
48
     * @param array                     $data
49
     */
50
    private static function populateIV(JWEInterface &$jwe, array $data)
51
    {
52
        if (array_key_exists('iv', $data)) {
53
            $jwe = $jwe->withIV(Base64Url::decode($data['iv']));
54
        }
55
    }
56
57
    /**
58
     * @param \Jose\Object\JWEInterface $jwe
59
     * @param array                     $data
60
     */
61
    private static function populateAAD(JWEInterface &$jwe, array $data)
62
    {
63
        if (array_key_exists('aad', $data)) {
64
            $jwe = $jwe->withAAD($data['aad']);
65
        }
66
    }
67
68
    /**
69
     * @param \Jose\Object\JWEInterface $jwe
70
     * @param array                     $data
71
     */
72
    private static function populateTag(JWEInterface &$jwe, array $data)
73
    {
74
        if (array_key_exists('tag', $data)) {
75
            $jwe = $jwe->withTag(Base64Url::decode($data['tag']));
76
        }
77
    }
78
79
    /**
80
     * @param \Jose\Object\JWEInterface $jwe
81
     * @param array                     $data
82
     */
83
    private static function populateSharedProtectedHeaders(JWEInterface &$jwe, array $data)
84
    {
85
        if (array_key_exists('protected', $data)) {
86
            $jwe = $jwe->withEncodedSharedProtectedHeaders($data['protected']);
87
            $jwe = $jwe->withSharedProtectedHeaders(json_decode(Base64Url::decode($data['protected']), true));
88
        }
89
    }
90
91
    /**
92
     * @param \Jose\Object\JWEInterface $jwe
93
     * @param array                     $data
94
     */
95
    private static function populateSharedHeaders(JWEInterface &$jwe, array $data)
96
    {
97
        if (array_key_exists('unprotected', $data)) {
98
            $jwe = $jwe->withSharedHeaders($data['unprotected']);
99
        }
100
    }
101
102
    /**
103
     * @param array $data
104
     *
105
     * @return array
106
     */
107
    private static function getRecipientHeaders(array $data)
108
    {
109
        if (array_key_exists('header', $data)) {
110
            return $data['header'];
111
        }
112
113
        return [];
114
    }
115
116
    /**
117
     * @param array $data
118
     *
119
     * @return null|string
120
     */
121
    private static function getRecipientEncryptedKey(array $data)
122
    {
123
        if (array_key_exists('encrypted_key', $data)) {
124
            return Base64Url::decode($data['encrypted_key']);
125
        }
126
127
        return null;
128
    }
129
}
130