Completed
Push — master ( 887aa0...057259 )
by Florent
02:35
created

StorableJWK::getFilename()   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 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 StorableJWK
20
 */
21
final class StorableJWK implements StorableJWKInterface
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
     * {@inheritdoc}
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
    private function getJWK()
112
    {
113
        if (null === $this->jwk) {
114
            $this->loadJWK();
115
        }
116
117
        return $this->jwk;
118
    }
119
120
    private function loadJWK()
121
    {
122
        if (file_exists($this->filename)) {
123
            $content = file_get_contents($this->filename);
124
            if (false === $content) {
125
                $this->createJWK();
126
            }
127
            $content = json_decode($content, true);
128
            if (!is_array($content)) {
129
                $this->createJWK();
130
            }
131
            $this->jwk = new JWK($content);
132
        } else {
133
            $this->createJWK();
134
        }
135
    }
136
137
    private function createJWK()
138
    {
139
        $data = JWKFactory::createKey($this->parameters)->getAll();
140
        $data['kid'] = Base64Url::encode(random_bytes(64));
141
        $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...
142
143
        file_put_contents(
144
            $this->filename,
145
            json_encode($this->jwk)
146
        );
147
    }
148
}
149