Passed
Pull Request — v3 (#613)
by
unknown
04:35 queued 02:19
created

Core::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 5
crap 1
1
<?php
2
/**
3
 * This file is part of GameQ.
4
 *
5
 * GameQ is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * GameQ is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace GameQ\Query;
20
21
/**
22
 * Core for the query mechanisms
23
 *
24
 * @author Austin Bischoff <[email protected]>
25
 */
26
abstract class Core
27
{
28
29
    /**
30
     * The socket used by this resource
31
     *
32
     * @type null|resource
33
     */
34
    public $socket = null;
35
36
    /**
37
     * The transport type (udp, tcp, etc...)
38
     * See http://php.net/manual/en/transports.php for the supported list
39
     *
40
     * @type string
41
     */
42
    protected $transport = null;
43
44
    /**
45
     * Connection IP address
46
     *
47
     * @type string
48
     */
49
    protected $ip = null;
50
51
    /**
52
     * Connection port
53
     *
54
     * @type int
55
     */
56
    protected $port = null;
57
58
    /**
59
     * The time in seconds to wait before timing out while connecting to the socket
60
     *
61
     * @type int
62
     */
63
    protected $timeout = 3; // Seconds
64
65
    /**
66
     * Socket is blocking?
67
     *
68
     * @type bool
69
     */
70
    protected $blocking = false;
71
72
    /**
73
     * Called when the class is cloned
74
     */
75 12
    public function __clone()
76
    {
77
78
        // Reset the properties for this class when cloned
79 12
        $this->reset();
80 6
    }
81
82
    /**
83
     * Set the connection information for the socket
84
     *
85
     * @param string $transport
86
     * @param string $ip
87
     * @param int    $port
88
     * @param int    $timeout seconds
89
     * @param bool   $blocking
90
     */
91 12
    public function set($transport, $ip, $port, $timeout = 3, $blocking = false)
92
    {
93
94 12
        $this->transport = $transport;
95
96 12
        $this->ip = $ip;
97
98 12
        $this->port = $port;
99
100 12
        $this->timeout = $timeout;
101
102 12
        $this->blocking = $blocking;
103 6
    }
104
105
    /**
106
     * Reset this instance's properties
107
     */
108 12
    public function reset()
109
    {
110
111 12
        $this->transport = null;
112
113 12
        $this->ip = null;
114
115 12
        $this->port = null;
116
117 12
        $this->timeout = 3;
118
119 12
        $this->blocking = false;
120 6
    }
121
122 6
    public function getTransport()
123
    {
124 6
        return $this->transport;
125
    }
126
127 6
    public function getIp()
128
    {
129 6
        return $this->ip;
130
    }
131
132 6
    public function getPort()
133
    {
134 6
        return $this->port;
135
    }
136
137 6
    public function getTimeout()
138
    {
139 6
        return $this->timeout;
140
    }
141
142 6
    public function getBlocking()
143
    {
144 6
        return $this->blocking;
145
    }
146
147
    /**
148
     * Create a new socket
149
     *
150
     * @return void
151
     */
152
    abstract protected function create();
153
154
    /**
155
     * Get the socket
156
     *
157
     * @return mixed
158
     */
159
    abstract public function get();
160
161
    /**
162
     * Write data to the socket
163
     *
164
     * @param string $data
165
     *
166
     * @return int The number of bytes written
167
     */
168
    abstract public function write($data);
169
170
    /**
171
     * Close the socket
172
     *
173
     * @return void
174
     */
175
    abstract public function close();
176
177
    /**
178
     * Read the responses from the socket(s)
179
     *
180
     * @SuppressWarnings(PHPMD.UnusedFormalParameter)
181
     *
182
     * @param array $sockets
183
     * @param int   $timeout
184
     * @param int   $stream_timeout
185
     *
186
     * @return array
187
     */
188
    abstract public function getResponses(array $sockets, $timeout, $stream_timeout);
189
}
190