Passed
Push — master ( 58b223...1219ea )
by Andreas
22:48
created

org_openpsa_sales_handler::process_notify_date()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 23.2584

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 28
c 1
b 0
f 0
nc 10
nop 2
dl 0
loc 43
ccs 8
cts 26
cp 0.3076
crap 23.2584
rs 8.5386
1
<?php
2
/**
3
 * @package org.openpsa.sales
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
 * Handler addons
11
 *
12
 * @package org.openpsa.sales
13
 */
14
trait org_openpsa_sales_handler
15
{
16
    /**
17
     * Function to process the notify date
18
     * creates/edits/deletes the corresponding at_entry if needed
19
     *
20
     * @param integer $notify The notify date
21
     * @param org_openpsa_sales_salesproject_deliverable_dba $deliverable The current deliverable
22
     */
23 3
    public function process_notify_date($notify, org_openpsa_sales_salesproject_deliverable_dba $deliverable)
24
    {
25
        //check if there is already an at_entry
26 3
        $mc = org_openpsa_relatedto_dba::new_collector('toGuid', $deliverable->guid);
27 3
        $mc->add_constraint('fromClass', '=', midcom_services_at_entry_dba::class);
28 3
        $mc->add_constraint('toClass', '=', org_openpsa_sales_salesproject_deliverable_dba::class);
29 3
        $mc->add_constraint('toExtra', '=', 'notify_at_entry');
30 3
        $at_entries = $mc->get_values('fromGuid');
31
32
        //check date
33 3
        if ($notify) {
34
            $at_entry = null;
35
36
            //get guid of at_entry
37
            foreach ($at_entries as $guid) {
38
                //check if related at_entry exists
39
                try {
40
                    $at_entry = new midcom_services_at_entry_dba($guid);
41
                } catch (midcom_error $e) {
42
                    $e->log();
43
                }
44
            }
45
46
            if ($at_entry === null) {
47
                $at_entry = new midcom_services_at_entry_dba;
48
                $at_entry->method = 'new_notification_message';
49
                $at_entry->component = 'org.openpsa.sales';
50
                $at_entry->arguments = ['deliverable' => $deliverable->guid];
51
                $at_entry->create();
52
                //relatedto from notification to deliverable
53
                org_openpsa_relatedto_plugin::create($at_entry, 'midcom.services.at', $deliverable, 'org.openpsa.sales', null, ['toExtra' => 'notify_at_entry']);
54
            }
55
            $at_entry->start = $notify;
56
            $at_entry->update();
57
        } else {
58
            //void date - so delete existing at_entries for this notify_date
59 3
            foreach ($at_entries as $guid) {
60
                try {
61
                    $at_entry = new midcom_services_at_entry_dba($guid);
62
                    //check if related at_entry exists & delete it
63
                    $at_entry->delete();
64
                } catch (midcom_error $e) {
65
                    $e->log();
66
                }
67
            }
68
        }
69 3
    }
70
}
71