Failed Conditions
Push — v7 ( 46cca8...c1355c )
by Florent
01:52
created

JSONFlattenedSerializer::unserialize()   D

Complexity

Conditions 10
Paths 65

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 22
nc 65
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Encryption\Serializer;
15
16
use Base64Url\Base64Url;
17
use Jose\Component\Encryption\JWE;
18
use Jose\Component\Encryption\Recipient;
19
20
/**
21
 * Class JSONFlattenedSerializer.
22
 */
23
final class JSONFlattenedSerializer implements JWESerializerInterface
24
{
25
    public const NAME = 'jwe_json_flattened';
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function name(): string
31
    {
32
        return self::NAME;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function serialize(JWE $jwe, ?int $recipientIndex = null): string
39
    {
40
        if (null === $recipientIndex) {
41
            $recipientIndex = 0;
42
        }
43
        $recipient = $jwe->getRecipient($recipientIndex);
44
        $data = [
45
            'ciphertext' => Base64Url::encode($jwe->getCiphertext()),
46
            'iv' => Base64Url::encode($jwe->getIV()),
47
            'tag' => Base64Url::encode($jwe->getTag()),
48
        ];
49
        if (null !== $jwe->getAAD()) {
50
            $data['aad'] = Base64Url::encode($jwe->getAAD());
51
        }
52
        if (!empty($jwe->getSharedProtectedHeaders())) {
53
            $data['protected'] = $jwe->getEncodedSharedProtectedHeaders();
54
        }
55
        if (!empty($jwe->getSharedHeaders())) {
56
            $data['unprotected'] = $jwe->getSharedHeaders();
57
        }
58
        if (!empty($recipient->getHeaders())) {
59
            $data['header'] = $recipient->getHeaders();
60
        }
61
        if (null !== $recipient->getEncryptedKey()) {
62
            $data['encrypted_key'] = Base64Url::encode($recipient->getEncryptedKey());
63
        }
64
65
        return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function unserialize(string $input): JWE
72
    {
73
        $data = json_decode($input, true);
74
        if (!is_array($data) || !array_key_exists('ciphertext', $data) || array_key_exists('recipients', $data)) {
75
            throw new \InvalidArgumentException('Unsupported input.');
76
        }
77
78
        $ciphertext = Base64Url::decode($data['ciphertext']);
79
        $iv = Base64Url::decode($data['iv']);
80
        $tag = Base64Url::decode($data['tag']);
81
        $aad = array_key_exists('aad', $data) ? Base64Url::decode($data['aad']) : null;
82
        $encodedSharedProtectedHeader = array_key_exists('protected', $data) ? $data['protected'] : null;
83
        $sharedProtectedHeader = $encodedSharedProtectedHeader ? json_decode(Base64Url::decode($encodedSharedProtectedHeader), true) : [];
84
        $sharedHeader = array_key_exists('unprotected', $data) ? $data['unprotected'] : [];
85
        $encryptedKey = array_key_exists('encrypted_key', $data) ? Base64Url::decode($data['encrypted_key']) : null;
86
        $header = array_key_exists('header', $data) ? $data['header'] : [];
87
88
        return JWE::create(
89
            $ciphertext,
90
            $iv,
91
            $tag,
92
            $aad,
93
            $sharedHeader,
94
            $sharedProtectedHeader,
95
            $encodedSharedProtectedHeader,
96
            [Recipient::create($header, $encryptedKey)]);
97
    }
98
}
99