Failed Conditions
Pull Request — master (#292)
by
unknown
03:25
created

StorableJWKSet   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 228
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 228
c 0
b 0
f 0
wmc 25
lcom 2
cbo 8
rs 10

24 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A current() 0 4 1
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A getKey() 0 4 1
A hasKey() 0 4 1
A getKeys() 0 4 1
A addKey() 0 4 1
A prependKey() 0 4 1
A removeKey() 0 4 1
A countKeys() 0 4 1
A selectKey() 0 4 1
A count() 0 4 1
A jsonSerialize() 0 4 1
A getJWKSet() 0 6 1
A createObjectFromFileContent() 0 4 1
A createNewObject() 0 10 2
A createJWK() 0 7 1
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2017 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\Object;
13
14
use Assert\Assertion;
15
use Base64Url\Base64Url;
16
use Jose\Factory\JWKFactory;
17
18
/**
19
 * Class StorableJWKSet.
20
 */
21
class StorableJWKSet implements StorableInterface, JWKSetInterface
22
{
23
    use Storable;
24
    use JWKSetPEM;
25
26
    /**
27
     * @var array
28
     */
29
    protected $parameters;
30
31
    /**
32
     * @var int
33
     */
34
    protected $nb_keys;
35
36
    /**
37
     * StorableJWKSet constructor.
38
     *
39
     * @param string $filename
40
     * @param array  $parameters
41
     * @param int    $nb_keys
42
     */
43
    public function __construct($filename, array $parameters, $nb_keys)
44
    {
45
        Assertion::integer($nb_keys, 'The key set must contain at least one key.');
46
        Assertion::greaterThan($nb_keys, 0, 'The key set must contain at least one key.');
47
        $this->setFilename($filename);
48
        $this->parameters = $parameters;
49
        $this->nb_keys = $nb_keys;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function current()
56
    {
57
        return $this->getJWKSet()->current();
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function next()
64
    {
65
        $this->getJWKSet()->next();
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function key()
72
    {
73
        return $this->getJWKSet()->key();
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function valid()
80
    {
81
        return $this->getJWKSet()->valid();
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function rewind()
88
    {
89
        $this->getJWKSet()->rewind();
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function offsetExists($offset)
96
    {
97
        return $this->getJWKSet()->offsetExists($offset);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function offsetGet($offset)
104
    {
105
        return $this->getJWKSet()->offsetGet($offset);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function offsetSet($offset, $value)
112
    {
113
        // Not available
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function offsetUnset($offset)
120
    {
121
        // Not available
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    public function getKey($index)
128
    {
129
        return $this->getJWKSet()->getKey($index);
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function hasKey($index)
136
    {
137
        return $this->getJWKSet()->hasKey($index);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function getKeys()
144
    {
145
        return $this->getJWKSet()->getKeys();
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151
    public function addKey(JWKInterface $key)
152
    {
153
        // Not available
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function prependKey(JWKInterface $key)
160
    {
161
        //Not available
162
    }
163
164
    /**
165
     * {@inheritdoc}
166
     */
167
    public function removeKey($index)
168
    {
169
        // Not available
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function countKeys()
176
    {
177
        return $this->getJWKSet()->countKeys();
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function selectKey($type, $algorithm = null, array $restrictions = [])
184
    {
185
        return $this->getJWKSet()->selectKey($type, $algorithm, $restrictions);
186
    }
187
188
    /**
189
     * {@inheritdoc}
190
     */
191
    public function count()
192
    {
193
        return $this->getJWKSet()->count();
194
    }
195
196
    /**
197
     * {@inheritdoc}
198
     */
199
    public function jsonSerialize()
200
    {
201
        return $this->getJWKSet()->jsonSerialize();
202
    }
203
204
    /**
205
     * @return \Jose\Object\JWKSetInterface
206
     */
207
    protected function getJWKSet()
208
    {
209
        $this->loadObjectIfNeeded();
210
211
        return $this->getObject();
212
    }
213
214
    /**
215
     * @param array $file_content
216
     *
217
     * @return \JsonSerializable
218
     */
219
    protected function createObjectFromFileContent(array $file_content)
220
    {
221
        return new JWKSet($file_content);
222
    }
223
224
    /**
225
     * This method creates the JWKSet and populate it with keys.
226
     */
227
    protected function createNewObject()
228
    {
229
        $jwkset = new JWKSet();
230
        for ($i = 0; $i < $this->nb_keys; ++$i) {
231
            $key = $this->createJWK();
232
            $jwkset->addKey($key);
0 ignored issues
show
Bug introduced by
It seems like $key defined by $this->createJWK() on line 231 can also be of type object<Jose\Object\JWKSet>; however, Jose\Object\JWKSet::addKey() does only seem to accept object<Jose\Object\JWKInterface>, 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...
233
        }
234
235
        return $jwkset;
236
    }
237
238
    /**
239
     * @return \Jose\Object\JWKInterface
240
     */
241
    protected function createJWK()
242
    {
243
        $data = JWKFactory::createKey($this->parameters)->getAll();
244
        $data['kid'] = Base64Url::encode(random_bytes(64));
245
246
        return JWKFactory::createFromValues($data);
247
    }
248
}
249