Completed
Push — master ( cfa673...036dbf )
by Robin
07:03
created

Connection::close()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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