Completed
Push — master ( 8fd767...f9358d )
by Luís
227:26 queued 225:31
created

Configuration::setBuilderFactory()   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 callable|null
56
     */
57
    private $builderFactory;
58
59
    /**
60
     * @var Constraint[]
61
     */
62
    private $validationConstraints = [];
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 3
    private function getBuilderFactory(): callable
95
    {
96 3
        if ($this->builderFactory === null) {
97
            $this->builderFactory = function (): Builder {
98 2
                return new Token\Builder($this->getEncoder());
99
            };
100
        }
101
102 3
        return $this->builderFactory;
103
    }
104
105 1
    public function setBuilderFactory(callable $builderFactory): void
106
    {
107 1
        $this->builderFactory = $builderFactory;
108 1
    }
109
110 3
    public function createBuilder(): Builder
111
    {
112 3
        return ($this->getBuilderFactory())();
113
    }
114
115 3
    public function getParser(): Parser
116
    {
117 3
        if ($this->parser === null) {
118 2
            $this->parser = new Token\Parser($this->getDecoder());
119
        }
120
121 3
        return $this->parser;
122
    }
123
124 1
    public function setParser(Parser $parser): void
125
    {
126 1
        $this->parser = $parser;
127 1
    }
128
129 3
    public function getSigner(): Signer
130
    {
131 3
        return $this->signer;
132
    }
133
134 3
    public function getSigningKey(): Key
135
    {
136 3
        return $this->signingKey;
137
    }
138
139 3
    public function getVerificationKey(): Key
140
    {
141 3
        return $this->verificationKey;
142
    }
143
144 2
    private function getEncoder(): Parsing\Encoder
145
    {
146 2
        if ($this->encoder === null) {
147 1
            $this->encoder = new Parsing\Parser();
148
        }
149
150 2
        return $this->encoder;
151
    }
152
153 1
    public function setEncoder(Parsing\Encoder $encoder): void
154
    {
155 1
        $this->encoder = $encoder;
156 1
    }
157
158 2
    private function getDecoder(): Parsing\Decoder
159
    {
160 2
        if ($this->decoder === null) {
161 1
            $this->decoder = new Parsing\Parser();
162
        }
163
164 2
        return $this->decoder;
165
    }
166
167 1
    public function setDecoder(Parsing\Decoder $decoder): void
168
    {
169 1
        $this->decoder = $decoder;
170 1
    }
171
172 2
    public function getValidator(): Validator
173
    {
174 2
        if ($this->validator === null) {
175 1
            $this->validator = new Validation\Validator();
176
        }
177
178 2
        return $this->validator;
179
    }
180
181 1
    public function setValidator(Validator $validator): void
182
    {
183 1
        $this->validator = $validator;
184 1
    }
185
186
    /**
187
     * @return Constraint[]
188
     */
189 2
    public function getValidationConstraints(): array
190
    {
191 2
        return $this->validationConstraints;
192
    }
193
194 1
    public function setValidationConstraints(Constraint ...$validationConstraints): void
195
    {
196 1
        $this->validationConstraints = $validationConstraints;
197 1
    }
198
}
199