Failed Conditions
Push — master ( 1a5cd4...b0e5e0 )
by Florent
10:14
created

StorableJWKSet::hasFileBeenUpdated()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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