PermissionsChecker::check()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace App\Http\Helpers\Installer;
4
5
class PermissionsChecker
6
{
7
    /**
8
     * @var array
9
     */
10
    protected $results = [];
11
12
    /**
13
     * Set the result array permissions and errors.
14
     *
15
     * @return mixed
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
16
     */
17
    public function __construct()
18
    {
19
        $this->results['permissions'] = [];
20
21
        $this->results['errors'] = null;
22
    }
23
24
    /**
25
     * Check for the folders permissions.
26
     *
27
     * @param array $folders
28
     * @return array
29
     */
30
    public function check(array $folders)
31
    {
32
        foreach($folders as $folder => $permission)
33
        {
34
            if(!($this->getPermission($folder) >= $permission))
35
            {
36
                $this->addFileAndSetErrors($folder, $permission, false);
37
            }
38
            else {
39
                $this->addFile($folder, $permission, true);
40
            }
41
        }
42
43
        return $this->results;
44
    }
45
46
    /**
47
     * Get a folder permission.
48
     *
49
     * @param $folder
50
     * @return string
51
     */
52
    private function getPermission($folder)
53
    {
54
        return substr(sprintf('%o', fileperms(base_path($folder))), -4);
55
    }
56
57
    /**
58
     * Add the file to the list of results.
59
     *
60
     * @param $folder
61
     * @param $permission
62
     * @param $isSet
63
     */
64
    private function addFile($folder, $permission, $isSet)
65
    {
66
        array_push($this->results['permissions'], [
67
            'folder' => $folder,
68
            'permission' => $permission,
69
            'isSet' => $isSet
70
        ]);
71
    }
72
73
    /**
74
     * Add the file and set the errors.
75
     *
76
     * @param $folder
77
     * @param $permission
78
     * @param $isSet
79
     */
80
    private function addFileAndSetErrors($folder, $permission, $isSet)
81
    {
82
        $this->addFile($folder, $permission, $isSet);
83
84
        $this->results['errors'] = true;
85
    }
86
}
87