NativeServer::available()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 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\Native;
9
10
use Icewind\SMB\AbstractServer;
11
use Icewind\SMB\Exception\AuthenticationException;
12
use Icewind\SMB\Exception\InvalidHostException;
13
use Icewind\SMB\IAuth;
14
use Icewind\SMB\IOptions;
15
use Icewind\SMB\IShare;
16
use Icewind\SMB\ISystem;
17
use Icewind\SMB\ITimeZoneProvider;
18
19
class NativeServer extends AbstractServer {
20
	/**
21
	 * @var NativeState
22 496
	 */
23 496
	protected $state;
24 496
25 496
	public function __construct(string $host, IAuth $auth, ISystem $system, ITimeZoneProvider $timeZoneProvider, IOptions $options) {
26
		parent::__construct($host, $auth, $system, $timeZoneProvider, $options);
27 2
		$this->state = new NativeState();
28 2
	}
29 2
30
	protected function connect(): void {
31
		$this->state->init($this->getAuth(), $this->getOptions());
32
	}
33
34
	/**
35
	 * @return IShare[]
36 2
	 * @throws AuthenticationException
37 2
	 * @throws InvalidHostException
38 2
	 */
39 2
	public function listShares(): array {
40 2
		$this->connect();
41 2
		$shares = [];
42 2
		$dh = $this->state->opendir('smb://' . $this->getHost());
43
		while ($share = $this->state->readdir($dh, '')) {
44
			if ($share['type'] === 'file share') {
45 2
				$shares[] = $this->getShare($share['name']);
46 2
			}
47
		}
48
		$this->state->closedir($dh, '');
49
		return $shares;
50
	}
51
52
	public function getShare(string $name): IShare {
53 494
		return new NativeShare($this, $name);
54 494
	}
55
56
	/**
57
	 * Check if the smbclient php extension is available
58
	 *
59
	 * @param ISystem $system
60
	 * @return bool
61
	 */
62
	public static function available(ISystem $system): bool {
63 6
		return $system->libSmbclientAvailable();
64 6
	}
65
}
66