Completed
Push — master ( 0bfb02...89f47c )
by Robin
02:44
created

Server   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 0
loc 67
ccs 30
cts 34
cp 0.8824
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 32 5
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\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\System;
17
18
class Server extends AbstractServer {
19
	/**
20
	 * Check if the smbclient php extension is available
21
	 *
22
	 * @param System $system
23
	 * @return bool
24
	 */
25
	public static function available(System $system) {
26
		return $system->getSmbclientPath();
27
	}
28
29 10
	private function getAuthFileArgument() {
30 10
		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...
31 10
			return '--authentication-file=' . 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('%s %s %s -L %s',
46 10
			$this->system->getSmbclientPath(),
47 10
			$this->getAuthFileArgument(),
48 10
			$this->getAuth()->getExtraCommandLineArguments(),
49 10
			escapeshellarg('//' . $this->getHost())
50 5
		);
51 10
		$connection = new RawConnection($command);
52 10
		$connection->writeAuthentication($this->getAuth()->getUsername(), $this->getAuth()->getPassword());
53 10
		$connection->connect();
54 10
		if (!$connection->isValid()) {
55
			throw new ConnectionException($connection->readLine());
56
		}
57 10
		$output = $connection->readAll();
58
		// sometimes we get an empty line first
59 10
		if (count($output) === 0) {
60 4
			$output = $connection->readAll();
61 2
		}
62 10
		$parser = new Parser($this->timezoneProvider);
63
64 10
		if (isset($output[0])) {
65 10
			$parser->checkConnectionError($output[0]);
66 2
		}
67
68 4
		$shareNames = $parser->parseListShares($output);
69
70 4
		$shares = array();
71 4
		foreach ($shareNames as $name => $description) {
72 4
			$shares[] = $this->getShare($name);
73 2
		}
74 4
		return $shares;
75
	}
76
77
	/**
78
	 * @param string $name
79
	 * @return IShare
80
	 */
81 512
	public function getShare($name) {
82 512
		return new Share($this, $name, $this->system);
83
	}
84
}
85