Passed
Push — master ( 742be8...9fb175 )
by Robin
04:11
created

NativeFileInfo::getMTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
			$acls[$user] = new ACL($type, $flags, /** @scrutinizer ignore-type */ $mask);
Loading history...
Bug introduced by
$flags of type string is incompatible with the type integer expected by parameter $flags of Icewind\SMB\ACL::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
			$acls[$user] = new ACL($type, /** @scrutinizer ignore-type */ $flags, $mask);
Loading history...
Bug introduced by
$type of type string is incompatible with the type integer expected by parameter $type of Icewind\SMB\ACL::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

168
			$acls[$user] = new ACL(/** @scrutinizer ignore-type */ $type, $flags, $mask);
Loading history...
169
		}
170
171
		return $acls;
172
	}
173
}
174