PermissionsChecker   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 77
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A check() 0 11 3
A getPermission() 0 3 1
A __construct() 0 5 1
A addFile() 0 6 1
A addFileAndSetErrors() 0 5 1
1
<?php
2
3
namespace Turahe\LaravelInstaller\Helpers;
4
5
/**
6
 * Class PermissionsChecker.
7
 */
8
class PermissionsChecker
9
{
10
    /**
11
     * @var array
12
     */
13
    protected array $results = [];
14
15
    /**
16
     * Set the result array permissions and errors.
17
     *
18
     * @return mixed
19
     */
20
    public function __construct()
21
    {
22
        $this->results['permissions'] = [];
23
24
        $this->results['errors'] = null;
25
    }
26
27
    /**
28
     * Check for the folders permissions.
29
     *
30
     * @param array $folders
31
     * @return array
32
     */
33
    public function check(array $folders): array
34
    {
35
        foreach ($folders as $folder => $permission) {
36
            if (! ($this->getPermission($folder) >= $permission)) {
37
                $this->addFileAndSetErrors($folder, $permission, false);
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): string
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