1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\RemoteRequest\Protocols\Fsp; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\RemoteRequest\Interfaces\IRRTranslations; |
7
|
|
|
use kalanis\RemoteRequest\RequestException; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Session |
12
|
|
|
* @package kalanis\RemoteRequest\Protocols\Fsp |
13
|
|
|
* Session in FSP |
14
|
|
|
*/ |
15
|
|
|
class Session |
16
|
|
|
{ |
17
|
|
|
/** @var IRRTranslations */ |
18
|
|
|
protected $lang = null; |
19
|
|
|
/** @var string|null */ |
20
|
|
|
protected $host = null; |
21
|
|
|
/** @var int[] */ |
22
|
|
|
protected static $key = null; |
23
|
|
|
/** @var array<string, array<int, Session\Sequence>> */ |
24
|
|
|
protected static $sequence = []; |
25
|
|
|
|
26
|
8 |
|
public function __construct(IRRTranslations $lang) |
27
|
|
|
{ |
28
|
8 |
|
$this->lang = $lang; |
29
|
8 |
|
} |
30
|
|
|
|
31
|
5 |
|
public function setHost(string $host): self |
32
|
|
|
{ |
33
|
5 |
|
$this->host = $host; |
34
|
5 |
|
static::$sequence[$this->host] = []; |
35
|
5 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
4 |
|
public function clear(): self |
39
|
|
|
{ |
40
|
4 |
|
if (empty($this->host)) { |
41
|
4 |
|
static::$key = []; |
42
|
4 |
|
static::$sequence = []; |
43
|
|
|
} else { |
44
|
3 |
|
unset(static::$key[$this->host]); |
45
|
3 |
|
unset(static::$sequence[$this->host]); |
46
|
3 |
|
$this->host = null; |
47
|
|
|
} |
48
|
4 |
|
return $this; |
49
|
|
|
} |
50
|
|
|
|
51
|
4 |
|
public function hasKey(): bool |
52
|
|
|
{ |
53
|
4 |
|
return (empty($this->host))? false : (!empty(static::$key[$this->host])); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @throws RequestException |
58
|
|
|
* @return int |
59
|
|
|
*/ |
60
|
3 |
|
public function getKey(): int |
61
|
|
|
{ |
62
|
3 |
|
$this->checkHost(); |
63
|
2 |
|
return $this->hasKey() ? static::$key[$this->host] : $this->getRandInitial(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return int |
68
|
|
|
* @codeCoverageIgnore because how to process random number? |
69
|
|
|
*/ |
70
|
|
|
protected function getRandInitial(): int |
71
|
|
|
{ |
72
|
|
|
return rand(0, 255); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param int $key |
77
|
|
|
* @throws RequestException |
78
|
|
|
* @return Session |
79
|
|
|
*/ |
80
|
1 |
|
public function setKey(int $key): self |
81
|
|
|
{ |
82
|
1 |
|
$this->checkHost(); |
83
|
1 |
|
static::$key[$this->host] = $key; |
84
|
1 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @throws RequestException |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
3 |
|
public function getSequence(): int |
92
|
|
|
{ |
93
|
3 |
|
return $this->generateSequence()->getKey(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $sequence |
98
|
|
|
* @throws RequestException |
99
|
|
|
* @return $this |
100
|
|
|
*/ |
101
|
2 |
|
public function updateSequence(int $sequence): self |
102
|
|
|
{ |
103
|
2 |
|
$this->getLastSequence()->checkSequence($sequence)->updateSequence(); |
104
|
1 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @throws RequestException |
109
|
|
|
* @return Session\Sequence |
110
|
|
|
*/ |
111
|
3 |
|
protected function generateSequence(): Session\Sequence |
112
|
|
|
{ |
113
|
3 |
|
$this->checkHost(); |
114
|
2 |
|
$sequence = $this->sequencer(); |
115
|
2 |
|
static::$sequence[$this->host][] = $sequence; |
116
|
2 |
|
return $sequence; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return Session\Sequence |
121
|
|
|
* @codeCoverageIgnore it contains generator! |
122
|
|
|
*/ |
123
|
|
|
protected function sequencer(): Session\Sequence |
124
|
|
|
{ |
125
|
|
|
return Session\Sequence::newSequence($this->lang); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @throws RequestException |
130
|
|
|
* @return Session\Sequence |
131
|
|
|
*/ |
132
|
2 |
|
protected function getLastSequence(): Session\Sequence |
133
|
|
|
{ |
134
|
2 |
|
$this->checkHost(); |
135
|
2 |
|
$last = end(static::$sequence[$this->host]); |
136
|
2 |
|
if (false === $last) { |
137
|
1 |
|
throw new RequestException($this->lang->rrFspEmptySequence()); |
138
|
|
|
} |
139
|
1 |
|
return $last; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @throws RequestException |
144
|
|
|
* @return $this |
145
|
|
|
*/ |
146
|
6 |
|
protected function checkHost(): self |
147
|
|
|
{ |
148
|
6 |
|
if (empty($this->host)) { |
149
|
2 |
|
throw new RequestException($this->lang->rrFspEmptyHost()); |
150
|
|
|
} |
151
|
4 |
|
return $this; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|