Completed
Push — v2.0.x ( 7a58b6 )
by Florent
24:58
created

Signature::getEncodedProtectedHeaders()   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-2015 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
use Base64Url\Base64Url;
14
15
/**
16
 * Class SignatureInstruction.
17
 */
18
final class Signature implements SignatureInterface
19
{
20
    /**
21
     * @var null|string
22
     */
23
    private $encoded_protected_headers = null;
24
25
    /**
26
     * @var array
27
     */
28
    private $protected_headers = [];
29
30
    /**
31
     * @var array
32
     */
33
    private $headers = [];
34
35
    /**
36
     * @var string
37
     */
38
    private $signature;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getProtectedHeaders()
44
    {
45
        return $this->protected_headers;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getHeaders()
52
    {
53
        return $this->headers;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function getEncodedProtectedHeaders()
60
    {
61
        return $this->encoded_protected_headers;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function withEncodedProtectedHeaders($encoded_protected_headers)
68
    {
69
        $signature = clone $this;
70
        $signature->encoded_protected_headers = $encoded_protected_headers;
71
        if (empty($encoded_protected_headers)) {
72
            $signature->protected_headers = [];
73
        } else {
74
            $signature->protected_headers = json_decode(Base64Url::decode($encoded_protected_headers), true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode(\Base64Url\B...otected_headers), true) of type * is incompatible with the declared type array of property $protected_headers.

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...
75
        }
76
77
        return $signature;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    public function withProtectedHeaders(array $protected_headers)
84
    {
85
        $signature = clone $this;
86
        $signature->protected_headers = $protected_headers;
87
        $signature->encoded_protected_headers = Base64Url::encode(json_encode($signature->protected_headers));
88
89
        return $signature;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function withProtectedHeader($key, $value)
96
    {
97
        $signature = clone $this;
98
        $signature->protected_headers[$key] = $value;
99
        $signature->encoded_protected_headers = Base64Url::encode(json_encode($signature->protected_headers));
100
101
        return $signature;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function getProtectedHeader($key)
108
    {
109
        if ($this->hasProtectedHeader($key)) {
110
            return $this->protected_headers[$key];
111
        }
112
        throw new \InvalidArgumentException(sprintf('The protected header "%s" does not exist', $key));
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function hasProtectedHeader($key)
119
    {
120
        return array_key_exists($key, $this->protected_headers);
121
    }
122
123
    /**
124
     * {@inheritdoc}
125
     */
126
    public function withHeaders(array $headers)
127
    {
128
        $signature = clone $this;
129
        $signature->headers = $headers;
130
131
        return $signature;
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function withHeader($key, $value)
138
    {
139
        $signature = clone $this;
140
        $signature->headers[$key] = $value;
141
142
        return $signature;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function getHeader($key)
149
    {
150
        if ($this->hasHeader($key)) {
151
            return $this->headers[$key];
152
        }
153
        throw new \InvalidArgumentException(sprintf('The header "%s" does not exist', $key));
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159
    public function hasHeader($key)
160
    {
161
        return array_key_exists($key, $this->headers);
162
    }
163
164
    public function getAllHeaders()
165
    {
166
        return array_merge(
167
            $this->getProtectedHeaders(),
168
            $this->getHeaders()
169
        );
170
    }
171
172
    /**
173
     * {@inheritdoc}
174
     */
175
    public function getSignature()
176
    {
177
        return $this->signature;
178
    }
179
180
    /**
181
     * {@inheritdoc}
182
     */
183
    public function withSignature($values)
184
    {
185
        $signature = clone $this;
186
        $signature->signature = $values;
187
188
        return $signature;
189
    }
190
}
191