ServerFactory::createServer()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018 Robin Appelman <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace Icewind\SMB;
23
24
use Icewind\SMB\Exception\DependencyException;
25
use Icewind\SMB\Native\NativeServer;
26
use Icewind\SMB\Wrapped\Server;
27
28
class ServerFactory {
29
	const BACKENDS = [
30
		NativeServer::class,
31
		Server::class
32
	];
33
34
	/** @var ISystem */
35
	private $system;
36
37
	/** @var IOptions */
38
	private $options;
39
40
	/** @var ITimeZoneProvider */
41
	private $timeZoneProvider;
42
43
	/**
44
	 * ServerFactory constructor.
45
	 *
46
	 * @param IOptions|null $options
47
	 * @param ISystem|null $system
48
	 * @param ITimeZoneProvider|null $timeZoneProvider
49
	 */
50 6
	public function __construct(
51
		IOptions $options = null,
52
		ISystem $system = null,
53
		ITimeZoneProvider $timeZoneProvider = null
54
	) {
55 6
		if (is_null($options)) {
56 6
			$options = new Options();
57
		}
58 6
		if (is_null($system)) {
59 2
			$system = new System();
60
		}
61 6
		if (is_null($timeZoneProvider)) {
62 6
			$timeZoneProvider = new TimeZoneProvider($system);
63
		}
64 6
		$this->options = $options;
65 6
		$this->system = $system;
66 6
		$this->timeZoneProvider = $timeZoneProvider;
67 6
	}
68
69
70
	/**
71
	 * @param string $host
72
	 * @param IAuth $credentials
73
	 * @return IServer
74
	 * @throws DependencyException
75
	 */
76 6
	public function createServer(string $host, IAuth $credentials): IServer {
77 6
		foreach (self::BACKENDS as $backend) {
78 6
			if (call_user_func("$backend::available", $this->system)) {
79 4
				return new $backend($host, $credentials, $this->system, $this->timeZoneProvider, $this->options);
80
			}
81
		}
82
83 2
		throw new DependencyException('No valid backend available, ensure smbclient is in the path or php-smbclient is installed');
84
	}
85
}
86