Passed
Push — master ( 9dc093...de7a06 )
by Andreas
24:11
created

org_openpsa_relatedto_suspect::add_links()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 4
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * @package org.openpsa.relatedto
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
/**
10
 * Class for finding suspected "related to" links
11
 *
12
 * @package org.openpsa.relatedto
13
 */
14
class org_openpsa_relatedto_suspect extends midcom_baseclasses_components_purecode
15
{
16
    /**
17
     * Query all specific component for objects related to given object
18
     *
19
     * If optional $defaults (org_openpsa_relatedto_dba object) is given
20
     * it's used to fill default values for link objects returned.
21
     *
22
     * NOTE: Only returns new suspected relations, confirmed/notrelated links are filtered out
23
     *
24
     * returns array with the target object and prefilled link object
25
     *
26
     * The see org.openpsa.projects for an example of how the component interface method
27
     * org_openpsa_relatedto_find_suspects() should work.
28
     */
29 9
    public static function find_links_object_component(midcom_core_dbaobject $object, string $component, org_openpsa_relatedto_dba $defaults) : array
30
    {
31 9
        $ret = [];
32
33
        //Make sure we can access the component
34 9
        if (!midcom::get()->componentloader->is_installed($component)) {
35
            debug_add("Component {$component} is not installed", MIDCOM_LOG_ERROR);
36
            return $ret;
37
        }
38
39 9
        $interface = midcom::get()->componentloader->get_interface_class($component);
40
41 9
        if (!method_exists($interface, 'org_openpsa_relatedto_find_suspects')) {
42
            //Component does not wish to tell us anything
43
            debug_add("component {$component} does not support querying for suspects", MIDCOM_LOG_INFO);
44
            return $ret;
45
        }
46
        //Get components suspected links
47 9
        $interface->org_openpsa_relatedto_find_suspects($object, $defaults, $ret);
48
49
        //Filter out existing links
50 9
        foreach ($ret as $k => $linkdata) {
51
            if ($guid = $linkdata['link']->check_db(false)) {
52
                //Essentially same link already exists in db, remove from returned values
53
                debug_print_r("found matching link with {$guid} (skipping), our data:", $linkdata['link']);
54
                unset($ret[$k]);
55
            }
56
        }
57
58 9
        return $ret;
59
    }
60
61
    public static function add_links(midcom_core_querybuilder $qb, string $component, org_openpsa_relatedto_dba $defaults, array &$links_array)
62
    {
63
        foreach ($qb->execute() as $object) {
64
            $links_array[] = [
65
                'other_obj' => $object,
66
                'link' => self::defaults_helper($defaults, $component, $object)
67
            ];
68
        }
69
    }
70
71
    /**
72
     * Fill properties of given $link object from given link object with defaults
73
     *
74
     * Tries to be smart about the direction (inbound vs outbound) properties
75
     */
76
    private static function defaults_helper(org_openpsa_relatedto_dba $defaults, string $component, midcom_core_dbaobject $obj) : org_openpsa_relatedto_dba
77
    {
78
        $link = new org_openpsa_relatedto_dba;
79
80
        $properties = ['fromClass', 'toClass', 'fromGuid', 'toGuid', 'fromComponent', 'toComponent', 'status', 'toExtra', 'toExtra'];
81
        foreach ($properties as $property) {
82
            if (   !empty($defaults->$property)
83
                && empty($link->$property)) {
84
                debug_add("Copying property '{$property}' ('{$defaults->$property}') from defaults");
85
                $link->$property = $defaults->$property;
86
            }
87
        }
88
89
        if (   empty($link->toComponent)
90
            && !empty($link->fromComponent)) {
91
            debug_add("Setting property 'toComponent' to '{$component}'");
92
            $link->toComponent = $component;
93
        } else {
94
            debug_add("Setting property 'fromComponent' to '{$component}'");
95
            $link->fromComponent = $component;
96
        }
97
98
        if (   empty($link->toGuid)
99
            && !empty($link->fromGuid)) {
100
            $link->toClass = get_class($obj);
101
            $link->toGuid = $obj->guid;
102
            debug_add("Setting property 'toGuid' to '{$link->toGuid}'");
103
            debug_add("Setting property 'toClass' to '{$link->toClass}'");
104
        } else {
105
            $link->fromClass = get_class($obj);
106
            $link->fromGuid = $obj->guid;
107
            debug_add("Setting property 'fromGuid' to '{$link->fromGuid}'");
108
            debug_add("Setting property 'fromClass' to '{$link->fromClass}'");
109
        }
110
        return $link;
111
    }
112
}
113