Passed
Push — master ( 65297e...cf6d5f )
by Andreas
02:08
created

PermissionInfo::formatWorldPerms()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.9666
c 0
b 0
f 0
cc 4
nc 8
nop 0
crap 4
1
<?php
2
/**
3
 * Permission information part.
4
 *
5
 * @copyright 2018 Institute of Legal Medicine, Medical University of Innsbruck
6
 * @author Andreas Erhard <[email protected]>
7
 * @license LGPL-3.0-only
8
 * @link http://www.gerichtsmedizin.at/
9
 *
10
 * @package fileinfo
11
 */
12
namespace Gmi\Toolkit\Fileinfo\Part;
13
14
/**
15
 * Represents the permission information part of a FileInfo.
16
 */
17
class PermissionInfo implements InfoPartInterface
18
{
19
    /**
20
     * @var int
21
     */
22
    private $owner;
23
24
    /**
25
     * @var string
26
     */
27
    private $ownerName;
28
29
    /**
30
     * @var int
31
     */
32
    private $group;
33
34
    /**
35
     * @var string
36
     */
37
    private $groupName;
38
39
    /**
40
     * @var int
41
     */
42
    private $perms;
43
44
    /**
45
     * Constructor.
46
     *
47
     * @internal
48
     *
49
     * @param int    $owner
50
     * @param string $ownerName
51
     * @param int    $group
52
     * @param string $groupName
53
     * @param int    $perms
54
     */
55 16
    public function __construct($owner, $ownerName, $group, $groupName, $perms)
56
    {
57 16
        $this->owner = $owner;
58 16
        $this->ownerName = $ownerName;
59 16
        $this->group = $group;
60 16
        $this->groupName = $groupName;
61 16
        $this->perms = $perms;
62 16
    }
63
64
    /**
65
     * Returns the owner ID of the file.
66
     *
67
     * @return int
68
     */
69 4
    public function getOwner()
70
    {
71 4
        return $this->owner;
72
    }
73
74
    /**
75
     * Returns the owner name of the file.
76
     *
77
     * @return string
78
     */
79 4
    public function getOwnerName()
80
    {
81 4
        return $this->ownerName;
82
    }
83
84
    /**
85
     * Returns the group ID of the file.
86
     *
87
     * @return int
88
     */
89 4
    public function getGroup()
90
    {
91 4
        return $this->group;
92
    }
93
94
    /**
95
     * Returns the group name of the file.
96
     *
97
     * @return string
98
     */
99 4
    public function getGroupName()
100
    {
101 4
        return $this->groupName;
102
    }
103
104
    /**
105
     * Returns the permissions of the file.
106
     *
107
     * @return int
108
     */
109 5
    public function getPerms()
110
    {
111 5
        return $this->perms;
112
    }
113
114
    /**
115
     * Returns the formatted permissions of the file in octal format.
116
     *
117
     * @return string
118
     */
119 1
    public function getPermsOctal()
120
    {
121 1
        return sprintf('%o', $this->getPerms());
122
    }
123
124
    /**
125
     * Returns the formatted permissions of the file in rwx format (only permission bits).
126
     *
127
     * @return string
128
     */
129 9
    public function getPermsFormatted()
130
    {
131 9
        return $this->formatOwnerPerms() . $this->formatGroupPerms() . $this->formatWorldPerms();
132
    }
133
134
    /**
135
     * Returns whether the file has the read bit set for the owner.
136
     *
137
     * @return bool
138
     */
139 10
    public function isOwnerReadable()
140
    {
141 10
        return $this->matchPerms('400');
142
    }
143
144
    /**
145
     * Returns whether the file has the write bit set for the owner.
146
     *
147
     * @return bool
148
     */
149 10
    public function isOwnerWritable()
150
    {
151 10
        return $this->matchPerms('200');
152
    }
153
154
    /**
155
     * Returns whether the file has the execute bit set for the owner.
156
     *
157
     * @return bool
158
     */
159 10
    public function isOwnerExecutable()
160
    {
161 10
        return $this->matchPerms('100');
162
    }
163
164
    /**
165
     * Returns whether the file has the read bit set for the group.
166
     *
167
     * @return bool
168
     */
169 10
    public function isGroupReadable()
170
    {
171 10
        return $this->matchPerms('040');
172
    }
173
174
    /**
175
     * Returns whether the file has the write bit set for the group.
176
     *
177
     * @return bool
178
     */
179 10
    public function isGroupWritable()
180
    {
181 10
        return $this->matchPerms('020');
182
    }
183
184
    /**
185
     * Returns whether the file has the execute bit set for the group.
186
     *
187
     * @return bool
188
     */
189 10
    public function isGroupExecutable()
190
    {
191 10
        return $this->matchPerms('010');
192
    }
193
194
    /**
195
     * Returns whether the file has the read bit set for others/world.
196
     *
197
     * @return bool
198
     */
199 10
    public function isWorldReadable()
200
    {
201 10
        return $this->matchPerms('004');
202
    }
203
204
    /**
205
     * Returns whether the file has the write bit set for others/world.
206
     *
207
     * @return bool
208
     */
209 10
    public function isWorldWritable()
210
    {
211 10
        return $this->matchPerms('002');
212
    }
213
214
    /**
215
     * Returns whether the file has the execute bit set for others/world.
216
     *
217
     * @return bool
218
     */
219 10
    public function isWorldExecutable()
220
    {
221 10
        return $this->matchPerms('001');
222
    }
223
224
    /**
225
     * Matches an octal string against the file permissions.
226
     *
227
     * @param string $oct octal permission string
228
     *
229
     * @return bool
230
     */
231 10
    private function matchPerms($oct)
232
    {
233 10
        return ($this->perms & octdec($oct)) === octdec($oct);
234
    }
235
236
    /**
237
     * Returns the formatted owner permissions of the file in rwx format.
238
     *
239
     * @return string
240
     */
241 9
    private function formatOwnerPerms()
242
    {
243 9
        $permstr = '';
244 9
        $permstr .= $this->isOwnerReadable() ? 'r' : '-';
245 9
        $permstr .= $this->isOwnerWritable() ? 'w' : '-';
246 9
        $permstr .= $this->isOwnerExecutable() ? 'x' : '-';
247
248 9
        return $permstr;
249
    }
250
251
    /**
252
     * Returns the formatted group permissions of the file in rwx format.
253
     *
254
     * @return string
255
     */
256 9
    private function formatGroupPerms()
257
    {
258 9
        $permstr = '';
259 9
        $permstr .= $this->isGroupReadable() ? 'r' : '-';
260 9
        $permstr .= $this->isGroupWritable() ? 'w' : '-';
261 9
        $permstr .= $this->isGroupExecutable() ? 'x' : '-';
262
263 9
        return $permstr;
264
    }
265
266
    /**
267
     * Returns the formatted world permissions of the file in rwx format.
268
     *
269
     * @return string
270
     */
271 9
    private function formatWorldPerms()
272
    {
273 9
        $permstr = '';
274 9
        $permstr .= $this->isWorldReadable() ? 'r' : '-';
275 9
        $permstr .= $this->isWorldWritable() ? 'w' : '-';
276 9
        $permstr .= $this->isWorldExecutable() ? 'x' : '-';
277
278 9
        return $permstr;
279
    }
280
}
281