Failed Conditions
Pull Request — master (#126)
by Florent
03:58
created

StorableJWK   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 130
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getFilename() 0 4 1
A getAll() 0 4 1
A get() 0 4 1
A has() 0 4 1
A thumbprint() 0 4 1
A toPublic() 0 4 1
A jsonSerialize() 0 4 1
A getJWK() 0 6 1
A loadJWK() 0 16 4
A createJWK() 0 8 1
A save() 0 4 1
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 StorableJWK.
20
 */
21
class StorableJWK implements StorableJWKInterface
22
{
23
    /**
24
     * @var \Jose\Object\JWKInterface
25
     */
26
    protected $jwk;
27
28
    /**
29
     * @var string
30
     */
31
    protected $filename;
32
33
    /**
34
     * @var array
35
     */
36
    protected $parameters;
37
38
    /**
39
     * RotatableJWK constructor.
40
     *
41
     * @param string $filename
42
     * @param array  $parameters
43
     */
44
    public function __construct($filename, array $parameters)
45
    {
46
        Assertion::directory(dirname($filename), 'The selected directory does not exist.');
47
        Assertion::writeable(dirname($filename), 'The selected directory is not writable.');
48
        $this->filename = $filename;
49
        $this->parameters = $parameters;
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    public function getFilename()
56
    {
57
        return $this->filename;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function getAll()
64
    {
65
        return $this->getJWK()->getAll();
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function get($key)
72
    {
73
        return $this->getJWK()->get($key);
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function has($key)
80
    {
81
        return $this->getJWK()->has($key);
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function thumbprint($hash_algorithm)
88
    {
89
        return $this->getJWK()->thumbprint($hash_algorithm);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function toPublic()
96
    {
97
        return $this->getJWK()->toPublic();
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function jsonSerialize()
104
    {
105
        return $this->getJWK()->jsonSerialize();
106
    }
107
108
    /**
109
     * @return \Jose\Object\JWKInterface
110
     */
111
    protected function getJWK()
112
    {
113
        $this->loadJWK();
114
115
        return $this->jwk;
116
    }
117
118
    protected function loadJWK()
119
    {
120
        if (file_exists($this->filename)) {
121
            $content = file_get_contents($this->filename);
122
            if (false === $content) {
123
                $this->createJWK();
124
            }
125
            $content = json_decode($content, true);
126
            if (!is_array($content)) {
127
                $this->createJWK();
128
            }
129
            $this->jwk = new JWK($content);
130
        } else {
131
            $this->createJWK();
132
        }
133
    }
134
135
136
    protected function createJWK()
137
    {
138
        $data = JWKFactory::createKey($this->parameters)->getAll();
139
        $data['kid'] = Base64Url::encode(random_bytes(64));
140
        $this->jwk = JWKFactory::createFromValues($data);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Jose\Factory\JWKFactory::createFromValues($data) can also be of type object<Jose\Object\JWKSet>. However, the property $jwk is declared as type object<Jose\Object\JWKInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
141
142
        $this->save();
143
    }
144
145
146
    protected function save()
147
    {
148
        file_put_contents($this->getFilename(), json_encode($this->jwk));
149
    }
150
}
151