Completed
Push — master ( eda387...29bdeb )
by Robin
04:14
created

Connection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 4
dl 0
loc 102
ccs 40
cts 50
cp 0.8
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A write() 0 3 1
A clearTillPrompt() 0 8 2
B read() 0 26 6
A isPrompt() 0 3 2
A unknownError() 0 12 3
A close() 0 6 2
1
<?php
2
/**
3
 * Copyright (c) 2014 Robin Appelman <[email protected]>
4
 * This file is licensed under the Licensed under the MIT license:
5
 * http://opensource.org/licenses/MIT
6
 */
7
8
namespace Icewind\SMB\Wrapped;
9
10
use Icewind\SMB\Exception\AuthenticationException;
11
use Icewind\SMB\Exception\ConnectException;
12
use Icewind\SMB\Exception\ConnectionException;
13
use Icewind\SMB\Exception\InvalidHostException;
14
use Icewind\SMB\Exception\NoLoginServerException;
15
16
class Connection extends RawConnection {
17
	const DELIMITER = 'smb:';
18
	const DELIMITER_LENGTH = 4;
19
20
	/** @var Parser */
21
	private $parser;
22
23 255
	public function __construct($command, Parser $parser, $env = array()) {
24 255
		parent::__construct($command, $env);
25 255
		$this->parser = $parser;
26 255
	}
27
28
	/**
29
	 * send input to smbclient
30
	 *
31
	 * @param string $input
32
	 */
33 255
	public function write($input) {
34 255
		parent::write($input . PHP_EOL);
35 255
	}
36
37 255
	public function clearTillPrompt() {
38 255
		$this->write('');
39
		do {
40 255
			$promptLine = $this->readLine();
41 255
		} while (!$this->isPrompt($promptLine));
42 255
		$this->write('');
43 255
		$this->readLine();
44 255
	}
45
46
	/**
47
	 * get all unprocessed output from smbclient until the next prompt
48
	 *
49
	 * @param callable $callback (optional) callback to call for every line read
50
	 * @return string[]
51
	 * @throws AuthenticationException
52
	 * @throws ConnectException
53
	 * @throws ConnectionException
54
	 * @throws InvalidHostException
55
	 * @throws NoLoginServerException
56
	 */
57 254
	public function read(callable $callback = null) {
58 254
		if (!$this->isValid()) {
59 1
			throw new ConnectionException('Connection not valid');
60
		}
61 254
		$promptLine = $this->readLine(); //first line is prompt
62 254
		$this->parser->checkConnectionError($promptLine);
63
64 254
		$output = array();
65 254
		$line = $this->readLine();
66 254
		if ($line === false) {
67
			$this->unknownError($promptLine);
0 ignored issues
show
Security Bug introduced by
It seems like $promptLine defined by $this->readLine() on line 61 can also be of type false; however, Icewind\SMB\Wrapped\Connection::unknownError() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
68
		}
69 254
		while (!$this->isPrompt($line)) { //next prompt functions as delimiter
70 252
			if (is_callable($callback)) {
71 2
				$result = $callback($line);
72 2
				if ($result === false) { // allow the callback to close the connection for infinite running commands
73 2
					$this->close(true);
74 2
					break;
75
				}
76
			} else {
77 250
				$output[] .= $line;
78
			}
79 250
			$line = $this->readLine();
80 250
		}
81 254
		return $output;
82
	}
83
84
	/**
85
	 * Check
86
	 *
87
	 * @param $line
88
	 * @return bool
89
	 */
90 255
	private function isPrompt($line) {
91 255
		return mb_substr($line, 0, self::DELIMITER_LENGTH) === self::DELIMITER || $line === false;
92
	}
93
94
	/**
95
	 * @param string $promptLine (optional) prompt line that might contain some info about the error
96
	 * @throws ConnectException
97
	 */
98
	private function unknownError($promptLine = '') {
99
		if ($promptLine) { //maybe we have some error we missed on the previous line
100
			throw new ConnectException('Unknown error (' . $promptLine . ')');
101
		} else {
102
			$error = $this->readError(); // maybe something on stderr
103
			if ($error) {
104
				throw new ConnectException('Unknown error (' . $error . ')');
105
			} else {
106
				throw new ConnectException('Unknown error');
107
			}
108
		}
109
	}
110
111 255
	public function close($terminate = true) {
112 255
		if (is_resource($this->getInputStream())) {
113 255
			$this->write('close' . PHP_EOL);
114 255
		}
115 255
		parent::close($terminate);
116 255
	}
117
}
118