Passed
Push — master ( 11084f...ddfcc7 )
by Andreas
23:39
created

org_openpsa_contacts_interface::find_root_group()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4.016

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 19
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 31
ccs 18
cts 20
cp 0.9
crap 4.016
rs 9.6333
1
<?php
2
/**
3
 * @package org.openpsa.contacts
4
 * @author Nemein Oy http://www.nemein.com/
5
 * @copyright Nemein Oy http://www.nemein.com/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
use midcom\datamanager\datamanager;
10
11
/**
12
 * OpenPSA Contact registers/user manager
13
 *
14
 * @package org.openpsa.contacts
15
 */
16
class org_openpsa_contacts_interface extends midcom_baseclasses_components_interface
17
implements midcom_services_permalinks_resolver
18
{
19
    /**
20
     * Prepares the component's indexer client
21
     */
22
    public function _on_reindex($topic, midcom_helper_configuration $config, midcom_services_indexer $indexer)
23
    {
24
        $qb_organisations = org_openpsa_contacts_group_dba::new_query_builder();
25
        $organisation_dm = datamanager::from_schemadb($config->get('schemadb_group'));
0 ignored issues
show
Bug introduced by
It seems like $config->get('schemadb_group') can also be of type false; however, parameter $path of midcom\datamanager\datamanager::from_schemadb() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

25
        $organisation_dm = datamanager::from_schemadb(/** @scrutinizer ignore-type */ $config->get('schemadb_group'));
Loading history...
26
27
        $qb_persons = org_openpsa_contacts_person_dba::new_query_builder();
28
        $person_dm = datamanager::from_schemadb($config->get('schemadb_person'));
29
30
        $indexer = new org_openpsa_contacts_midcom_indexer($topic, $indexer);
31
        $indexer->add_query('organisations', $qb_organisations, $organisation_dm);
32
        $indexer->add_query('persons', $qb_persons, $person_dm);
33
34
        return $indexer;
35
    }
36
37
    /**
38
     * Locates the root group
39
     */
40 16
    public static function find_root_group() : midcom_db_group
41
    {
42 16
        static $root_group;
43
44
        //Check if we have already initialized
45 16
        if (!empty($root_group)) {
46 15
            return $root_group;
47
        }
48
49 1
        $qb = midcom_db_group::new_query_builder();
50 1
        $qb->add_constraint('owner', '=', 0);
51 1
        $qb->add_constraint('name', '=', '__org_openpsa_contacts');
52
53 1
        if ($results = $qb->execute()) {
54
            return $root_group = end($results);
55
        }
56
57 1
        debug_add("OpenPSA Contacts root group could not be found", MIDCOM_LOG_WARN);
58
59
        //Attempt to  auto-initialize the group.
60 1
        $root_group = new midcom_db_group();
61 1
        $root_group->owner = 0;
62 1
        $root_group->name = '__org_openpsa_contacts';
63 1
        $root_group->official = midcom::get()->i18n->get_string($root_group->name, 'org.openpsa.contacts');
64 1
        midcom::get()->auth->request_sudo('org.openpsa.contacts');
65 1
        $ret = $root_group->create();
66 1
        midcom::get()->auth->drop_sudo();
67 1
        if (!$ret) {
68
            throw new midcom_error("Could not auto-initialize the module, group creation failed: " . midcom_connection::get_error_string());
69
        }
70 1
        return $root_group;
71
    }
72
73
    public function resolve_object_link(midcom_db_topic $topic, midcom_core_dbaobject $object) : ?string
74
    {
75
        if (   $object instanceof org_openpsa_contacts_group_dba
76
            || $object instanceof midcom_db_group) {
77
            return "group/{$object->guid}/";
78
        }
79
        if (   $object instanceof org_openpsa_contacts_person_dba
80
            || $object instanceof midcom_db_person) {
81
            return "person/{$object->guid}/";
82
        }
83
        return null;
84
    }
85
}
86