Passed
Push — master ( 02ae83...de4532 )
by Andreas
26:58
created

org_openpsa_relatedto_finder::prepare_links()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * @package org.openpsa.relatedto
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/lgpl.html GNU Lesser General Public License
7
 */
8
9
/**
10
 * Find relatedto suspects
11
 *
12
 * @package org.openpsa.relatedto
13
 */
14
abstract class org_openpsa_relatedto_finder
15
{
16
    abstract public function process();
17
18
    protected function count_links(string $guid, $classes, string $direction) : int
19
    {
20
        // Do no seek if we already have confirmed links
21
        $mc = new org_openpsa_relatedto_collector($this->event->guid, $classes, $direction);
22
        $mc->add_constraint('status', '=', org_openpsa_relatedto_dba::CONFIRMED);
23
        return count($mc->get_related_guids());
24
    }
25
26
    protected function prepare_links(midcom_core_querybuilder $qb, string $component, org_openpsa_relatedto_dba $defaults) : array
27
    {
28
        $links_array = [];
29
        foreach ($qb->execute() as $object) {
30
            $links_array[] = [
31
                'other_obj' => $object,
32
                'link' => $this->defaults_helper($defaults, $component, $object)
33
            ];
34
        }
35
        return $links_array;
36
    }
37
38
    /**
39
     * Fill properties of given $link object from given link object with defaults
40
     *
41
     * Tries to be smart about the direction (inbound vs outbound) properties
42
     */
43
    private static function defaults_helper(org_openpsa_relatedto_dba $defaults, string $component, midcom_core_dbaobject $obj) : org_openpsa_relatedto_dba
44
    {
45
        $link = new org_openpsa_relatedto_dba;
46
47
        $properties = ['fromClass', 'toClass', 'fromGuid', 'toGuid', 'fromComponent', 'toComponent', 'status', 'toExtra', 'toExtra'];
48
        foreach ($properties as $property) {
49
            if (!empty($defaults->$property)) {
50
                debug_add("Copying property '{$property}' ('{$defaults->$property}') from defaults");
51
                $link->$property = $defaults->$property;
52
            }
53
        }
54
55
        if (   empty($link->toComponent)
56
            && !empty($link->fromComponent)) {
57
            debug_add("Setting property 'toComponent' to '{$component}'");
58
            $link->toComponent = $component;
59
        } else {
60
            debug_add("Setting property 'fromComponent' to '{$component}'");
61
            $link->fromComponent = $component;
62
        }
63
64
        if (   empty($link->toGuid)
65
            && !empty($link->fromGuid)) {
66
            $link->toClass = get_class($obj);
67
            $link->toGuid = $obj->guid;
68
            debug_add("Setting property 'toGuid' to '{$link->toGuid}'");
69
            debug_add("Setting property 'toClass' to '{$link->toClass}'");
70
        } else {
71
            $link->fromClass = get_class($obj);
72
            $link->fromGuid = $obj->guid;
73
            debug_add("Setting property 'fromGuid' to '{$link->fromGuid}'");
74
            debug_add("Setting property 'fromClass' to '{$link->fromClass}'");
75
        }
76
        return $link;
77
    }
78
79
    protected function save(array $suspect_links)
80
    {
81
        foreach ($suspect_links as $linkdata) {
82
            if ($linkdata['link']->create()) {
83
                debug_add("saved link to {$linkdata['other_obj']->guid} (link id #{$linkdata['link']->id})", MIDCOM_LOG_INFO);
84
            } else {
85
                debug_add("could not save link to {$linkdata['other_obj']->guid}, errstr: " . midcom_connection::get_error_string(), MIDCOM_LOG_WARN);
86
            }
87
        }
88
    }
89
90
    /**
91
     * Returns a defaults template for relatedto objects
92
     */
93
    protected function suspect_defaults(midcom_core_dbaobject $object, string $component, string $direction = 'incoming') : org_openpsa_relatedto_dba
94
    {
95
        $prefix = $direction == 'incoming' ? 'to' : 'from';
96
97
        $link_def = new org_openpsa_relatedto_dba();
98
        $link_def->{$prefix . 'Component'} = $component;
99
        $link_def->{$prefix . 'Guid'} = $object->guid;
100
        $link_def->{$prefix . 'Class'} = get_class($object);
101
        $link_def->status = org_openpsa_relatedto_dba::SUSPECTED;
102
        return $link_def;
103
    }
104
}