Reconciled   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 14
dl 0
loc 59
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A free() 0 3 1
A wait() 0 3 1
A fail() 0 7 2
A done() 0 7 2
A shutdown() 0 4 2
1
<?php
2
/**
3
 * Reconciled of packets
4
 * User: moyo
5
 * Date: 2018/8/1
6
 * Time: 10:08 PM
7
 */
8
9
namespace Carno\HRPC\Accel\Transport;
10
11
use Throwable;
12
13
class Reconciled
14
{
15
    /**
16
     * @var Packet[]
17
     */
18
    private $packets = [];
19
20
    /**
21
     * @param int $seq
22
     * @return Packet
23
     */
24
    public function wait(int $seq) : Packet
25
    {
26
        return $this->packets[$seq] = new Packet($seq, $this);
27
    }
28
29
    /**
30
     * @param int $seq
31
     * @param string $message
32
     * @return bool
33
     */
34
    public function done(int $seq, string $message) : bool
35
    {
36
        if ($packet = $this->packets[$seq] ?? null) {
37
            unset($this->packets[$seq]);
38
            return $packet->received($message);
39
        }
40
        return false;
41
    }
42
43
    /**
44
     * @param int $seq
45
     * @param Throwable $e
46
     * @return bool
47
     */
48
    public function fail(int $seq, Throwable $e = null) : bool
49
    {
50
        if ($packet = $this->packets[$seq] ?? null) {
51
            unset($this->packets[$seq]);
52
            return $packet->failure($e);
53
        }
54
        return false;
55
    }
56
57
    /**
58
     * @param int $seq
59
     */
60
    public function free(int $seq) : void
61
    {
62
        unset($this->packets[$seq]);
63
    }
64
65
    /**
66
     * @param Throwable $e
67
     */
68
    public function shutdown(Throwable $e = null) : void
69
    {
70
        foreach (array_keys($this->packets) as $seq) {
71
            $this->fail($seq, $e);
72
        }
73
    }
74
}
75