Passed
Push — master ( f1fb72...be1df6 )
by Petr
08:10
created

Groups::noDirectoryDelimiterSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace kalanis\kw_auth_sources\Sources\Files;
4
5
6
use kalanis\kw_auth_sources\AuthSourcesException;
7
use kalanis\kw_auth_sources\Data\FileGroup;
8
use kalanis\kw_auth_sources\Interfaces;
9
use kalanis\kw_auth_sources\Sources;
10
use kalanis\kw_locks\Interfaces\ILock;
11
use kalanis\kw_locks\LockException;
12
13
14
/**
15
 * Class Groups
16
 * @package kalanis\kw_auth_sources\Sources\Files
17
 * Work with groups of users
18
 */
19
class Groups implements Interfaces\IWorkGroups
20
{
21
    use Sources\TAuthLock;
22
    use Sources\TLines;
23
    use Sources\TSeparated;
24
    use Sources\TStatusTransform;
25
26
    // default positions
27
    const GRP_ID = 0;
28
    const GRP_NAME = 1;
29
    const GRP_AUTHOR = 2;
30
    const GRP_DESC = 3;
31
    const GRP_STATUS = 4;
32
    const GRP_PARENTS = 5;
33
    const GRP_EXTRA = 6;
34
    const GRP_FEED = 7;
35
36
    /** @var Storages\AStorage */
37
    protected $storage = null;
38
    /** @var Interfaces\IWorkAccounts */
39
    protected $accounts = null;
40
    /** @var Interfaces\IExtraParser */
41
    protected $extraParser = null;
42
    /** @var string[] */
43
    protected $path = [];
44
45
    /**
46
     * @param Storages\AStorage $storage
47
     * @param Interfaces\IWorkAccounts $accounts
48
     * @param Interfaces\IExtraParser $parser
49
     * @param ILock $lock
50
     * @param string[] $path
51
     * @param Interfaces\IKAusTranslations|null $lang
52
     */
53 26
    public function __construct(
54
        Storages\AStorage $storage,
55
        Interfaces\IWorkAccounts $accounts,
56
        Interfaces\IExtraParser $parser,
57
        ILock $lock,
58
        array $path,
59
        ?Interfaces\IKAusTranslations $lang = null
60
    )
61
    {
62 26
        $this->setAusLang($lang);
63 26
        $this->initAuthLock($lock);
64 26
        $this->storage = $storage;
65 26
        $this->path = $path;
66 26
        $this->accounts = $accounts;
67 26
        $this->extraParser = $parser;
68 26
    }
69
70 3
    public function createGroup(Interfaces\IGroup $group): bool
71
    {
72 3
        $userId = $group->getGroupAuthorId();
73 3
        $groupName = $this->stripChars($group->getGroupName());
74 3
        $groupDesc = $this->stripChars($group->getGroupDesc());
75
76
        // not everything necessary is set
77 3
        if (empty($userId) || empty($groupName)) {
78 1
            throw new AuthSourcesException($this->getAusLang()->kauGroupMissParam());
79
        }
80 2
        $this->checkLock();
81
82 2
        $gid = 0;
83 2
        $this->getLock()->create();
84
85
        // read groups
86
        try {
87 2
            $groupLines = $this->openGroups();
88 1
        } catch (AuthSourcesException $ex) {
89
            // silence the problems on storage
90 1
            $groupLines = [];
91
        }
92 2
        foreach ($groupLines as &$line) {
93 1
            $gid = max($gid, $line[static::GRP_ID]);
94
        }
95 2
        $gid++;
96
97
        $newGroup = [
98 2
            static::GRP_ID => $gid,
99 2
            static::GRP_NAME => $groupName,
100 2
            static::GRP_AUTHOR => $userId,
101 2
            static::GRP_DESC => !empty($groupDesc) ? $groupDesc : $groupName,
102 2
            static::GRP_STATUS => $group->getGroupStatus(),
103 2
            static::GRP_PARENTS => $this->compactStr($group->getGroupParents()),
104 2
            static::GRP_EXTRA => $this->extraParser->compact($group->getGroupExtra()),
105 2
            static::GRP_FEED => '',
106
        ];
107 2
        ksort($newGroup);
108 2
        $groupLines[] = $newGroup;
109
110
        try {
111
            // now save it
112 2
            $result = $this->saveGroups($groupLines);
113 2
        } finally {
114 2
            $this->getLock()->delete();
115
        }
116 2
        return $result;
117
    }
118
119 2
    public function getGroupDataOnly(string $groupId): ?Interfaces\IGroup
120
    {
121 2
        $this->checkLock();
122
        try {
123 2
            $groupLines = $this->openGroups();
124 1
        } catch (AuthSourcesException $ex) {
125
            // silence the problems on storage
126 1
            return null;
127
        }
128 1
        foreach ($groupLines as &$line) {
129 1
            if ($line[static::GRP_ID] == $groupId) {
130 1
                return $this->getGroupClass($line);
131
            }
132
        }
133 1
        return null;
134
    }
135
136 1
    public function readGroup(): array
137
    {
138 1
        $this->checkLock();
139
140 1
        $groupLines = $this->openGroups();
141 1
        $result = [];
142 1
        foreach ($groupLines as &$line) {
143 1
            $result[] = $this->getGroupClass($line);
144
        }
145
146 1
        return $result;
147
    }
148
149
    /**
150
     * @param array<int, string> $line
151
     * @throws AuthSourcesException
152
     * @return Interfaces\IGroup
153
     */
154 2
    protected function getGroupClass(array &$line): Interfaces\IGroup
155
    {
156 2
        $group = new FileGroup();
157 2
        $group->setGroupData(
158 2
            strval($line[static::GRP_ID]),
159 2
            strval($line[static::GRP_NAME]),
160 2
            strval($line[static::GRP_DESC]),
161 2
            strval($line[static::GRP_AUTHOR]),
162 2
            intval($line[static::GRP_STATUS]),
163 2
            $this->separateStr($line[static::GRP_PARENTS]),
164 2
            $this->extraParser->expand($line[static::GRP_EXTRA])
165
        );
166 2
        return $group;
167
    }
168
169
    /**
170
     * @param Interfaces\IGroup $group
171
     * @return bool
172
     * @throws LockException
173
     * @throws AuthSourcesException
174
     */
175 1
    public function updateGroup(Interfaces\IGroup $group): bool
176
    {
177 1
        $groupName = $this->stripChars($group->getGroupName());
178 1
        $groupDesc = $this->stripChars($group->getGroupDesc());
179
180 1
        $this->checkLock();
181
182 1
        $this->getLock()->create();
183
        try {
184 1
            $groupLines = $this->openGroups();
185 1
        } finally {
186 1
            $this->getLock()->delete();
187
        }
188 1
        foreach ($groupLines as &$line) {
189 1
            if ($line[static::GRP_ID] == $group->getGroupId()) {
190
                // REFILL
191 1
                $line[static::GRP_NAME] = !empty($groupName) ? $groupName : $line[static::GRP_NAME] ;
192 1
                $line[static::GRP_DESC] = !empty($groupDesc) ? $groupDesc : $line[static::GRP_DESC] ;
193 1
                $line[static::GRP_STATUS] = $group->getGroupStatus();
194 1
                $line[static::GRP_PARENTS] = $this->compactStr($group->getGroupParents());
195 1
                $line[static::GRP_EXTRA] = $this->extraParser->compact($group->getGroupExtra());
196
            }
197
        }
198
199
        try {
200 1
            $result = $this->saveGroups($groupLines);
201 1
        } finally {
202 1
            $this->getLock()->delete();
203
        }
204 1
        return $result;
205
    }
206
207
    /**
208
     * @param string $groupId
209
     * @return bool
210
     * @throws LockException
211
     * @throws AuthSourcesException
212
     */
213 3
    public function deleteGroup(string $groupId): bool
214
    {
215 3
        $this->checkLock();
216 3
        $this->checkRest($groupId);
217
218 2
        $changed = false;
219 2
        $this->getLock()->create();
220
221
        // update groups
222
        try {
223 2
            $openGroups = $this->openGroups();
224 1
        } catch (AuthSourcesException $ex) {
225
            // silence the problems on storage
226 1
            $this->getLock()->delete();
227 1
            return false;
228
        }
229 1
        foreach ($openGroups as $index => &$line) {
230 1
            if ($line[static::GRP_ID] == $groupId) {
231 1
                unset($openGroups[$index]);
232 1
                $changed = true;
233
            }
234
        }
235
236 1
        $result = true;
237
        try {
238
            // now save it
239 1
            if ($changed) {
240 1
                $result = $this->saveGroups($openGroups);
241
            }
242 1
        } finally {
243 1
            $this->getLock()->delete();
244
        }
245 1
        return $changed && $result;
246
    }
247
248
    /**
249
     * @param string $groupId
250
     * @throws AuthSourcesException
251
     * @throws LockException
252
     */
253 3
    protected function checkRest(string $groupId): void
254
    {
255 3
        $passLines = $this->accounts->readAccounts();
256 3
        foreach ($passLines as &$line) {
257 3
            if ($line->getGroup() == $groupId) {
258 1
                throw new AuthSourcesException($this->getAusLang()->kauGroupHasMembers());
259
            }
260
        }
261 2
    }
262
263
    /**
264
     * @throws AuthSourcesException
265
     * @return array<int, array<int, string|int>>
266
     */
267 5
    protected function openGroups(): array
268
    {
269 5
        return $this->storage->read(array_merge($this->path, [Interfaces\IFile::GROUP_FILE]));
270
    }
271
272
    /**
273
     * @param array<int, array<int, string|int>> $lines
274
     * @throws AuthSourcesException
275
     * @return bool
276
     */
277 2
    protected function saveGroups(array $lines): bool
278
    {
279 2
        return $this->storage->write(array_merge($this->path, [Interfaces\IFile::GROUP_FILE]), $lines);
280
    }
281
282
    /**
283
     * @return string
284
     * @codeCoverageIgnore translation
285
     */
286
    protected function noDirectoryDelimiterSet(): string
287
    {
288
        return $this->getAusLang()->kauNoDelimiterSet();
289
    }
290
}
291