Failed Conditions
Pull Request — master (#292)
by
unknown
06:01 queued 02:17
created

RotatableJWKSet::rotateIfNeeded()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 7
nc 4
nop 0
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
/**
15
 * Class RotatableJWKSet.
16
 */
17
final class RotatableJWKSet extends StorableJWKSet implements RotatableInterface
18
{
19
    /**
20
     * Interval at which keys should be rotated.
21
     *
22
     * @var int|null
23
     */
24
    private $interval;
25
26
    /**
27
     * RotatableJWKSet constructor.
28
     *
29
     * @param string   $filename
30
     * @param array    $parameters
31
     * @param int      $nb_keys
32
     * @param int|null $interval
33
     */
34
    public function __construct($filename, array $parameters, $nb_keys, $interval = null)
35
    {
36
        parent::__construct($filename, $parameters, $nb_keys);
37
38
        $this->interval = $interval;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getJWKSet()
45
    {
46
        // Check if we need to rotate keys upon every interaction with the underlying JWK set
47
        $this->rotateIfNeeded();
48
49
        return parent::getJWKSet();
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function rotate()
56
    {
57
        $jwkset = parent::getJWKSet();
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getJWKSet() instead of rotate()). Are you sure this is correct? If so, you might want to change this to $this->getJWKSet().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
58
59
        // Remove last key in set
60
        $jwkset->removeKey($jwkset->countKeys() - 1);
61
62
        // Prepend new key to set
63
        $jwkset->prependKey($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\JWKSetInterface::prependKey() 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...
64
65
        // Save new key set
66
        $this->saveObject($jwkset);
67
    }
68
69
    /**
70
     * Rotate key set if last modification time is due.
71
     */
72
    private function rotateIfNeeded()
73
    {
74
        if (isset($this->interval) && $this->interval >= 0) {
75
            $modificationTime = $this->getLastModificationTime();
76
77
            if (null === $modificationTime) {
78
                $this->regen();
79
            } elseif (($modificationTime + $this->interval) < time()) {
80
                $this->rotate();
81
            }
82
        }
83
    }
84
}
85