Passed
Push — master ( 80a4ed...26ec76 )
by Robin
73:34 queued 71:57
created

Server::getShare()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
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\ConnectionRefusedException;
15
use Icewind\SMB\Exception\InvalidHostException;
16
use Icewind\SMB\IShare;
17
use Icewind\SMB\ISystem;
18
19
class Server extends AbstractServer {
20
	/**
21
	 * Check if the smbclient php extension is available
22
	 *
23
	 * @param ISystem $system
24
	 * @return bool
25
	 */
26 4
	public static function available(ISystem $system) {
27 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...
28
	}
29
30 10
	private function getAuthFileArgument() {
31 10
		if ($this->getAuth()->getUsername()) {
32 10
			return '--authentication-file=' . $this->system->getFD(3);
33
		} else {
34
			return '';
35
		}
36
	}
37
38
	/**
39
	 * @return IShare[]
40
	 *
41
	 * @throws AuthenticationException
42
	 * @throws InvalidHostException
43
	 * @throws ConnectException
44
	 */
45 10
	public function listShares() {
46 10
		$command = sprintf(
47 10
			'%s %s %s -L %s',
48 10
			$this->system->getSmbclientPath(),
49 10
			$this->getAuthFileArgument(),
50 10
			$this->getAuth()->getExtraCommandLineArguments(),
51 10
			escapeshellarg('//' . $this->getHost())
52
		);
53 10
		$connection = new RawConnection($command);
54 10
		$connection->writeAuthentication($this->getAuth()->getUsername(), $this->getAuth()->getPassword());
55 10
		$connection->connect();
56 10
		if (!$connection->isValid()) {
57
			throw new ConnectionException($connection->readLine());
58
		}
59
60 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

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