Passed
Push — master ( 658945...f0b652 )
by Petr
02:27
created

Groups::updateGroup()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 17
ccs 13
cts 13
cp 1
crap 3
rs 9.8666
1
<?php
2
3
namespace kalanis\kw_auth_sources\Sources\Memory;
4
5
6
use kalanis\kw_auth_sources\Interfaces;
7
8
9
/**
10
 * Class Groups
11
 * @package kalanis\kw_auth_sources\Sources\Memory
12
 * Work with groups of users - in memory
13
 */
14
class Groups implements Interfaces\IWorkGroups
15
{
16
    /** @var Interfaces\IGroup[] */
17
    protected $local = [];
18
19
    /**
20
     * @param Interfaces\IGroup[] $initial
21
     */
22 6
    public function __construct(array $initial = [])
23
    {
24 6
        $this->local = $initial;
25 6
    }
26
27 2
    public function createGroup(Interfaces\IGroup $group): bool
28
    {
29 2
        foreach ($this->local as $local) {
30 2
            if ($local->getGroupId() == $group->getGroupId()) {
31 1
                return false;
32
            }
33
        }
34
35 1
        $this->local[] = $group;
36 1
        return true;
37
    }
38
39 2
    public function getGroupDataOnly(string $groupId): ?Interfaces\IGroup
40
    {
41 2
        foreach ($this->local as $local) {
42 1
            if ($local->getGroupId() == $groupId) {
43 1
                return clone $local;
44
            }
45
        }
46 2
        return null;
47
    }
48
49 1
    public function readGroup(): array
50
    {
51 1
        return $this->local;
52
    }
53
54 2
    public function updateGroup(Interfaces\IGroup $group): bool
55
    {
56 2
        foreach ($this->local as $local) {
57 2
            if ($local->getGroupId() == $group->getGroupId()) {
58 1
                $local->setGroupData(
59 1
                    $local->getGroupId(),
60 1
                    $group->getGroupName(),
61 1
                    $group->getGroupDesc(),
62 1
                    $local->getGroupAuthorId(),
63 1
                    $group->getGroupStatus(),
64 1
                    $group->getGroupParents(),
65 1
                    $group->getGroupExtra()
66
                );
67 1
                return true;
68
            }
69
        }
70 1
        return false;
71
    }
72
73 2
    public function deleteGroup(string $groupId): bool
74
    {
75 2
        $willDelete = false;
76 2
        $use = [];
77 2
        foreach ($this->local as $local) {
78 2
            if ($local->getGroupId() == $groupId) {
79 1
                $willDelete = true;
80
            } else {
81 2
                $use[] = $local;
82
            }
83
        }
84 2
        $this->local = $use;
85 2
        return $willDelete;
86
    }
87
}
88