Passed
Push — master ( 8481d6...507b18 )
by Robin
03:14
created

Server::getAuthFileArgument()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 5
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 2
b 0
f 1
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\AbstractServer;
11
use Icewind\SMB\Exception\AuthenticationException;
12
use Icewind\SMB\Exception\ConnectException;
13
use Icewind\SMB\Exception\ConnectionException;
14
use Icewind\SMB\Exception\InvalidHostException;
15
use Icewind\SMB\IShare;
16
use Icewind\SMB\ISystem;
17
18
class Server extends AbstractServer {
19
	/**
20
	 * Check if the smbclient php extension is available
21
	 *
22
	 * @param ISystem $system
23
	 * @return bool
24
	 */
25 4
	public static function available(ISystem $system) {
26 4
		return $system->getSmbclientPath();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $system->getSmbclientPath() also could return the type string which is incompatible with the documented return type boolean.
Loading history...
27
	}
28
29 10
	private function getAuthFileArgument() {
30 10
		if ($this->getAuth()->getUsername()) {
31 10
			return '--authentication-file=' . $this->system->getFD(3);
32
		} else {
33
			return '';
34
		}
35
	}
36
37
	/**
38
	 * @return IShare[]
39
	 *
40
	 * @throws AuthenticationException
41
	 * @throws InvalidHostException
42
	 * @throws ConnectException
43
	 */
44 10
	public function listShares() {
45 10
		$command = sprintf(
46 10
			'%s %s %s -L %s',
47 10
			$this->system->getSmbclientPath(),
48 10
			$this->getAuthFileArgument(),
49 10
			$this->getAuth()->getExtraCommandLineArguments(),
50 10
			escapeshellarg('//' . $this->getHost())
51
		);
52 10
		$connection = new RawConnection($command);
53 10
		$connection->writeAuthentication($this->getAuth()->getUsername(), $this->getAuth()->getPassword());
54 10
		$connection->connect();
55 10
		if (!$connection->isValid()) {
56
			throw new ConnectionException($connection->readLine());
57
		}
58
59 10
		$parser = new Parser($this->timezoneProvider);
0 ignored issues
show
Bug introduced by
$this->timezoneProvider of type Icewind\SMB\TimeZoneProvider is incompatible with the type string expected by parameter $timeZone of Icewind\SMB\Wrapped\Parser::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

59
		$parser = new Parser(/** @scrutinizer ignore-type */ $this->timezoneProvider);
Loading history...
60
61 10
		$output = $connection->readAll();
62 10
		if (isset($output[0])) {
63 6
			$parser->checkConnectionError($output[0]);
64
		}
65
66
		// sometimes we get an empty line first
67 4
		if (count($output) < 2) {
68 4
			$output = $connection->readAll();
69
		}
70
71 4
		if (isset($output[0])) {
72 4
			$parser->checkConnectionError($output[0]);
73
		}
74
75 4
		$shareNames = $parser->parseListShares($output);
76
77 4
		$shares = [];
78 4
		foreach ($shareNames as $name => $description) {
79 4
			$shares[] = $this->getShare($name);
80
		}
81 4
		return $shares;
82
	}
83
84
	/**
85
	 * @param string $name
86
	 * @return IShare
87
	 */
88 488
	public function getShare($name) {
89 488
		return new Share($this, $name, $this->system);
90
	}
91
}
92