Completed
Branch develop (8ddc4a)
by Florent
02:46
created

Signature::withEncodedProtectedHeaders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 11
nc 3
nop 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A Signature::withProtectedHeaders() 0 10 2
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 Base64Url\Base64Url;
15
16
/**
17
 * Class SignatureInstruction.
18
 */
19
final class Signature implements SignatureInterface
20
{
21
    /**
22
     * @var null|string
23
     */
24
    private $encoded_protected_headers = null;
25
26
    /**
27
     * @var array
28
     */
29
    private $protected_headers = [];
30
31
    /**
32
     * @var array
33
     */
34
    private $headers = [];
35
36
    /**
37
     * @var string
38
     */
39
    private $signature;
40
41
    /**
42
     * @var \Jose\Object\JWKInterface
43
     */
44
    private $signature_key;
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function getProtectedHeaders()
50
    {
51
        return $this->protected_headers;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getHeaders()
58
    {
59
        return $this->headers;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function getEncodedProtectedHeaders()
66
    {
67
        return $this->encoded_protected_headers;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function withProtectedHeaders(array $protected_headers)
74
    {
75
        $signature = clone $this;
76
        $signature->protected_headers = $protected_headers;
77
        if (!empty($protected_headers)) {
78
            $signature->encoded_protected_headers = Base64Url::encode(json_encode($signature->protected_headers));
79
        }
80
81
        return $signature;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function getProtectedHeader($key)
88
    {
89
        if ($this->hasProtectedHeader($key)) {
90
            return $this->protected_headers[$key];
91
        }
92
        throw new \InvalidArgumentException(sprintf('The protected header "%s" does not exist', $key));
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function hasProtectedHeader($key)
99
    {
100
        return array_key_exists($key, $this->protected_headers);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function withHeaders(array $headers)
107
    {
108
        $signature = clone $this;
109
        $signature->headers = $headers;
110
111
        return $signature;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function withHeader($key, $value)
118
    {
119
        $signature = clone $this;
120
        $signature->headers[$key] = $value;
121
122
        return $signature;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getHeader($key)
129
    {
130
        if ($this->hasHeader($key)) {
131
            return $this->headers[$key];
132
        }
133
        throw new \InvalidArgumentException(sprintf('The header "%s" does not exist', $key));
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function hasHeader($key)
140
    {
141
        return array_key_exists($key, $this->headers);
142
    }
143
144
    public function getAllHeaders()
145
    {
146
        return array_merge(
147
            $this->getProtectedHeaders(),
148
            $this->getHeaders()
149
        );
150
    }
151
152
    /**
153
     * {@inheritdoc}
154
     */
155
    public function getSignature()
156
    {
157
        return $this->signature;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function withSignature($values)
164
    {
165
        $signature = clone $this;
166
        $signature->signature = $values;
167
168
        return $signature;
169
    }
170
171
    /**
172
     * @return \Jose\Object\JWKInterface
173
     */
174
    public function getSignatureKey()
175
    {
176
        return $this->signature_key;
177
    }
178
179
    /**
180
     * @param \Jose\Object\JWKInterface $signature_key
181
     *
182
     * @return \Jose\Object\SignatureInterface
183
     */
184
    public function withSignatureKey(JWKInterface $signature_key)
185
    {
186
        $signature = clone $this;
187
        $signature->signature_key = $signature_key;
188
189
        return $signature;
190
    }
191
}
192