Failed Conditions
Pull Request — master (#42)
by Florent
01:57
created

JWE::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 8

How to fix   Many Parameters   

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-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;
15
16
use Jose\Component\Core\JWT;
17
18
/**
19
 * Class JWE.
20
 */
21
final class JWE implements JWT
22
{
23
    /**
24
     * @var Recipient[]
25
     */
26
    private $recipients = [];
27
28
    /**
29
     * @var string|null
30
     */
31
    private $ciphertext = null;
32
33
    /**
34
     * @var string
35
     */
36
    private $iv;
37
38
    /**
39
     * @var string|null
40
     */
41
    private $aad = null;
42
43
    /**
44
     * @var string
45
     */
46
    private $tag;
47
48
    /**
49
     * @var array
50
     */
51
    private $sharedHeader = [];
52
53
    /**
54
     * @var array
55
     */
56
    private $sharedProtectedHeader = [];
57
58
    /**
59
     * @var string|null
60
     */
61
    private $encodedSharedProtectedHeader = null;
62
63
    /**
64
     * @var string|null
65
     */
66
    private $payload = null;
67
68
    /**
69
     * JWE constructor.
70
     *
71
     * @param string      $ciphertext
72
     * @param string      $iv
73
     * @param string      $tag
74
     * @param null|string $aad
75
     * @param array       $sharedHeader
76
     * @param array       $sharedProtectedHeader
77
     * @param null|string $encodedSharedProtectedHeader
78
     * @param array       $recipients
79
     */
80
    private function __construct(string $ciphertext, string $iv, string $tag, ?string $aad = null, array $sharedHeader = [], array $sharedProtectedHeader = [], ?string $encodedSharedProtectedHeader = null, array $recipients = [])
81
    {
82
        $this->ciphertext = $ciphertext;
83
        $this->iv = $iv;
84
        $this->aad = $aad;
85
        $this->tag = $tag;
86
        $this->sharedHeader = $sharedHeader;
87
        $this->sharedProtectedHeader = $sharedProtectedHeader;
88
        $this->encodedSharedProtectedHeader = $encodedSharedProtectedHeader;
89
        $this->recipients = $recipients;
90
    }
91
92
    /**
93
     * @param string      $ciphertext
94
     * @param string      $iv
95
     * @param string      $tag
96
     * @param null|string $aad
97
     * @param array       $sharedHeader
98
     * @param array       $sharedProtectedHeader
99
     * @param null|string $encodedSharedProtectedHeader
100
     * @param array       $recipients
101
     *
102
     * @return JWE
103
     */
104
    public static function create(string $ciphertext, string $iv, string $tag, ?string $aad = null, array $sharedHeader = [], array $sharedProtectedHeader = [], ?string $encodedSharedProtectedHeader = null, array $recipients = []): self
105
    {
106
        return new self($ciphertext, $iv, $tag, $aad, $sharedHeader, $sharedProtectedHeader, $encodedSharedProtectedHeader, $recipients);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getPayload(): ?string
113
    {
114
        return $this->payload;
115
    }
116
117
    /**
118
     * @param string $payload
119
     *
120
     * @return JWE
121
     */
122
    public function withPayload(string $payload): self
123
    {
124
        $clone = clone $this;
125
        $clone->payload = $payload;
126
127
        return $clone;
128
    }
129
130
    /**
131
     * Returns the number of recipients associated with the JWS.
132
     *
133
     * @return int
134
     */
135
    public function countRecipients(): int
136
    {
137
        return count($this->recipients);
138
    }
139
140
    /**
141
     * @return bool
142
     */
143
    public function isEncrypted(): bool
144
    {
145
        return null !== $this->getCiphertext();
146
    }
147
148
    /**
149
     * Returns the recipients associated with the JWS.
150
     *
151
     * @return Recipient[]
152
     */
153
    public function getRecipients(): array
154
    {
155
        return $this->recipients;
156
    }
157
158
    /**
159
     * @param int $id
160
     *
161
     * @return Recipient
162
     */
163
    public function getRecipient(int $id): Recipient
164
    {
165
        if (!array_key_exists($id, $this->recipients)) {
166
            throw new \InvalidArgumentException('The recipient does not exist.');
167
        }
168
169
        return $this->recipients[$id];
170
    }
171
172
    /**
173
     * @return string|null The cyphertext
174
     */
175
    public function getCiphertext(): ?string
176
    {
177
        return $this->ciphertext;
178
    }
179
180
    /**
181
     * @return string|null
182
     */
183
    public function getAAD(): ?string
184
    {
185
        return $this->aad;
186
    }
187
188
    /**
189
     * @return string|null
190
     */
191
    public function getIV(): ?string
192
    {
193
        return $this->iv;
194
    }
195
196
    /**
197
     * @return string|null
198
     */
199
    public function getTag(): ?string
200
    {
201
        return $this->tag;
202
    }
203
204
    /**
205
     * @return string
206
     */
207
    public function getEncodedSharedProtectedHeader(): string
208
    {
209
        return $this->encodedSharedProtectedHeader ?? '';
210
    }
211
212
    /**
213
     * @return array
214
     */
215
    public function getSharedProtectedHeader(): array
216
    {
217
        return $this->sharedProtectedHeader;
218
    }
219
220
    /**
221
     * @param string $key The key
222
     *
223
     * @return mixed|null Header value
224
     */
225
    public function getSharedProtectedHeaderParameter(string $key)
226
    {
227
        if ($this->hasSharedProtectedHeaderParameter($key)) {
228
            return $this->sharedProtectedHeader[$key];
229
        }
230
231
        throw new \InvalidArgumentException(sprintf('The shared protected header "%s" does not exist.', $key));
232
    }
233
234
    /**
235
     * @param string $key The key
236
     *
237
     * @return bool
238
     */
239
    public function hasSharedProtectedHeaderParameter(string $key): bool
240
    {
241
        return array_key_exists($key, $this->sharedProtectedHeader);
242
    }
243
244
    /**
245
     * @return array
246
     */
247
    public function getSharedHeader(): array
248
    {
249
        return $this->sharedHeader;
250
    }
251
252
    /**
253
     * @param string $key The key
254
     *
255
     * @return mixed|null Header value
256
     */
257
    public function getSharedHeaderParameter(string $key)
258
    {
259
        if ($this->hasSharedHeaderParameter($key)) {
260
            return $this->sharedHeader[$key];
261
        }
262
263
        throw new \InvalidArgumentException(sprintf('The shared header "%s" does not exist.', $key));
264
    }
265
266
    /**
267
     * @param string $key The key
268
     *
269
     * @return bool
270
     */
271
    public function hasSharedHeaderParameter(string $key): bool
272
    {
273
        return array_key_exists($key, $this->sharedHeader);
274
    }
275
}
276