Failed Conditions
Push — master ( e08481...7ad838 )
by Florent
03:52 queued 01:57
created

NestedTokenBuilder::create()   D

Complexity

Conditions 10
Paths 21

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 25
nc 21
nop 8

How to fix   Complexity    Many Parameters   

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 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;
15
16
use Jose\Component\Encryption\Serializer\JWESerializerManager;
17
use Jose\Component\Signature\JWSBuilder;
18
use Jose\Component\Signature\Serializer\JWSSerializerManager;
19
20
class NestedTokenBuilder
21
{
22
    /**
23
     * @var JWSBuilder
24
     */
25
    private $jwsBuilder;
26
27
    /**
28
     * @var JWSSerializerManager
29
     */
30
    private $jwsSerializerManager;
31
32
    /**
33
     * @var JWEBuilder
34
     */
35
    private $jweBuilder;
36
37
    /**
38
     * @var JWESerializerManager
39
     */
40
    private $jweSerializerManager;
41
42
    /**
43
     * NestedTokenBuilder constructor.
44
     *
45
     * @param JWEBuilder           $jweBuilder
46
     * @param JWESerializerManager $jweSerializerManager
47
     * @param JWSBuilder           $jwsBuilder
48
     * @param JWSSerializerManager $jwsSerializerManager
49
     */
50
    public function __construct(JWEBuilder $jweBuilder, JWESerializerManager $jweSerializerManager, JWSBuilder $jwsBuilder, JWSSerializerManager $jwsSerializerManager)
51
    {
52
        $this->jweBuilder = $jweBuilder;
53
        $this->jwsSerializerManager = $jwsSerializerManager;
54
        $this->jwsBuilder = $jwsBuilder;
55
        $this->jweSerializerManager = $jweSerializerManager;
56
    }
57
58
    /**
59
     * @param string      $payload
60
     * @param array[]     $signatures
61
     * @param string      $jws_serialization_mode
62
     * @param array       $jweSharedProtectedHeader
63
     * @param array       $jweSharedHeader
64
     * @param array[]     $recipients
65
     * @param string      $jwe_serialization_mode
66
     * @param string|null $aad
67
     *
68
     * @throws \Exception
69
     *
70
     * @return string
71
     */
72
    public function create(string $payload, array $signatures, string $jws_serialization_mode, array $jweSharedProtectedHeader, array $jweSharedHeader, array $recipients, string $jwe_serialization_mode, ?string $aad = null): string
73
    {
74
        $jws = $this->jwsBuilder->create()->withPayload($payload);
75
        foreach ($signatures as $signature) {
76
            if (!is_array($signature) || !array_key_exists('key', $signature)) {
77
                throw new \InvalidArgumentException('The signatures must be an array of arrays containing a key, a protected header and a header');
78
            }
79
            $signature['protected_header'] = array_key_exists('protected_header', $signature) ? $signature['protected_header'] : [];
80
            $signature['header'] = array_key_exists('header', $signature) ? $signature['header'] : [];
81
            $jws = $jws->addSignature($signature['key'], $signature['protected_header'], $signature['header']);
82
        }
83
        $jws = $jws->build();
84
        $token = $this->jwsSerializerManager->serialize($jws_serialization_mode, $jws);
85
86
        $jweSharedProtectedHeader['cty'] = 'JWT';
87
88
        $jwe = $this->jweBuilder
89
            ->create()
90
            ->withPayload($token)
91
            ->withSharedProtectedHeader($jweSharedProtectedHeader)
92
            ->withSharedHeader($jweSharedHeader)
93
            ->withAAD($aad);
94
        foreach ($recipients as $recipient) {
95
            if (!is_array($recipient) || !array_key_exists('key', $recipient)) {
96
                throw new \InvalidArgumentException('The recipients must be an array of arrays containing a key and a header');
97
            }
98
            $recipient['header'] = array_key_exists('header', $recipient) ? $recipient['header'] : [];
99
            $jwe = $jwe->addRecipient($recipient['key'], $recipient['header']);
100
        }
101
        $jwe = $jwe->build();
102
        $token = $this->jweSerializerManager->serialize($jwe_serialization_mode, $jwe);
103
104
        return $token;
105
    }
106
}
107