Completed
Push — stable2 ( 3cf60d...464459 )
by Robin
08:59
created

Connection   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 79.63%

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 4
dl 0
loc 110
ccs 43
cts 54
cp 0.7963
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 9 2
B read() 0 30 7
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;
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 1024
	public function __construct($command, Parser $parser, $env = array()) {
24 1024
		parent::__construct($command, $env);
25 1024
		$this->parser = $parser;
26 1024
	}
27
28
	/**
29
	 * send input to smbclient
30
	 *
31
	 * @param string $input
32
	 */
33 1024
	public function write($input) {
34 1024
		parent::write($input . PHP_EOL);
35 1024
	}
36
37
	/**
38
	 * @throws ConnectException
39
	 */
40 1024
	public function clearTillPrompt() {
41 1024
		$this->write('');
42
		do {
43 1024
			$promptLine = $this->readLine();
44 1024
			$this->parser->checkConnectionError($promptLine);
45 1024
		} while (!$this->isPrompt($promptLine));
46 1024
		$this->write('');
47 1024
		$this->readLine();
48 1024
	}
49
50
	/**
51
	 * get all unprocessed output from smbclient until the next prompt
52
	 *
53
	 * @param callable $callback (optional) callback to call for every line read
54
	 * @return string[]
55
	 * @throws AuthenticationException
56
	 * @throws ConnectException
57
	 * @throws ConnectionException
58
	 * @throws InvalidHostException
59
	 * @throws NoLoginServerException
60
	 */
61 1020
	public function read(callable $callback = null) {
62 1020
		if (!$this->isValid()) {
63
			throw new ConnectionException('Connection not valid');
64
		}
65 1020
		$promptLine = $this->readLine(); //first line is prompt
66 1020
		$this->parser->checkConnectionError($promptLine);
67
68 1020
		$output = array();
69 1020
		if (!$this->isPrompt($promptLine)) {
70 4
			$line = $promptLine;
71 2
		} else {
72 1020
			$line = $this->readLine();
73
		}
74 1020
		if ($line === false) {
75
			$this->unknownError($promptLine);
0 ignored issues
show
Security Bug introduced by
It seems like $promptLine defined by $this->readLine() on line 65 can also be of type false; however, Icewind\SMB\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...
76
		}
77 1020
		while (!$this->isPrompt($line)) { //next prompt functions as delimiter
78 1012
			if (is_callable($callback)) {
79 8
				$result = $callback($line);
80 8
				if ($result === false) { // allow the callback to close the connection for infinite running commands
81 8
					$this->close(true);
82 8
					break;
83
				}
84
			} else {
85 1004
				$output[] .= $line;
86
			}
87 1004
			$line = $this->readLine();
88 502
		}
89 1020
		return $output;
90
	}
91
92
	/**
93
	 * Check
94
	 *
95
	 * @param $line
96
	 * @return bool
97
	 */
98 1024
	private function isPrompt($line) {
99 1024
		return mb_substr($line, 0, self::DELIMITER_LENGTH) === self::DELIMITER || $line === false;
100
	}
101
102
	/**
103
	 * @param string $promptLine (optional) prompt line that might contain some info about the error
104
	 * @throws ConnectException
105
	 */
106
	private function unknownError($promptLine = '') {
107
		if ($promptLine) { //maybe we have some error we missed on the previous line
108
			throw new ConnectException('Unknown error (' . $promptLine . ')');
109
		} else {
110
			$error = $this->readError(); // maybe something on stderr
111
			if ($error) {
112
				throw new ConnectException('Unknown error (' . $error . ')');
113
			} else {
114
				throw new ConnectException('Unknown error');
115
			}
116
		}
117
	}
118
119 1024
	public function close($terminate = true) {
120 1024
		if (is_resource($this->getInputStream())) {
121 1024
			$this->write('close' . PHP_EOL);
122 512
		}
123 1024
		parent::close($terminate);
124 1024
	}
125
}
126