Completed
Push — stable3.0 ( 04db6f...bd43cd )
by Robin
03:57
created

NativeFileInfo::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
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;
34
35
	/**
36
	 * @var int
37
	 */
38
	protected $modeCache;
39
40
	/**
41
	 * @param NativeShare $share
42
	 * @param string $path
43
	 * @param string $name
44
	 * @param array $stat
45
	 */
46
	public function __construct($share, $path, $name, $stat = null) {
47
		$this->share = $share;
48
		$this->path = $path;
49
		$this->name = $name;
50
		$this->statCache = $stat;
51
	}
52
53
	/**
54
	 * @return string
55
	 */
56
	public function getPath() {
57
		return $this->path;
58
	}
59
60
	/**
61
	 * @return string
62
	 */
63
	public function getName() {
64
		return $this->name;
65
	}
66
67
	/**
68
	 * @return array
69
	 */
70
	protected function stat() {
71
		if (is_null($this->statCache)) {
72
			$this->statCache = $this->share->getStat($this->getPath());
73
		}
74
		return $this->statCache;
75
	}
76
77
	/**
78
	 * @return int
79
	 */
80
	public function getSize() {
81
		$stat = $this->stat();
82
		return $stat['size'];
83
	}
84
85
	/**
86
	 * @return int
87
	 */
88
	public function getMTime() {
89
		$stat = $this->stat();
90
		return $stat['mtime'];
91
	}
92
93
	/**
94
	 * @return bool
95
	 */
96
	public function isDirectory() {
97
		$stat = $this->stat();
98
		return !($stat['mode'] & self::MODE_FILE);
99
	}
100
101
	/**
102
	 * @return int
103
	 */
104
	protected function getMode() {
105
		if (!$this->modeCache) {
106
			$attribute = $this->share->getAttribute($this->path, 'system.dos_attr.mode');
107
			// parse hex string
108
			$this->modeCache = (int)hexdec(substr($attribute, 2));
109
		}
110
		return $this->modeCache;
111
	}
112
113
	/**
114
	 * @return bool
115
	 */
116
	public function isReadOnly() {
117
		$mode = $this->getMode();
118
		return (bool)($mode & IFileInfo::MODE_READONLY);
119
	}
120
121
	/**
122
	 * @return bool
123
	 */
124
	public function isHidden() {
125
		$mode = $this->getMode();
126
		return (bool)($mode & IFileInfo::MODE_HIDDEN);
127
	}
128
129
	/**
130
	 * @return bool
131
	 */
132
	public function isSystem() {
133
		$mode = $this->getMode();
134
		return (bool)($mode & IFileInfo::MODE_SYSTEM);
135
	}
136
137
	/**
138
	 * @return bool
139
	 */
140
	public function isArchived() {
141
		$mode = $this->getMode();
142
		return (bool)($mode & IFileInfo::MODE_ARCHIVE);
143
	}
144
}
145