Completed
Push — master ( bebed6...b6e66d )
by Robin
02:58
created

TimeZoneProvider::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Copyright (c) 2015 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;
9
10
class TimeZoneProvider implements ITimeZoneProvider {
11
	/**
12
	 * @var string[]
13
	 */
14
	private $timeZones = [];
15
16
	/**
17
	 * @var ISystem
18
	 */
19
	private $system;
20
21
	/**
22
	 * @param ISystem $system
23
	 */
24 1044
	public function __construct(ISystem $system) {
25 1044
		$this->system = $system;
26 1044
	}
27
28 1024
	public function get($host) {
29 1024
		if (!isset($this->timeZones[$host])) {
30 1024
			$net = $this->system->getNetPath();
31
			// for local domain names we can assume same timezone
32 1024
			if ($net && $host && strpos($host, '.') !== false) {
33
				$command = sprintf(
34
					'%s time zone -S %s',
35
					$net,
36
					escapeshellarg($host)
37
				);
38
				$timeZone = exec($command);
39
				if (!$timeZone) {
40
					$timeZone = date_default_timezone_get();
41
				}
42
				$this->timeZones[$host] = $timeZone;
43
			} else { // fallback to server timezone
44 1024
				$this->timeZones[$host] = date_default_timezone_get();
45
			}
46 256
		}
47 1024
		return $this->timeZones[$host];
48
	}
49
}
50