Completed
Push — master ( 390e4e...b0ec12 )
by Taosikai
12:57
created

PublicConnection   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 0
Metric Value
wmc 9
c 0
b 0
f 0
lcom 2
cbo 1
dl 0
loc 93
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A __call() 0 4 1
A getId() 0 4 1
A getConnection() 0 4 1
A setInitBuffer() 0 4 1
A getInitBuffer() 0 4 1
A pause() 0 4 1
A resume() 0 4 1
A getWaitingDuration() 0 4 1
1
<?php
2
/**
3
 * Spike library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Spike\Server\TunnelServer;
7
8
use React\Socket\ConnectionInterface;
9
10
/**
11
 * @method write($data)
12
 * @method on($eventName, callable $listener)
13
 * @method pipe($dst, array $options = []);
14
 * @method removeListener($eventName, callable $listener);
15
 * @method removeAllListeners($eventName = null);
16
 * @method end($data = null);
17
 */
18
class PublicConnection
19
{
20
    /**
21
     * @var ConnectionInterface
22
     */
23
    protected $connection;
24
25
    /**
26
     * @var string
27
     */
28
    protected $initBuffer;
29
30
    /**
31
     * @var string
32
     */
33
    protected $id;
34
35
    /**
36
     * The create time
37
     * @var int
38
     */
39
    protected $createAt;
40
41
    public function __construct(ConnectionInterface $connection, $initBuffer = '')
42
    {
43
        $this->connection =  $connection;
44
        $this->initBuffer = $initBuffer;
45
        $this->id = spl_object_hash($connection);
46
        $this->createAt = microtime(true);
0 ignored issues
show
Documentation Bug introduced by
The property $createAt was declared of type integer, but microtime(true) is of type double. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
47
    }
48
49
    public function __call($name, $arguments)
50
    {
51
        return call_user_func_array([$this->connection, $name], $arguments);
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getId()
58
    {
59
        return $this->id;
60
    }
61
62
    /**
63
     * @return ConnectionInterface
64
     */
65
    public function getConnection()
66
    {
67
        return $this->connection;
68
    }
69
70
    /**
71
     * @param string $initBuffer
72
     */
73
    public function setInitBuffer($initBuffer)
74
    {
75
        $this->initBuffer = $initBuffer;
76
    }
77
78
    /**
79
     * @return string
80
     */
81
    public function getInitBuffer()
82
    {
83
        return $this->initBuffer;
84
    }
85
86
    /**
87
     * Pauses the connection
88
     */
89
    public function pause()
90
    {
91
        $this->connection->pause();
92
    }
93
94
    /**
95
     * Resumes the connection
96
     */
97
    public function resume()
98
    {
99
        $this->connection->resume();
100
    }
101
102
    /**
103
     * Gets the waiting duration of the connection
104
     * @return float
105
     */
106
    public function getWaitingDuration()
107
    {
108
        return microtime(true) - $this->createAt;
109
    }
110
}