Completed
Push — master ( 228057...4623d7 )
by Robin
05:13
created

Server   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 59
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 26 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
	private function getAuthFileArgument() {
24
		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
			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
	public function listShares() {
38
		$command = sprintf('%s %s %s -L %s',
39
			$this->system->getSmbclientPath(),
40
			$this->getAuthFileArgument(),
41
			$this->getAuth()->getExtraCommandLineArguments(),
42
			escapeshellarg('//' . $this->getHost())
43
		);
44
		var_dump($command);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($command); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
45
		$connection = new RawConnection($command);
46
		$connection->writeAuthentication($this->getAuth()->getUsername(), $this->getAuth()->getPassword());
47
		$connection->connect();
48
		$output = $connection->readAll();
49
		$parser = new Parser($this->timezoneProvider);
50
51
		if (isset($output[0])) {
52
			$parser->checkConnectionError($output[0]);
53
		}
54
55
		$shareNames = $parser->parseListShares($output);
56
57
		$shares = array();
58
		foreach ($shareNames as $name => $description) {
59
			$shares[] = $this->getShare($name);
60
		}
61
		return $shares;
62
	}
63
64
	/**
65
	 * @param string $name
66
	 * @return \Icewind\SMB\IShare
67
	 */
68
	public function getShare($name) {
69
		return new Share($this, $name, $this->system);
70
	}
71
}
72