Passed
Push — master ( 30283f...45f254 )
by Daniel
01:31
created

InputOutputFilePermissions::firstFlagCodeArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2019 Daniel Popiniuc
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
namespace danielgp\io_operations;
28
29
trait InputOutputFilePermissions
30
{
31
32
    /**
33
     * Returns an array with meaningful content of permissions
34
     *
35
     * @param int $permissionNumber
36
     * @return array
37
     */
38
    public function explainPerms($permissionNumber)
39
    {
40
        $firstFlag            = $this->matchFirstFlagSingle($permissionNumber);
41
        $permissionsString    = substr(sprintf('%o', $permissionNumber), -4);
42
        $numericalPermissions = $this->numericalPermissionsArray();
43
        return [
44
            'Permissions' => $permissionNumber,
45
            'Code'        => $permissionsString,
46
            'First'       => $firstFlag,
47
            'Overall'     => implode('', [
48
                $firstFlag['code'],
49
                $numericalPermissions[substr($permissionsString, 1, 1)]['code'],
50
                $numericalPermissions[substr($permissionsString, 2, 1)]['code'],
51
                $numericalPermissions[substr($permissionsString, 3, 1)]['code'],
52
            ]),
53
            'First'       => $firstFlag,
54
            'Owner'       => $numericalPermissions[substr($permissionsString, 1, 1)],
55
            'Group'       => $numericalPermissions[substr($permissionsString, 2, 1)],
56
            'World/Other' => $numericalPermissions[substr($permissionsString, 3, 1)],
57
        ];
58
    }
59
60
    private function firstFlagCodeArray()
61
    {
62
        return [
63
            0x8000 => '-',
64
            0x6000 => 'b',
65
            0x2000 => 'c',
66
            0x4000 => 'd',
67
            0xA000 => 'l',
68
            0x1000 => 'p',
69
            0xC000 => 's',
70
        ];
71
    }
72
73
    private function firstFlagDescriptionArray($codeFlag)
74
    {
75
        $aCodes = [
76
            '-' => 'Regular',
77
            'b' => 'Block special',
78
            'c' => 'Character special',
79
            'd' => 'Directory',
80
            'l' => 'Symbolic Link',
81
            'p' => 'FIFO pipe',
82
            's' => 'Socket',
83
            'w' => 'Whiteout',
84
        ];
85
        return ['code' => $codeFlag, 'name' => $aCodes[$codeFlag]];
86
    }
87
88
    private function matchFirstFlagSingle($permissionNumber)
89
    {
90
        $aCodes      = $this->firstFlagCodeArray();
91
        $matchedCode = 'w';
92
        foreach ($aCodes as $key => $value) {
93
            if (($permissionNumber & $key) == $key) {
94
                $matchedCode = $value;
95
            }
96
        }
97
        return $this->firstFlagDescriptionArray($matchedCode);
98
    }
99
100
    private function numericalPermissionsArray()
101
    {
102
        return [
103
            0 => ['code' => '---', 'name' => 'none'],
104
            1 => ['code' => '--x', 'name' => 'execute only'],
105
            2 => ['code' => '-w-', 'name' => 'write only'],
106
            3 => ['code' => '-wx', 'name' => 'write and execute'],
107
            4 => ['code' => 'r--', 'name' => 'read only'],
108
            5 => ['code' => 'r-x', 'name' => 'read and execute'],
109
            6 => ['code' => 'rw-', 'name' => 'read and write'],
110
            7 => ['code' => 'rwx', 'name' => 'read, write and execute'],
111
        ];
112
    }
113
}
114