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

Server   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 82.76%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 58
ccs 24
cts 29
cp 0.8276
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A available() 0 3 1
A getAuthFileArgument() 0 7 2
B listShares() 0 25 3
A getShare() 0 3 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\System;
12
13
class Server extends AbstractServer {
14
	/**
15
	 * Check if the smbclient php extension is available
16
	 *
17
	 * @return bool
18
	 */
19
	public static function available(System $system) {
20
		return $system->getSmbclientPath();
21
	}
22
23 5
	private function getAuthFileArgument() {
24 5
		if ($this->getAuth()->getUsername()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->getAuth()->getUsername() of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
25 5
			return '--authentication-file=' . System::getFD(3);
26
		} else {
27
			return '';
28
		}
29
	}
30
31
	/**
32
	 * @return \Icewind\SMB\IShare[]
33
	 *
34
	 * @throws \Icewind\SMB\Exception\AuthenticationException
35
	 * @throws \Icewind\SMB\Exception\InvalidHostException
36
	 */
37 5
	public function listShares() {
38 5
		$command = sprintf('%s %s %s -L %s',
39 5
			$this->system->getSmbclientPath(),
40 5
			$this->getAuthFileArgument(),
41 5
			$this->getAuth()->getExtraCommandLineArguments(),
42 5
			escapeshellarg('//' . $this->getHost())
43 5
		);
44 5
		$connection = new RawConnection($command);
45 5
		$connection->writeAuthentication($this->getAuth()->getUsername(), $this->getAuth()->getPassword());
46 5
		$connection->connect();
47 5
		$output = $connection->readAll();
48 5
		$parser = new Parser($this->timezoneProvider);
49
50 5
		if (isset($output[0])) {
51 3
			$parser->checkConnectionError($output[0]);
52
		}
53
54 2
		$shareNames = $parser->parseListShares($output);
55
56 2
		$shares = array();
57 2
		foreach ($shareNames as $name => $description) {
58
			$shares[] = $this->getShare($name);
59 2
		}
60 2
		return $shares;
61
	}
62
63
	/**
64
	 * @param string $name
65
	 * @return \Icewind\SMB\IShare
66
	 */
67 255
	public function getShare($name) {
68 255
		return new Share($this, $name, $this->system);
69
	}
70
}
71