Completed
Push — master ( de268f...fc9f0b )
by Robin
02:48
created

ServerFactory::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 14
cts 14
cp 1
rs 9.6666
c 0
b 0
f 0
cc 4
nc 8
nop 3
crap 4
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
25
use Icewind\SMB\Exception\DependencyException;
26
use Icewind\SMB\Native\NativeServer;
27
use Icewind\SMB\Wrapped\Server;
28
29
class ServerFactory {
30
	const BACKENDS = [
31
		NativeServer::class,
32
		Server::class
33
	];
34
35
	/** @var System */
36
	private $system;
37
38
	/** @var IOptions */
39
	private $options;
40
41
	/** @var ITimeZoneProvider */
42
	private $timeZoneProvider;
43
44
	/**
45
	 * ServerFactory constructor.
46
	 *
47
	 * @param IOptions|null $options
48
	 * @param ISystem|null $system
49
	 * @param ITimeZoneProvider|null $timeZoneProvider
50
	 */
51 7
	public function __construct(
52
		IOptions $options = null,
53
		ISystem $system = null,
54
		ITimeZoneProvider $timeZoneProvider = null
55
	) {
56 7
		if (is_null($options)) {
57 7
			$options = new Options();
58 1
		}
59 7
		if (is_null($system)) {
60 1
			$system = new System();
61 1
		}
62 7
		if (is_null($timeZoneProvider)) {
63 7
			$timeZoneProvider = new TimeZoneProvider($system);
64 1
		}
65 7
		$this->options = $options;
66 7
		$this->system = $system;
67 7
		$this->timeZoneProvider = $timeZoneProvider;
68 7
	}
69
70
71
	/**
72
	 * @param $host
73
	 * @param IAuth $credentials
74
	 * @return IServer
75
	 * @throws DependencyException
76
	 */
77 7
	public function createServer($host, IAuth $credentials) {
78 7
		foreach (self::BACKENDS as $backend) {
79 7
			if (call_user_func("$backend::available", $this->system)) {
80 7
				return new $backend($host, $credentials, $this->system, $this->timeZoneProvider, $this->options);
81
			}
82
		}
83
84 3
		throw new DependencyException('No valid backend available, ensure smbclient is in the path or php-smbclient is installed');
85
	}
86
}
87