Completed
Push — master ( d1d02b...e3e5bf )
by Maik
02:45
created

ClientSocket::disconnect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * This file is part of the PHP Generics package.
5
 *
6
 * @package Generics
7
 */
8
namespace Generics\Socket;
9
10
/**
11
 * This class provides a basic client socket implementation
12
 *
13
 * @author Maik Greubel <[email protected]>
14
 */
15
class ClientSocket extends Socket {
16
	
17
	/**
18
	 * Whether the socket is connected
19
	 *
20
	 * @var boolean
21
	 */
22
	private $conntected;
23
	
24
	/**
25
	 * Create a new client socket
26
	 *
27
	 * @param Endpoint $endpoint
28
	 *        	The endpoint to use
29
	 * @param resource $clientHandle
30
	 *        	optional existing client handle
31
	 */
32 6
	public function __construct(Endpoint $endpoint, $clientHandle = null) {
33 6
		$this->endpoint = $endpoint;
34 6
		$this->handle = $clientHandle;
35 6
		$this->conntected = false;
36
		
37 6
		if (! is_resource ( $clientHandle )) {
38 6
			parent::__construct ( $endpoint );
39
		}
40 6
	}
41
	
42
	/**
43
	 * Connect to remote endpoint
44
	 *
45
	 * @throws SocketException
46
	 */
47 5 View Code Duplication
	public function connect() {
48 5
		if (! @socket_connect ( $this->handle, $this->endpoint->getAddress (), $this->endpoint->getPort () )) {
49 1
			$code = socket_last_error ( $this->handle );
50 1
			throw new SocketException ( socket_strerror ( $code ), array (), $code );
51
		}
52 4
		$this->conntected = true;
53 4
	}
54
	
55
	/**
56
	 * Disconnects the socket
57
	 *
58
	 * @throws SocketException
59
	 */
60 3
	public function disconnect() {
61 3
		$this->close ();
62 3
	}
63
	
64
	/**
65
	 * Whether the client is connected
66
	 *
67
	 * @return boolean
68
	 */
69 4
	public function isConnected() {
70 4
		return $this->conntected;
71
	}
72
	
73
	/**
74
	 *
75
	 * @see \Generics\Socket\ClientSocket::disconnect()
76
	 */
77 6
	public function close() {
78 6
		if (! $this->conntected) {
79 6
			throw new SocketException ( "Socket is not connected" );
80
		}
81
		
82 4
		parent::close ();
83 4
		$this->conntected = false;
84 4
	}
85
	
86
	/**
87
	 *
88
	 * {@inheritdoc}
89
	 * @see \Generics\Socket\Socket::isWriteable()
90
	 */
91 1
	public function isWriteable() {
92 1
		if (! $this->isConnected ()) {
93 1
			return false;
94
		}
95
		
96 1
		return parent::isWriteable ();
97
	}
98
}
99