1 | <?php |
||
10 | abstract class GameapAbstractAdapter extends AbstractAdapter |
||
11 | { |
||
12 | /** |
||
13 | * @var GdaemonFiles |
||
14 | */ |
||
15 | protected $connection; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $host; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | protected $port = 31717; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $timeout = 10; |
||
31 | |||
32 | /** |
||
33 | * @var SafeStorage |
||
34 | */ |
||
35 | protected $safeStorage; |
||
36 | |||
37 | protected $privateKey; |
||
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | protected $permPublic = 0744; |
||
43 | |||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | protected $permPrivate = 0700; |
||
48 | |||
49 | /** |
||
50 | * Connect to the server. |
||
51 | */ |
||
52 | public function connect() |
||
64 | |||
65 | /** |
||
66 | * Disconnect |
||
67 | */ |
||
68 | 3 | public function disconnect() |
|
69 | { |
||
70 | 3 | if ($this->isConnected()) { |
|
71 | 3 | $this->connection->disconnect(); |
|
72 | 1 | } |
|
73 | 3 | } |
|
74 | |||
75 | /** |
||
76 | * @return GDaemonFiles |
||
77 | */ |
||
78 | 63 | public function getConnection() |
|
90 | |||
91 | /** |
||
92 | * @param GdaemonFiles $connection |
||
93 | */ |
||
94 | 3 | public function setConnection($connection) |
|
95 | { |
||
96 | 3 | $this->connection = $connection; |
|
97 | 3 | } |
|
98 | |||
99 | /** |
||
100 | * Constructor. |
||
101 | * |
||
102 | * @param array $config |
||
103 | */ |
||
104 | public function __construct(array $config) |
||
109 | |||
110 | /** |
||
111 | * Set the config. |
||
112 | * |
||
113 | * @param array $config |
||
114 | * |
||
115 | * @return $this |
||
116 | */ |
||
117 | 3 | public function setConfig(array $config) |
|
118 | { |
||
119 | 3 | foreach ($this->configurable as $setting) { |
|
|
|||
120 | 3 | if ( ! isset($config[$setting])) { |
|
121 | 3 | continue; |
|
122 | } |
||
123 | |||
124 | 3 | $method = 'set' . ucfirst($setting); |
|
125 | |||
126 | 3 | if (method_exists($this, $method)) { |
|
127 | 3 | $this->$method($config[$setting]); |
|
128 | 1 | } |
|
129 | 1 | } |
|
130 | |||
131 | 3 | return $this; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Disconnect on destruction. |
||
136 | */ |
||
137 | public function __destruct() |
||
141 | |||
142 | /** |
||
143 | * Returns the username. |
||
144 | * |
||
145 | * @return string username |
||
146 | */ |
||
147 | 6 | public function getUsername() |
|
148 | { |
||
149 | 6 | $username = $this->safeStorage->retrieveSafely('username'); |
|
150 | |||
151 | 6 | return $username !== null ? $username : 'anonymous'; |
|
152 | } |
||
153 | |||
154 | /** |
||
155 | * Set username. |
||
156 | * |
||
157 | * @param string $username |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | 6 | public function setUsername($username) |
|
162 | { |
||
163 | 6 | $this->safeStorage->storeSafely('username', $username); |
|
164 | |||
165 | 6 | return $this; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Returns the password. |
||
170 | * |
||
171 | * @return string password |
||
172 | */ |
||
173 | 6 | public function getPassword() |
|
174 | { |
||
175 | 6 | return $this->safeStorage->retrieveSafely('password'); |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * Set the password. |
||
180 | * |
||
181 | * @param string $password |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | 6 | public function setPassword($password) |
|
186 | { |
||
187 | 6 | $this->safeStorage->storeSafely('password', $password); |
|
188 | |||
189 | 6 | return $this; |
|
190 | } |
||
191 | |||
192 | /** |
||
193 | * Returns the password. |
||
194 | * |
||
195 | * @return string password |
||
196 | */ |
||
197 | 6 | public function getPrivateKey() |
|
198 | { |
||
199 | 6 | return $this->privateKey; |
|
200 | } |
||
201 | |||
202 | /** |
||
203 | * Set the private key (string or path to local file). |
||
204 | * |
||
205 | * @param string $key |
||
206 | * |
||
207 | * @return $this |
||
208 | */ |
||
209 | 6 | public function setPrivateKey($key) |
|
210 | { |
||
211 | 6 | $this->privateKey = $key; |
|
212 | |||
213 | 6 | return $this; |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * Returns the password. |
||
218 | * |
||
219 | * @return string password |
||
220 | */ |
||
221 | 6 | public function getPrivateKeyPass() |
|
222 | { |
||
223 | 6 | return $this->safeStorage->retrieveSafely('privateKeyPass'); |
|
224 | } |
||
225 | |||
226 | /** |
||
227 | * Set the password. |
||
228 | * |
||
229 | * @param string $password |
||
230 | * |
||
231 | * @return $this |
||
232 | */ |
||
233 | 6 | public function setPrivateKeyPass($privateKeyPass) |
|
234 | { |
||
235 | 6 | $this->safeStorage->storeSafely('privateKeyPass', $privateKeyPass); |
|
236 | |||
237 | 6 | return $this; |
|
238 | } |
||
239 | |||
240 | /** |
||
241 | * Returns the host. |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | 6 | public function getHost() |
|
246 | { |
||
247 | 6 | return $this->host; |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * Set the host. |
||
252 | * |
||
253 | * @param string $host |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | 6 | public function setHost($host) |
|
258 | { |
||
259 | 6 | $this->host = $host; |
|
260 | |||
261 | 6 | return $this; |
|
262 | } |
||
263 | |||
264 | /** |
||
265 | * Returns the port. |
||
266 | * |
||
267 | * @return int |
||
268 | */ |
||
269 | 6 | public function getPort() |
|
270 | { |
||
271 | 6 | return $this->port; |
|
272 | } |
||
273 | |||
274 | /** |
||
275 | * Set the port. |
||
276 | * |
||
277 | * @param int|string $port |
||
278 | * |
||
279 | * @return $this |
||
280 | */ |
||
281 | 6 | public function setPort($port) |
|
282 | { |
||
283 | 6 | $this->port = (int) $port; |
|
284 | |||
285 | 6 | return $this; |
|
286 | } |
||
287 | |||
288 | /** |
||
289 | * Returns the amount of seconds before the connection will timeout. |
||
290 | * |
||
291 | * @return int |
||
292 | */ |
||
293 | 6 | public function getTimeout() |
|
294 | { |
||
295 | 6 | return $this->timeout; |
|
296 | } |
||
297 | |||
298 | /** |
||
299 | * Set the amount of seconds before the connection should timeout. |
||
300 | * |
||
301 | * @param int $timeout |
||
302 | * |
||
303 | * @return $this |
||
304 | */ |
||
305 | 3 | public function setTimeout($timeout) |
|
306 | { |
||
307 | 3 | $this->timeout = (int) $timeout; |
|
308 | |||
309 | 3 | return $this; |
|
310 | } |
||
311 | |||
312 | /** |
||
313 | * Returns the root folder to work from. |
||
314 | * |
||
315 | * @return string |
||
316 | */ |
||
317 | 3 | public function getRoot() |
|
318 | { |
||
319 | 3 | return $this->getPathPrefix(); |
|
320 | } |
||
321 | |||
322 | /** |
||
323 | * Set the root folder to work from. |
||
324 | * |
||
325 | * @param string $root |
||
326 | * |
||
327 | * @return $this |
||
328 | */ |
||
329 | 3 | public function setRoot($root) |
|
330 | { |
||
331 | 3 | $this->setPathPrefix($root); |
|
332 | 3 | return $this; |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * Check if a connection is active. |
||
337 | * |
||
338 | * @return bool |
||
339 | */ |
||
340 | abstract public function isConnected(); |
||
341 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: