get_fields()   A
last analyzed

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
 * Search for duplicate persons and groups in database
11
 *
12
 * @package org.openpsa.contacts
13
 */
14
class org_openpsa_contacts_duplicates_check_person extends org_openpsa_contacts_duplicates_check
15
{
16
    /**
17
     * Cache memberships when possible
18
     */
19
    private array $membership_cache = [];
20
21 1
    protected function get_class() : string
22
    {
23 1
        return org_openpsa_contacts_person_dba::class;
24
    }
25
26 1
    protected function get_fields() : array
27
    {
28 1
        return ['firstname', 'lastname', 'email', 'handphone', 'city', 'street', 'homephone', 'id'];
29
    }
30
31
    /**
32
     * Calculates P for the given two persons being duplicates
33
     */
34 1
    protected function p_duplicate(array $person1, array $person2) : float
35
    {
36 1
        $p = 0;
37
38
        //TODO: read weight values from configuration
39 1
        if ($this->match('email', $person1, $person2)) {
40 1
            $p += 1;
41
        } elseif (!empty($person1['lastname'])) {
42
            // if user's lastname is part of the email address, check to see if the difference is only in the domain part
43
            $email1 = preg_replace('/@.+/', '', $person1['email']);
44
            $email2 = preg_replace('/@.+/', '', $person2['email']);
45
            if (   str_contains($email1, $person1['lastname'])
46
                && $email1 == $email2) {
47
                $p += 0.5;
48
            }
49
        }
50
51 1
        if ($this->match('handphone', $person1, $person2)) {
52
            $p += 1;
53
        }
54
55 1
        if ($this->match('firstname', $person1, $person2)) {
56
            if ($this->match('homephone', $person1, $person2)) {
57
                $p += 0.7;
58
            }
59
            if ($this->match('lastname', $person1, $person2)) {
60
                if ($this->match('city', $person1, $person2)) {
61
                    $p += 0.5;
62
                }
63
                if ($this->match('street', $person1, $person2)) {
64
                    $p += 0.9;
65
                }
66
67
                // We cannot do this check if person1 hasn't been created yet...
68
                if (!empty($person1['guid'])) {
69
                    $person1_memberships = $this->load_memberships($person1['id']);
70
                    $person2_memberships = $this->load_memberships($person2['id']);
71
                    $matches = array_intersect($person1_memberships, $person2_memberships);
72
                    if (!empty($matches)) {
73
                        $p += (count($matches) * 0.5);
74
                    }
75
                }
76
            }
77
        }
78
79
        // All checks done, return
80 1
        return $p;
81
    }
82
83
    /**
84
     * Get membership maps
85
     */
86
    private function load_memberships(int $id) : array
87
    {
88
        if (!isset($this->membership_cache[$id])) {
89
            $this->membership_cache[$id] = [];
90
            $mc = midcom_db_member::new_collector('uid', $id);
91
            $this->membership_cache[$id] = $mc->get_values('gid');
92
        }
93
        return $this->membership_cache[$id];
94
    }
95
}
96