GroupsDatabase   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 10.26%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 36
c 1
b 0
f 0
dl 0
loc 88
ccs 4
cts 39
cp 0.1026
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getGroupDataOnly() 0 9 2
A deleteGroup() 0 11 2
A __construct() 0 4 1
A createGroup() 0 9 1
A updateGroup() 0 10 1
A readGroup() 0 4 1
1
<?php
2
3
namespace kalanis\kw_auth_sources\Sources\Mapper;
4
5
6
use kalanis\kw_accounts\Interfaces;
7
use kalanis\kw_auth_sources\Traits\TSeparated;
8
use kalanis\kw_mapper\MapperException;
9
use kalanis\kw_mapper\Search\Search;
10
11
12
/**
13
 * Class GroupsDatabase
14
 * @package kalanis\kw_auth_sources\Sources\Mapper
15
 * Authenticate via Database
16
 * need kw_mapper!
17
 * @codeCoverageIgnore because access external content
18
 */
19
class GroupsDatabase implements Interfaces\IProcessGroups
20
{
21
    use TSeparated;
22
23
    protected Database\GroupsRecord $groupsRecord;
24
    protected Database\UsersRecord $usersRecord;
25
26 1
    public function __construct()
27
    {
28 1
        $this->groupsRecord = new Database\GroupsRecord();
29 1
        $this->usersRecord = new Database\UsersRecord();
30 1
    }
31
32
    /**
33
     * @param Interfaces\IGroup $group
34
     * @throws MapperException
35
     * @return bool
36
     */
37
    public function createGroup(Interfaces\IGroup $group): bool
38
    {
39
        $record = clone $this->groupsRecord;
40
        $record->name = $group->getGroupName();
41
        $record->desc = $group->getGroupDesc();
42
        $record->authorId = $group->getGroupAuthorId();
43
        $record->parents = $this->compactStr($group->getGroupParents());
44
        $record->status = $group->getGroupStatus();
45
        return $record->save(true);
46
    }
47
48
    /**
49
     * @param string $groupId
50
     * @throws MapperException
51
     * @return Interfaces\IGroup|null
52
     */
53
    public function getGroupDataOnly(string $groupId): ?Interfaces\IGroup
54
    {
55
        $record = clone $this->groupsRecord;
56
        $record->id = $groupId;
57
        if (empty($record->count())) {
58
            return null;
59
        }
60
        $record->load();
61
        return $record;
62
    }
63
64
    /**
65
     * @throws MapperException
66
     * @return Database\GroupsRecord[]
67
     */
68
    public function readGroup(): array
69
    {
70
        $search = new Search(clone $this->groupsRecord);
71
        return $search->getResults();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $search->getResults() returns the type kalanis\kw_mapper\Records\ARecord[] which is incompatible with the return type mandated by kalanis\kw_accounts\Inte...cessGroups::readGroup() of kalanis\kw_accounts\Interfaces\IGroup[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
72
    }
73
74
    /**
75
     * @param Interfaces\IGroup $group
76
     * @throws MapperException
77
     * @return bool
78
     */
79
    public function updateGroup(Interfaces\IGroup $group): bool
80
    {
81
        $record = clone $this->groupsRecord;
82
        $record->id = $group->getGroupId();
83
        $record->load();
84
        $record->name = $group->getGroupName();
85
        $record->desc = $group->getGroupDesc();
86
        $record->parents = $this->compactStr($group->getGroupParents());
87
        $record->status = $group->getGroupStatus();
88
        return $record->save();
89
    }
90
91
    /**
92
     * @param string $groupId
93
     * @throws MapperException
94
     * @return bool
95
     */
96
    public function deleteGroup(string $groupId): bool
97
    {
98
        $users = clone $this->usersRecord;
99
        $users->groupId = $groupId;
100
        if (0 >= $users->count()) {
101
            // not empty group
102
            return false;
103
        }
104
        $record = clone $this->groupsRecord;
105
        $record->id = $groupId;
106
        return $record->delete();
107
    }
108
}
109