Completed
Push — master ( 13ada5...8d8bc4 )
by Luís
10s
created

Configuration::setValidationConstraints()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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