Passed
Branch master (d4715f)
by Andreas
26:06
created

org_openpsa_projects_role_dba::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 11
nc 2
nop 3
dl 0
loc 16
ccs 12
cts 12
cp 1
crap 2
rs 9.9
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package org.openpsa.projects
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
 * @property integer $project
11
 * @property integer $role
12
 * @property string $description
13
 * @property integer $person
14
 * @property integer $status
15
 * @package org.openpsa.projects
16
 */
17
class org_openpsa_projects_role_dba extends midcom_core_dbaobject
18
{
19
    public $__midcom_class_name__ = __CLASS__;
20
    public $__mgdschema_class_name__ = 'org_openpsa_role';
21
22
    public $_use_rcs = false;
23
24 19
    public static function add(int $project, int $person, int $role) : bool
25
    {
26 19
        $mc = self::new_collector('project', $project);
27 19
        $mc->add_constraint('role', '=', $role);
28 19
        $mc->add_constraint('person', '=', $person);
29 19
        $mc->execute();
30 19
        if ($mc->count() > 0) {
31
            //Resource is already present, aborting silently
32 7
            return true;
33
        }
34
35 17
        $new_role = new self();
36 17
        $new_role->person = $person;
37 17
        $new_role->role = $role;
38 17
        $new_role->project = $project;
39 17
        return $new_role->create();
40
    }
41
42
    /**
43
     * Returns true for NO existing duplicates
44
     */
45 17
    public function check_duplicates() : bool
46
    {
47 17
        $qb = new midgard_query_builder('org_openpsa_role');
48 17
        $qb->add_constraint('person', '=', $this->person);
49 17
        $qb->add_constraint('objectGuid', '=', $this->objectGuid);
0 ignored issues
show
Bug Best Practice introduced by
The property objectGuid does not exist on org_openpsa_projects_role_dba. Since you implemented __get, consider adding a @property annotation.
Loading history...
50 17
        $qb->add_constraint('role', '=', $this->role);
51
52 17
        if ($this->id) {
53
            $qb->add_constraint('id', '<>', $this->id);
54
        }
55
56 17
        return $qb->count() == 0;
57
    }
58
59 17
    public function _on_creating() : bool
60
    {
61 17
        return $this->check_duplicates();
62
    }
63
64
    public function _on_updating() : bool
65
    {
66
        return $this->check_duplicates();
67
    }
68
}
69