Completed
Push — master ( fdaf09...72216c )
by Robin
04:18
created

NativeFileInfo::getMTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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\IFileInfo;
11
12
class NativeFileInfo implements IFileInfo {
13
	const MODE_FILE = 0100000;
14
15
	/**
16
	 * @var string
17
	 */
18
	protected $path;
19
20
	/**
21
	 * @var string
22
	 */
23
	protected $name;
24
25
	/**
26
	 * @var NativeShare
27
	 */
28
	protected $share;
29
30
	/**
31
	 * @var array|null
32
	 */
33
	protected $statCache = null;
34
35
	/** @var callable|null */
36
	protected $statCallback = null;
37
38
	/**
39
	 * @var int
40
	 */
41
	protected $modeCache;
42
43
	/**
44
	 * @param NativeShare $share
45
	 * @param string $path
46
	 * @param string $name
47
	 * @param array|callable $stat
48
	 */
49 208
	public function __construct($share, $path, $name, $stat) {
50 208
		$this->share = $share;
51 208
		$this->path = $path;
52 208
		$this->name = $name;
53
54 208
		if (is_array($stat)) {
55 46
			$this->statCache = $stat;
56 206
		} elseif (is_callable($stat)) {
57 204
			$this->statCallback = $stat;
58 102
		} else {
59
			throw new \InvalidArgumentException('$stat needs to be an array or callback');
60
		}
61 208
	}
62
63
	/**
64
	 * @return string
65
	 */
66 202
	public function getPath() {
67 202
		return $this->path;
68
	}
69
70
	/**
71
	 * @return string
72
	 */
73 98
	public function getName() {
74 98
		return $this->name;
75
	}
76
77
	/**
78
	 * @return array
79
	 */
80 204
	protected function stat() {
81 204
		if (is_null($this->statCache)) {
82 204
			$this->statCache = call_user_func($this->statCallback);
83 102
		}
84 204
		return $this->statCache;
85
	}
86
87
	/**
88
	 * @return int
89
	 */
90 58
	public function getSize() {
91 58
		$stat = $this->stat();
92 58
		return $stat['size'];
93
	}
94
95
	/**
96
	 * @return int
97
	 */
98 2
	public function getMTime() {
99 2
		$stat = $this->stat();
100 2
		return $stat['mtime'];
101
	}
102
103
	/**
104
	 * @return bool
105
	 */
106 202
	public function isDirectory() {
107 202
		$stat = $this->stat();
108 202
		return !($stat['mode'] & self::MODE_FILE);
109
	}
110
111
	/**
112
	 * @return int
113
	 */
114 28
	protected function getMode() {
115 28
		if (!$this->modeCache) {
116 28
			$attribute = $this->share->getAttribute($this->path, 'system.dos_attr.mode');
117
			// parse hex string
118 28
			$this->modeCache = (int)hexdec(substr($attribute, 2));
119 14
		}
120 28
		return $this->modeCache;
121
	}
122
123
	/**
124
	 * @return bool
125
	 */
126 18
	public function isReadOnly() {
127 18
		$mode = $this->getMode();
128 18
		return (bool)($mode & IFileInfo::MODE_READONLY);
129
	}
130
131
	/**
132
	 * @return bool
133
	 */
134 26
	public function isHidden() {
135 26
		$mode = $this->getMode();
136 26
		return (bool)($mode & IFileInfo::MODE_HIDDEN);
137
	}
138
139
	/**
140
	 * @return bool
141
	 */
142 16
	public function isSystem() {
143 16
		$mode = $this->getMode();
144 16
		return (bool)($mode & IFileInfo::MODE_SYSTEM);
145
	}
146
147
	/**
148
	 * @return bool
149
	 */
150 16
	public function isArchived() {
151 16
		$mode = $this->getMode();
152 16
		return (bool)($mode & IFileInfo::MODE_ARCHIVE);
153
	}
154
}
155