1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the tmilos/jose-jwt package. |
5
|
|
|
* |
6
|
|
|
* (c) Milos Tomic <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Tmilos\JoseJwt\Random; |
13
|
|
|
|
14
|
|
|
use Tmilos\JoseJwt\Error\JoseJwtException; |
15
|
|
|
|
16
|
|
|
class FallbackRandomGenerator implements RandomGenerator |
17
|
|
|
{ |
18
|
|
|
/** @var RandomGenerator|null */ |
19
|
|
|
private $first; |
20
|
|
|
|
21
|
|
|
/** @var RandomGenerator|null */ |
22
|
|
|
private $second; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param RandomGenerator $first |
26
|
|
|
* @param RandomGenerator $second |
27
|
|
|
*/ |
28
|
|
|
public function __construct(RandomGenerator $first = null, RandomGenerator $second = null) |
29
|
|
|
{ |
30
|
|
|
$this->first = $first; |
31
|
|
|
$this->second = $second; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return RandomGenerator|null |
36
|
|
|
*/ |
37
|
|
|
public function getFirst() |
38
|
|
|
{ |
39
|
|
|
return $this->first; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param RandomGenerator|null $first |
44
|
|
|
* |
45
|
|
|
* @return FallbackRandomGenerator |
46
|
|
|
*/ |
47
|
|
|
public function setFirst(RandomGenerator $first = null) |
48
|
|
|
{ |
49
|
|
|
$this->first = $first; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return RandomGenerator|null |
56
|
|
|
*/ |
57
|
|
|
public function getSecond() |
58
|
|
|
{ |
59
|
|
|
return $this->second; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param RandomGenerator|null $second |
64
|
|
|
* |
65
|
|
|
* @return FallbackRandomGenerator |
66
|
|
|
*/ |
67
|
|
|
public function setSecond(RandomGenerator $second = null) |
68
|
|
|
{ |
69
|
|
|
$this->second = $second; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param int $bytesLength |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function get($bytesLength) |
80
|
|
|
{ |
81
|
|
|
if ($this->first) { |
82
|
|
|
return $this->first->get($bytesLength); |
83
|
|
|
} elseif ($this->second) { |
84
|
|
|
return $this->second->get($bytesLength); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
throw new JoseJwtException('No random generators provided'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|