Completed
Push — master ( 143878...f45699 )
by Taosikai
14:33
created

Client::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the slince/spike package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Spike\Server;
13
14
use React\Socket\ConnectionInterface;
15
use Spike\Client\ClientInterface;
16
17
class Client implements ClientInterface
18
{
19
    /**
20
     * id.
21
     *
22
     * @var string
23
     */
24
    protected $id;
25
26
    /**
27
     * Client information.
28
     *
29
     * @var array
30
     */
31
    protected $info;
32
33
    /**
34
     * @var \DateTimeInterface
35
     */
36
    protected $activeAt;
37
38
    /**
39
     * @var ConnectionInterface
40
     */
41
    protected $controlConnection;
42
43
    public function __construct($info, ConnectionInterface $controlConnection)
44
    {
45
        $this->info = $info;
46
        $this->controlConnection = $controlConnection;
47
        $this->activeAt = new \DateTime();
48
    }
49
50
    /**
51
     * Sets the control connection for the client.
52
     *
53
     * @param ConnectionInterface $controlConnection
54
     */
55
    public function setControlConnection($controlConnection)
56
    {
57
        $this->controlConnection = $controlConnection;
58
    }
59
60
    /**
61
     * Gets the control connection of the client.
62
     *
63
     * @return ConnectionInterface
64
     */
65
    public function getControlConnection()
66
    {
67
        return $this->controlConnection;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function getId()
74
    {
75
        return $this->id ?: ($this->id = spl_object_hash($this));
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function setId($id)
82
    {
83
        $this->id = $id;
84
    }
85
86
    /**
87
     * Close the client.
88
     */
89
    public function close()
90
    {
91
        $this->controlConnection->end();
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function setActiveAt($activeAt)
98
    {
99
        $this->activeAt = $activeAt;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getActiveAt()
106
    {
107
        return $this->activeAt;
108
    }
109
110
    /**
111
     * Gets the client information.
112
     *
113
     * @return array
114
     */
115
    public function toArray()
116
    {
117
        return array_replace($this->info, [
118
            'id' => $this->getId(),
119
        ]);
120
    }
121
}