Completed
Push — master ( 65afed...db16d4 )
by Robin
03:00
created

NativeFileInfo::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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
	public function __construct($share, $path, $name, $stat) {
50
		$this->share = $share;
51
		$this->path = $path;
52
		$this->name = $name;
53
54
		if (is_array($stat)) {
55
			$this->statCache = $stat;
56
		} elseif (is_callable($stat)) {
57
			$this->statCallback = $stat;
58
		} else {
59
			throw new \InvalidArgumentException('$stat needs to be an array or callback');
60
		}
61
	}
62
63
	/**
64
	 * @return string
65
	 */
66
	public function getPath() {
67
		return $this->path;
68
	}
69
70
	/**
71
	 * @return string
72
	 */
73
	public function getName() {
74
		return $this->name;
75
	}
76
77
	/**
78
	 * @return array
79
	 */
80
	protected function stat() {
81
		if (is_null($this->statCache)) {
82
			$this->statCache = call_user_func($this->statCallback);
83
		}
84
		return $this->statCache;
85
	}
86
87
	/**
88
	 * @return int
89
	 */
90
	public function getSize() {
91
		$stat = $this->stat();
92
		return $stat['size'];
93
	}
94
95
	/**
96
	 * @return int
97
	 */
98
	public function getMTime() {
99
		$stat = $this->stat();
100
		return $stat['mtime'];
101
	}
102
103
	/**
104
	 * @return bool
105
	 */
106
	public function isDirectory() {
107
		$stat = $this->stat();
108
		return !($stat['mode'] & self::MODE_FILE);
109
	}
110
111
	/**
112
	 * @return int
113
	 */
114
	protected function getMode() {
115
		if (!$this->modeCache) {
116
			$attribute = $this->share->getAttribute($this->path, 'system.dos_attr.mode');
117
			// parse hex string
118
			$this->modeCache = (int)hexdec(substr($attribute, 2));
119
		}
120
		return $this->modeCache;
121
	}
122
123
	/**
124
	 * @return bool
125
	 */
126
	public function isReadOnly() {
127
		$mode = $this->getMode();
128
		return (bool)($mode & IFileInfo::MODE_READONLY);
129
	}
130
131
	/**
132
	 * @return bool
133
	 */
134
	public function isHidden() {
135
		$mode = $this->getMode();
136
		return (bool)($mode & IFileInfo::MODE_HIDDEN);
137
	}
138
139
	/**
140
	 * @return bool
141
	 */
142
	public function isSystem() {
143
		$mode = $this->getMode();
144
		return (bool)($mode & IFileInfo::MODE_SYSTEM);
145
	}
146
147
	/**
148
	 * @return bool
149
	 */
150
	public function isArchived() {
151
		$mode = $this->getMode();
152
		return (bool)($mode & IFileInfo::MODE_ARCHIVE);
153
	}
154
}
155