Failed Conditions
Push — JWKSet ( 3580c0 )
by Florent
04:07
created

RotatableJWKSet::getJWKSet()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 3
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
16
/**
17
 * Class RotatableJWKSet.
18
 */
19
final class RotatableJWKSet extends StorableJWKSet implements RotatableJWKSetInterface
20
{
21
    /**
22
     * @var int
23
     */
24
    private $ttl;
25
26
    /**
27
     * RotatableJWKSet constructor.
28
     *
29
     * @param string $filename
30
     * @param array  $parameters
31
     * @param int    $nb_keys
32
     * @param int    $ttl
33
     */
34
    public function __construct($filename, array $parameters, $nb_keys, $ttl)
35
    {
36
        Assertion::integer($ttl);
37
        Assertion::greaterThan($ttl, 0, 'The parameter TTL must be at least 0.');
38
        $this->ttl = $ttl;
39
        parent::__construct($filename, $parameters, $nb_keys);
40
    }
41
42
    /**
43
     * @return \Jose\Object\JWKSetInterface
44
     */
45
    protected function getJWKSet()
46
    {
47
        $jwkset = parent::getJWKSet();
48
        if (null !== $this->getFileLastModificationTime()) {
49
            if ($this->getFileLastModificationTime() + $this->ttl < time()) {
50
                $keys = $jwkset->getKeys();
51
                unset($keys[count($keys) - 1]);
52
                $jwkset = new JWKSet();
53
                $jwkset->addKey($this->createJWK());
0 ignored issues
show
Bug introduced by
It seems like $this->createJWK() targeting Jose\Object\StorableJWKSet::createJWK() 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?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
54
                foreach ($keys as $key) {
55
                    $jwkset->addKey($key);
56
                }
57
                $this->jwkset = $jwkset;
58
                $this->save();
59
            }
60
        }
61
62
        return parent::getJWKSet();
63
    }
64
65
    /**
66
     * @return int|null
67
     */
68
    private function getFileLastModificationTime()
69
    {
70
        if (file_exists($this->getFilename())) {
71
            return filemtime($this->getFilename());
72
        }
73
    }
74
}
75