Completed
Push — master ( 6b3010...8922e4 )
by Luís
34s
created

Configuration::forUnsecuredSigner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of Lcobucci\JWT, a simple library to handle JWT and JWS
4
 *
5
 * @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
6
 */
7
8
declare(strict_types=1);
9
10
namespace Lcobucci\JWT;
11
12
use Lcobucci\Jose\Parsing;
13
use Lcobucci\JWT\Signer\Key;
14
use Lcobucci\JWT\Signer\None;
15
use Lcobucci\JWT\Token;
16
use Lcobucci\JWT\Validation;
17
18
/**
19
 * Configuration container for the JWT Builder and Parser
20
 *
21
 * Serves like a small DI container to simplify the creation and usage
22
 * of the objects.
23
 *
24
 * @author Luís Otávio Cobucci Oblonczyk <[email protected]>
25
 * @since 4.0.0
26
 */
27
final class Configuration
28
{
29
    /**
30
     * @var Parser|null
31
     */
32
    private $parser;
33
34
    /**
35
     * @var Signer
36
     */
37
    private $signer;
38
39
    /**
40
     * @var Key
41
     */
42
    private $signingKey;
43
44
    /**
45
     * @var Key
46
     */
47
    private $verificationKey;
48
49
    /**
50
     * @var Parsing\Encoder|null
51
     */
52
    private $encoder;
53
54
    /**
55
     * @var Parsing\Decoder|null
56
     */
57
    private $decoder;
58
59
    /**
60
     * @var Validator|null
61
     */
62
    private $validator;
63
64 1
    public static function forAsymmetricSigner(
65
        Signer $signer,
66
        Key $signingKey,
67
        Key $verificationKey
68
    ): self {
69 1
        return new self($signer, $signingKey, $verificationKey);
70
    }
71
72 1
    public static function forSymmetricSigner(Signer $signer, Key $key): self
73
    {
74 1
        return new self($signer, $key, $key);
75
    }
76
77 1
    public static function forUnsecuredSigner(): self
78
    {
79 1
        $key = new Key('');
80
81 1
        return new self(new None(), $key, $key);
82
    }
83
84 3
    private function __construct(
85
        Signer $signer,
86
        Key $signingKey,
87
        Key $verificationKey
88
    ) {
89 3
        $this->signer = $signer;
90 3
        $this->signingKey = $signingKey;
91 3
        $this->verificationKey = $verificationKey;
92 3
    }
93
94 2
    public function createBuilder(): Builder
95
    {
96 2
        return new Token\Builder($this->getEncoder());
97
    }
98
99 3
    public function getParser(): Parser
100
    {
101 3
        if ($this->parser === null) {
102 2
            $this->parser = new Token\Parser($this->getDecoder());
103
        }
104
105 3
        return $this->parser;
106
    }
107
108 1
    public function setParser(Parser $parser): void
109
    {
110 1
        $this->parser = $parser;
111 1
    }
112
113 1
    public function getSigner(): Signer
114
    {
115 1
        return $this->signer;
116
    }
117
118 1
    public function getSigningKey(): Key
119
    {
120 1
        return $this->signingKey;
121
    }
122
123 1
    public function getVerificationKey(): Key
124
    {
125 1
        return $this->verificationKey;
126
    }
127
128 2
    private function getEncoder(): Parsing\Encoder
129
    {
130 2
        if ($this->encoder === null) {
131 1
            $this->encoder = new Parsing\Parser();
132
        }
133
134 2
        return $this->encoder;
135
    }
136
137 1
    public function setEncoder(Parsing\Encoder $encoder): void
138
    {
139 1
        $this->encoder = $encoder;
140 1
    }
141
142 2
    private function getDecoder(): Parsing\Decoder
143
    {
144 2
        if ($this->decoder === null) {
145 1
            $this->decoder = new Parsing\Parser();
146
        }
147
148 2
        return $this->decoder;
149
    }
150
151 1
    public function setDecoder(Parsing\Decoder $decoder): void
152
    {
153 1
        $this->decoder = $decoder;
154 1
    }
155
156 2
    public function getValidator(): Validator
157
    {
158 2
        if ($this->validator === null) {
159 1
            $this->validator = new Validation\Validator();
160
        }
161
162 2
        return $this->validator;
163
    }
164
165 1
    public function setValidator(Validator $validator): void
166
    {
167 1
        $this->validator = $validator;
168 1
    }
169
}
170