Failed Conditions
Push — StorableJWKSet ( 8a4b4b...626162 )
by Florent
02:55
created

StorableJWKSet::getJWKSet()   A

Complexity

Conditions 2
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 2
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
final class StorableJWKSet implements StorableJWKSetInterface
0 ignored issues
show
Bug introduced by
There is at least one abstract method in this class. Maybe declare it as abstract, or implement the remaining methods: addKey, count, countKeys, current, getKey, getKeys, hasKey, key, next, offsetExists, offsetGet, offsetSet, offsetUnset, removeKey, rewind, selectKey, valid
Loading history...
22
{
23
    /**
24
     * @var \Jose\Object\JWKSetInterface
25
     */
26
    private $jwkset;
27
28
    /**
29
     * @var string
30
     */
31
    private $filename;
32
33
    /**
34
     * @var array
35
     */
36
    private $parameters;
37
38
    /**
39
     * @var array
40
     */
41
    private $nb_keys;
42
43
    /**
44
     * StorableJWKSet constructor.
45
     *
46
     * @param string $filename
47
     * @param array  $parameters
48
     * @param int    $nb_keys
49
     */
50
    public function __construct($filename, array $parameters, $nb_keys)
51
    {
52
        Assertion::directory(dirname($filename), 'The selected directory does not exist.');
53
        Assertion::writeable(dirname($filename), 'The selected directory is not writable.');
54
        Assertion::integer($nb_keys, 'The key set must contain at least one key.');
55
        Assertion::greaterThan($nb_keys, 0, 'The key set must contain at least one key.');
56
        $this->filename = $filename;
57
        $this->parameters = $parameters;
58
        $this->nb_keys = $nb_keys;
0 ignored issues
show
Documentation Bug introduced by
It seems like $nb_keys of type integer is incompatible with the declared type array of property $nb_keys.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function getFilename()
65
    {
66
        return $this->filename;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function jsonSerialize()
73
    {
74
        return $this->getJWKSet()->jsonSerialize();
75
    }
76
77
    /**
78
     * @return \Jose\Object\JWKSetInterface
79
     */
80
    private function getJWKSet()
81
    {
82
        if (null === $this->jwkset) {
83
            $this->loadJWKSet();
84
        }
85
86
        return $this->jwkset;
87
    }
88
89
    private function loadJWKSet()
90
    {
91
        if (file_exists($this->filename)) {
92
            $content = file_get_contents($this->filename);
93
            if (false === $content) {
94
                $this->createJWKSet();
0 ignored issues
show
Unused Code introduced by
The call to the method Jose\Object\StorableJWKSet::createJWKSet() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
95
            }
96
            $content = json_decode($content, true);
97
            if (!is_array($content)) {
98
                $this->createJWKSet();
0 ignored issues
show
Unused Code introduced by
The call to the method Jose\Object\StorableJWKSet::createJWKSet() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
99
            }
100
            $this->jwkset = new JWKSet($content);
101
        } else {
102
            $this->createJWKSet();
0 ignored issues
show
Unused Code introduced by
The call to the method Jose\Object\StorableJWKSet::createJWKSet() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
103
        }
104
    }
105
106
    private function createJWKSet()
107
    {
108
        /*$data = JWKFactory::createKey($this->parameters)->getAll();
109
        $this->jwkset = JWKFactory::createFromValues($data);
110
111
        file_put_contents(
112
            $this->filename,
113
            json_encode($this->jwkset)
114
        );*/
115
    }
116
}
117