Passed
Push — master ( 5da13c...489a75 )
by Andreas
23:20
created

org_openpsa_contacts_mycontacts::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package org.openpsa.contacts
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * Helper class for managing "My contacts" lists
11
 *
12
 * @package org.openpsa.contacts
13
 */
14
class org_openpsa_contacts_mycontacts
15
{
16
    /**
17
     * The user whose list we're working on
18
     *
19
     * @var midcom_core_user
20
     */
21
    private $_user;
22
23
    /**
24
     * The list we're working on
25
     *
26
     * @var org_openpsa_contacts_list_dba
27
     */
28
    private $_group;
29
30 4
    public function __construct(midcom_core_user $user = null)
31
    {
32 4
        $this->_user = $user ?: midcom::get()->auth->user;
33 4
    }
34
35 4
    private function _get_group(bool $autocreate = false) : ?org_openpsa_contacts_list_dba
36
    {
37 4
        if (!$this->_group) {
38 4
            $qb = org_openpsa_contacts_list_dba::new_query_builder();
39 4
            $qb->add_constraint('person', '=', $this->_user->guid);
40 4
            $results = $qb->execute();
41 4
            if (!empty($results)) {
42 1
                $this->_group = $results[0];
43 4
            } elseif ($autocreate) {
44 2
                $this->_group = new org_openpsa_contacts_list_dba;
45 2
                $this->_group->person = $this->_user->guid;
46 2
                midcom::get()->auth->request_sudo('org.openpsa.contacts');
47 2
                $this->_group->create();
48 2
                $this->_group->set_privilege('midgard:owner', $this->_user->id, MIDCOM_PRIVILEGE_ALLOW);
49 2
                midcom::get()->auth->drop_sudo();
50
            }
51
        }
52
53 4
        return $this->_group;
54
    }
55
56 2
    public function add($guid)
57
    {
58 2
        $group = $this->_get_group(true);
59 2
        $group->add_member($guid);
60 2
    }
61
62 1
    public function remove($guid)
63
    {
64 1
        if ($group = $this->_get_group()) {
65 1
            $group->remove_member($guid);
66
        }
67 1
    }
68
69 1
    public function is_member($guid) : bool
70
    {
71 1
        if ($group = $this->_get_group()) {
72
            return $group->is_member($guid);
73
        }
74 1
        return false;
75
    }
76
77
    /**
78
     * @return org_openpsa_contacts_person_dba[]
79
     */
80 1
    public function list_members() : array
81
    {
82 1
        if ($group = $this->_get_group()) {
83
            $memberships = $group->list_members();
84
            if (!empty($memberships)) {
85
                $qb = org_openpsa_contacts_person_dba::new_query_builder();
86
                $qb->add_constraint('id', 'IN', $memberships);
87
                return $qb->execute();
88
            }
89
        }
90 1
        return [];
91
    }
92
}
93