Passed
Push — master ( 4cca2c...a3e137 )
by Andreas
27:35 queued 11s
created

org_openpsa_contacts_mycontacts::save()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
     * @var midcom_db_person
18
     */
19
    private $person;
20
21
    /**
22
     * @var array
23
     */
24
    private $contacts;
25
26 4
    public function __construct()
27
    {
28 4
        $this->person = midcom::get()->auth->user->get_storage();
29 4
    }
30
31 4
    private function load()
32
    {
33 4
        if ($this->contacts === null) {
34 4
            $this->contacts = [];
35 4
            if ($saved = $this->person->get_parameter('org.openpsa.contacts', 'mycontacts')) {
36 1
                $this->contacts = unserialize($saved) ?: [];
37
            }
38
        }
39 4
    }
40
41 2
    private function save()
42
    {
43 2
        $this->person->set_parameter('org.openpsa.contacts', 'mycontacts', serialize($this->contacts));
44 2
    }
45
46 2
    public function add(string $guid)
47
    {
48 2
        $this->load();
49 2
        $this->contacts[] = $guid;
50 2
        $this->contacts = array_unique($this->contacts);
51 2
        $this->save();
52 2
    }
53
54 1
    public function remove(string $guid)
55
    {
56 1
        $this->load();
57 1
        $key = array_search($guid, $this->contacts);
58 1
        if ($key !== false) {
59 1
            unset($this->contacts[$key]);
60
        }
61 1
        $this->save();
62 1
    }
63
64 1
    public function is_member(string $guid) : bool
65
    {
66 1
        $this->load();
67 1
        return in_array($guid, $this->contacts);
68
    }
69
70
    /**
71
     * @return org_openpsa_contacts_person_dba[]
72
     */
73 1
    public function list_members() : array
74
    {
75 1
        $this->load();
76 1
        $qb = org_openpsa_contacts_person_dba::new_query_builder();
77 1
        $qb->add_constraint('guid', 'IN', $this->contacts);
78 1
        return $qb->execute();
79
    }
80
}
81