FileResource::save()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace WebComplete\rbac\resource;
4
5
use WebComplete\rbac\entity\PermissionInterface;
6
use WebComplete\rbac\entity\RoleInterface;
7
8
class FileResource extends AbstractResource
9
{
10
11
    /**
12
     * @var string
13
     */
14
    protected $file;
15
16
    /**
17
     * @param string $file
18
     */
19
    public function __construct(string $file)
20
    {
21
        $this->file = $file;
22
    }
23
24
    /**
25
     * @throws \WebComplete\rbac\exception\RbacException
26
     */
27
    public function load()
28
    {
29
        $this->clear();
30
31
        if (!file_exists($this->file) || (!$data = json_decode(file_get_contents($this->file), true))) {
32
            $data = [];
33
        }
34
35
        $this->restorePermissions($data['permissions'] ?? []);
36
        $this->restoreRoles($data['roles'] ?? []);
37
    }
38
39
    /**
40
     */
41
    public function save()
42
    {
43
        $data = [
44
            'roles' => [],
45
            'permissions' => [],
46
        ];
47
        foreach ($this->roles as $role) {
48
            $data['roles'][$role->getName()] = $this->roleToRow($role);
49
        }
50
        foreach ($this->permissions as $permission) {
51
            $data['permissions'][$permission->getName()] = $this->permissionToRow($permission);
52
        }
53
54
        file_put_contents($this->file, json_encode($data));
55
    }
56
57
    /**
58
     * @param RoleInterface $role
59
     *
60
     * @return array
61
     */
62
    protected function roleToRow(RoleInterface $role): array
63
    {
64
        $result = [];
65
        $result['name'] = $role->getName();
66
        $result['description'] = $role->getDescription();
67
        $childrenNames = [];
68
        foreach ($role->getChildren() as $child) {
69
            $childrenNames[] = $child->getName();
70
        }
71
        $result['children'] = $childrenNames;
72
        $permissionNames = [];
73
        foreach ($role->getPermissions() as $permission) {
74
            $permissionNames[] = $permission->getName();
75
        }
76
        $result['permissions'] = $permissionNames;
77
        return $result;
78
    }
79
80
    /**
81
     * @param PermissionInterface $permission
82
     *
83
     * @return array
84
     */
85
    protected function permissionToRow(PermissionInterface $permission): array
86
    {
87
        $result = [];
88
        $result['name'] = $permission->getName();
89
        $result['description'] = $permission->getDescription();
90
        $childrenNames = [];
91
        foreach ($permission->getChildren() as $child) {
92
            $childrenNames[] = $child->getName();
93
        }
94
        $result['children'] = $childrenNames;
95
        $result['ruleClass'] = $permission->getRuleClass();
96
        return $result;
97
    }
98
99
    /**
100
     * @param array[] $permissionsData
101
     *
102
     * @throws \WebComplete\rbac\exception\RbacException
103
     */
104
    protected function restorePermissions($permissionsData)
105
    {
106
        /** @var string[][] $permChildrenNames */
107
        $permChildrenNames = [];
108
109
        foreach ($permissionsData as $pData) {
110
            $permission = $this->createPermission($pData['name'] ?? '', $pData['description'] ?? '');
111
            $permission->setRuleClass($pData['ruleClass'] ?? '');
112
            $permChildrenNames[$permission->getName()] = $pData['children'] ?? [];
113
        }
114
115
        foreach ($permChildrenNames as $permissionName => $childrenNames) {
116
            foreach ($childrenNames as $childName) {
117
                $permission = $this->getPermission($permissionName);
118
                $child = $this->getPermission($childName);
119
                if ($permission && $child) {
120
                    $permission->addChild($child);
121
                }
122
            }
123
        }
124
    }
125
126
    /**
127
     * @param array[] $rolesData
128
     *
129
     * @throws \WebComplete\rbac\exception\RbacException
130
     */
131
    protected function restoreRoles($rolesData)
132
    {
133
        /** @var string[][] $rolesChildrenNames */
134
        $rolesChildrenNames = [];
135
136
        foreach ($rolesData as $rData) {
137
            $role = $this->createRole($rData['name'] ?? '', $rData['description'] ?? '');
138
            $rolesChildrenNames[$role->getName()] = $rData['children'] ?? [];
139
            $permissionNames = $rData['permissions'] ?? [];
140
            foreach ($permissionNames as $permissionName) {
141
                if ($permission = $this->getPermission($permissionName)) {
142
                    $role->addPermission($permission);
143
                }
144
            }
145
        }
146
147
        foreach ($rolesChildrenNames as $roleName => $childrenNames) {
148
            foreach ($childrenNames as $childName) {
149
                $role = $this->getRole($roleName);
150
                $child = $this->getRole($childName);
151
                if ($role && $child) {
152
                    $role->addChild($child);
153
                }
154
            }
155
        }
156
    }
157
}
158