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

Sequence   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 59
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A generateSequence() 0 5 1
A __construct() 0 3 1
A updateSequence() 0 5 1
A newSequence() 0 4 1
A getKey() 0 3 1
A checkSequence() 0 6 2
A getRandInitial() 0 3 1
1
<?php
2
3
namespace kalanis\RemoteRequest\Protocols\Fsp\Session;
4
5
6
use kalanis\RemoteRequest\Interfaces\IRRTranslations;
7
use kalanis\RemoteRequest\RequestException;
8
use kalanis\RemoteRequest\Traits\TLang;
9
10
11
/**
12
 * Class Sequence
13
 * @package kalanis\RemoteRequest\Protocols\Fsp\Session
14
 * Session sequences in FSP
15
 */
16
class Sequence
17
{
18
    use TLang;
19
20
    protected int $key = 0;
21
    protected float $created = 0.0;
22
    protected float $done = 0.0;
23
    protected float $length = 0.0;
24
25 1
    public static function newSequence(?IRRTranslations $lang = null): self
26
    {
27 1
        $lib = new static($lang);
28 1
        return $lib->generateSequence();
29
    }
30
31 4
    final public function __construct(?IRRTranslations $lang = null)
32
    {
33 4
        $this->setRRLang($lang);
34
    }
35
36 3
    public function generateSequence(): self
37
    {
38 3
        $this->key = $this->getRandInitial();
39 3
        $this->created = doubleval(microtime(true));
40 3
        return $this;
41
    }
42
43
    /**
44
     * @return int
45
     * @codeCoverageIgnore because how to process random number?
46
     */
47
    protected function getRandInitial(): int
48
    {
49
        return rand(0, 65535);
50
    }
51
52 3
    public function getKey(): int
53
    {
54 3
        return $this->key;
55
    }
56
57
    /**
58
     * @param int $sequence
59
     * @throws RequestException
60
     * @return $this
61
     */
62 3
    public function checkSequence(int $sequence): self
63
    {
64 3
        if ($this->key != $sequence) {
65 1
            throw new RequestException($this->getRRLang()->rrFspWrongSequence($sequence, $this->key));
66
        }
67 2
        return $this;
68
    }
69
70 2
    public function updateSequence(): self
71
    {
72 2
        $this->done = doubleval(microtime(true));
73 2
        $this->length = $this->done - $this->created;
74 2
        return $this;
75
    }
76
}
77