Failed Conditions
Push — v7 ( 22f220...514019 )
by Florent
03:13
created

JWS::checkPayloadEncoding()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 7
nop 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 Jose\Component\Core\JWTInterface;
17
18
/**
19
 * Class JWS.
20
 */
21
final class JWS implements JWTInterface
22
{
23
    /**
24
     * @var bool
25
     */
26
    private $isPayloadDetached = false;
27
28
    /**
29
     * @var string|null
30
     */
31
    private $encodedPayload = null;
32
33
    /**
34
     * @var Signature[]
35
     */
36
    private $signatures = [];
37
38
    /**
39
     * @var string|null
40
     */
41
    private $payload = null;
42
43
    /**
44
     * JWS constructor.
45
     *
46
     * @param string|null $payload
47
     * @param string|null $encodedPayload
48
     * @param bool        $isPayloadDetached
49
     */
50
    private function __construct(?string $payload, ?string $encodedPayload = null, bool $isPayloadDetached = false)
51
    {
52
        $this->payload = $payload;
53
        $this->encodedPayload = $encodedPayload;
54
        $this->isPayloadDetached = $isPayloadDetached;
55
    }
56
57
    /**
58
     * @param string|null $payload
59
     * @param string|null $encodedPayload
60
     * @param bool        $isPayloadDetached
61
     *
62
     * @return JWS
63
     */
64
    public static function create(?string $payload, ?string $encodedPayload = null, bool $isPayloadDetached = false): JWS
65
    {
66
        return new self($payload, $encodedPayload, $isPayloadDetached);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function getPayload(): ?string
73
    {
74
        return $this->payload;
75
    }
76
77
    /**
78
     * @return bool
79
     */
80
    public function isPayloadDetached(): bool
81
    {
82
        return $this->isPayloadDetached;
83
    }
84
85
    /**
86
     * @return string|null
87
     */
88
    public function getEncodedPayload(): ?string
89
    {
90
        if (true === $this->isPayloadDetached()) {
91
            return null;
92
        }
93
94
        return $this->encodedPayload;
95
    }
96
97
    /**
98
     * Returns the signature associated with the JWS.
99
     *
100
     * @return Signature[]
101
     */
102
    public function getSignatures(): array
103
    {
104
        return $this->signatures;
105
    }
106
107
    /**
108
     * @param int $id
109
     *
110
     * @return Signature
111
     */
112
    public function getSignature(int $id): Signature
113
    {
114
        if (isset($this->signatures[$id])) {
115
            return $this->signatures[$id];
116
        }
117
118
        throw new \InvalidArgumentException('The signature does not exist.');
119
    }
120
121
    /**
122
     * @param string      $signature
123
     * @param array       $protectedHeaders
124
     * @param string|null $encodedProtectedHeaders
125
     * @param array       $headers
126
     *
127
     * @return JWS
128
     */
129
    public function addSignature(string $signature, array $protectedHeaders, ?string $encodedProtectedHeaders, array $headers = []): JWS
130
    {
131
        $jws = clone $this;
132
        $jws->signatures[] = Signature::create($signature, $protectedHeaders, $encodedProtectedHeaders, $headers);
133
134
        return $jws;
135
    }
136
137
    /**
138
     * Returns the number of signature associated with the JWS.
139
     *
140
     *
141
     * @return int
142
     */
143
    public function countSignatures(): int
144
    {
145
        return count($this->signatures);
146
    }
147
}
148