Signature   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 2
cbo 0
dl 0
loc 116
rs 10
c 0
b 0
f 0

9 Methods

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