Failed Conditions
Pull Request — master (#42)
by Florent
01:57
created

Signature::hasProtectedHeaderParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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
/**
17
 * Class Signature.
18
 */
19
final class Signature
20
{
21
    /**
22
     * @var null|string
23
     */
24
    private $encodedProtectedHeader;
25
26
    /**
27
     * @var array
28
     */
29
    private $protectedHeader;
30
31
    /**
32
     * @var array
33
     */
34
    private $header;
35
36
    /**
37
     * @var string
38
     */
39
    private $signature;
40
41
    /**
42
     * Signature constructor.
43
     *
44
     * @param string      $signature
45
     * @param array       $protectedHeader
46
     * @param null|string $encodedProtectedHeader
47
     * @param array       $header
48
     */
49
    private function __construct(string $signature, array $protectedHeader, ?string $encodedProtectedHeader, array $header)
50
    {
51
        $this->protectedHeader = null === $encodedProtectedHeader ? [] : $protectedHeader;
52
        $this->encodedProtectedHeader = $encodedProtectedHeader;
53
        $this->signature = $signature;
54
        $this->header = $header;
55
    }
56
57
    /**
58
     * @param string      $signature
59
     * @param array       $protectedHeader
60
     * @param string|null $encodedProtectedHeader
61
     * @param array       $header
62
     *
63
     * @return Signature
64
     */
65
    public static function create(string $signature, array $protectedHeader, ?string $encodedProtectedHeader, array $header = []): self
66
    {
67
        return new self($signature, $protectedHeader, $encodedProtectedHeader, $header);
68
    }
69
70
    /**
71
     * The protected header associated with the signature.
72
     *
73
     * @return array
74
     */
75
    public function getProtectedHeader(): array
76
    {
77
        return $this->protectedHeader;
78
    }
79
80
    /**
81
     * The unprotected header associated with the signature.
82
     *
83
     * @return array
84
     */
85
    public function getHeader(): array
86
    {
87
        return $this->header;
88
    }
89
90
    /**
91
     * The protected header associated with the signature.
92
     *
93
     *
94
     * @return null|string
95
     */
96
    public function getEncodedProtectedHeader(): ?string
97
    {
98
        return $this->encodedProtectedHeader;
99
    }
100
101
    /**
102
     * Returns the value of the protected header of the specified key.
103
     *
104
     * @param string $key The key
105
     *
106
     * @return mixed|null Header value
107
     */
108
    public function getProtectedHeaderParameter(string $key)
109
    {
110
        if ($this->hasProtectedHeaderParameter($key)) {
111
            return $this->getProtectedHeader()[$key];
112
        }
113
114
        throw new \InvalidArgumentException(sprintf('The protected header "%s" does not exist', $key));
115
    }
116
117
    /**
118
     * @param string $key The key
119
     *
120
     * @return bool
121
     */
122
    public function hasProtectedHeaderParameter(string $key): bool
123
    {
124
        return array_key_exists($key, $this->getProtectedHeader());
125
    }
126
127
    /**
128
     * Returns the value of the unprotected header of the specified key.
129
     *
130
     * @param string $key The key
131
     *
132
     * @return mixed|null Header value
133
     */
134
    public function getHeaderParameter(string $key)
135
    {
136
        if ($this->hasHeaderParameter($key)) {
137
            return $this->header[$key];
138
        }
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 hasHeaderParameter(string $key): bool
149
    {
150
        return array_key_exists($key, $this->header);
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