Failed Conditions
Push — v7 ( 61ffea...f7e5f1 )
by Florent
04:20
created

Signature::getAllHeaders()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Signature;
15
16
use Assert\Assertion;
17
use Base64Url\Base64Url;
18
19
final class Signature
20
{
21
    /**
22
     * @var null|string
23
     */
24
    private $encodedProtectedHeaders = null;
25
26
    /**
27
     * @var array
28
     */
29
    private $protectedHeaders = [];
30
31
    /**
32
     * @var array
33
     */
34
    private $headers = [];
35
36
    /**
37
     * @var string
38
     */
39
    private $signature;
40
41
    /**
42
     * Signature constructor.
43
     *
44
     * @param string      $signature
45
     * @param null|string $encodedProtectedHeaders
46
     * @param array       $headers
47
     */
48
    private function __construct(string $signature, ?string $encodedProtectedHeaders, array $headers)
49
    {
50
        if (null !== $encodedProtectedHeaders) {
51
            $protected_headers = json_decode(Base64Url::decode($encodedProtectedHeaders), true);
52
            Assertion::isArray($protected_headers, 'Unable to decode the protected headers.');
53
            $this->protectedHeaders = $protected_headers;
54
        }
55
        $this->encodedProtectedHeaders = $encodedProtectedHeaders;
56
        $this->signature = $signature;
57
        $this->headers = $headers;
58
    }
59
60
    /**
61
     * @param string      $signature
62
     * @param string|null $encodedProtectedHeaders
63
     * @param array       $headers
64
     *
65
     * @return Signature
66
     */
67
    public static function create(string $signature, ?string $encodedProtectedHeaders, array $headers = []): Signature
68
    {
69
        return new self($signature, $encodedProtectedHeaders, $headers);
70
    }
71
72
    /**
73
     * The protected header associated with the signature.
74
     *
75
     * @return array
76
     */
77
    public function getProtectedHeaders(): array
78
    {
79
        return $this->protectedHeaders;
80
    }
81
82
    /**
83
     * The unprotected header associated with the signature.
84
     *
85
     * @return array
86
     */
87
    public function getHeaders(): array
88
    {
89
        return $this->headers;
90
    }
91
92
    /**
93
     * The protected header associated with the signature.
94
     *
95
     *
96
     * @return null|string
97
     */
98
    public function getEncodedProtectedHeaders(): ?string
99
    {
100
        return $this->encodedProtectedHeaders;
101
    }
102
103
    /**
104
     * Returns the value of the protected header of the specified key.
105
     *
106
     * @param string $key The key
107
     *
108
     * @return mixed|null Header value
109
     */
110
    public function getProtectedHeader(string $key)
111
    {
112
        if ($this->hasProtectedHeader($key)) {
113
            return $this->getProtectedHeaders()[$key];
114
        }
115
        throw new \InvalidArgumentException(sprintf('The protected header "%s" does not exist', $key));
116
    }
117
118
    /**
119
     * @param string $key The key
120
     *
121
     * @return bool
122
     */
123
    public function hasProtectedHeader(string $key): bool
124
    {
125
        return array_key_exists($key, $this->getProtectedHeaders());
126
    }
127
128
    /**
129
     * Returns the value of the unprotected header of the specified key.
130
     *
131
     * @param string $key The key
132
     *
133
     * @return mixed|null Header value
134
     */
135
    public function getHeader(string $key)
136
    {
137
        if ($this->hasHeader($key)) {
138
            return $this->headers[$key];
139
        }
140
        throw new \InvalidArgumentException(sprintf('The header "%s" does not exist', $key));
141
    }
142
143
    /**
144
     * @param string $key The key
145
     *
146
     * @return bool
147
     */
148
    public function hasHeader(string $key): bool
149
    {
150
        return array_key_exists($key, $this->headers);
151
    }
152
153
    /**
154
     * Returns the value of the signature.
155
     *
156
     * @return string
157
     */
158
    public function getSignature(): string
159
    {
160
        return $this->signature;
161
    }
162
}
163