Passed
Push — master ( 06f2b4...a60608 )
by Petr
09:01
created

Session   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 136
ccs 43
cts 43
cp 1
rs 10
c 0
b 0
f 0
wmc 18

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setKey() 0 5 1
A clear() 0 11 2
A hasKey() 0 3 2
A generateSequence() 0 6 1
A getLastSequence() 0 8 2
A __construct() 0 3 1
A setHost() 0 5 1
A updateSequence() 0 4 1
A getSequence() 0 3 1
A checkHost() 0 6 2
A getKey() 0 4 2
A sequencer() 0 3 1
A getRandInitial() 0 3 1
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols\Fsp;
4
5
6
use kalanis\RemoteRequest\Interfaces\IRRTranslations;
7
use kalanis\RemoteRequest\RequestException;
8
use kalanis\RemoteRequest\Traits\TLang;
9
10
11
/**
12
 * Class Session
13
 * @package kalanis\RemoteRequest\Protocols\Fsp
14
 * Session in FSP
15
 */
16
class Session
17
{
18
    use TLang;
19
20
    protected ?string $host = null;
21
    /** @var int[] */
22
    protected static array $key = [];
23
    /** @var array<string, array<int, Session\Sequence>> */
24
    protected static array $sequence = [];
25
26 8
    public function __construct(?IRRTranslations $lang = null)
27
    {
28 8
        $this->setRRLang($lang);
29
    }
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->getRRLang());
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->getRRLang()->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->getRRLang()->rrFspEmptyHost());
150
        }
151 4
        return $this;
152
    }
153
}
154