Completed
Push — master ( 765f89...887aa0 )
by Florent
03:16
created

StorableJWK::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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
final class StorableJWK implements JWKInterface
22
{
23
    /**
24
     * @var \Jose\Object\JWKInterface
25
     */
26
    private $jwk;
27
28
    /**
29
     * @var string
30
     */
31
    private $filename;
32
33
    /**
34
     * @var array
35
     */
36
    private $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 \Jose\Object\JWKInterface
54
     */
55
    private function getJWK()
56
    {
57
        if (null === $this->jwk) {
58
            $this->loadJWK();
59
        }
60
61
        return $this->jwk;
62
    }
63
64
    private function loadJWK()
65
    {
66
        if (file_exists($this->filename)) {
67
            $content = file_get_contents($this->filename);
68
            if (false === $content) {
69
                $this->createJWK();
70
            }
71
            $content = json_decode($content, true);
72
            if (!is_array($content)) {
73
                $this->createJWK();
74
            }
75
            $this->jwk = new JWK($content);
76
        } else {
77
            $this->createJWK();
78
        }
79
    }
80
81
    private function createJWK()
82
    {
83
        $data = JWKFactory::createKey($this->parameters)->getAll();
84
        $data['kid'] = Base64Url::encode(random_bytes(64));
85
        $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...
86
87
        file_put_contents(
88
            $this->filename,
89
            json_encode($this->jwk)
90
        );
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getAll()
97
    {
98
        return $this->getJWK()->getAll();
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function get($key)
105
    {
106
        return $this->getJWK()->get($key);
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function has($key)
113
    {
114
        return $this->getJWK()->has($key);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function thumbprint($hash_algorithm)
121
    {
122
        return $this->getJWK()->thumbprint($hash_algorithm);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function toPublic()
129
    {
130
        return $this->getJWK()->toPublic();
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136
    public function jsonSerialize()
137
    {
138
        return $this->getJWK()->jsonSerialize();
139
    }
140
}
141