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

new_subscription_cycle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 3
eloc 10
c 3
b 0
f 0
nc 3
nop 2
dl 0
loc 16
ccs 0
cts 10
cp 0
crap 12
rs 9.9332
1
<?php
2
/**
3
 * @package org.openpsa.sales
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
 * OpenPSA Sales management component
11
 *
12
 * @package org.openpsa.sales
13
 */
14
class org_openpsa_sales_interface extends midcom_baseclasses_components_interface
15
implements midcom_services_permalinks_resolver
16
{
17
    public function resolve_object_link(midcom_db_topic $topic, midcom_core_dbaobject $object) : ?string
18
    {
19
        if ($object instanceof org_openpsa_sales_salesproject_dba) {
20
            return "salesproject/{$object->guid}/";
21
        }
22
        if ($object instanceof org_openpsa_sales_salesproject_deliverable_dba) {
23
            return "deliverable/{$object->guid}/";
24
        }
25
        return null;
26
    }
27
28
    /**
29
     * AT handler for handling subscription cycles.
30
     */
31
    public function new_subscription_cycle(array $args, midcom_baseclasses_components_cron_handler $handler)
32
    {
33
        if (!isset($args['deliverable'], $args['cycle'])) {
34
            $handler->print_error('deliverable GUID or cycle number not set, aborting');
35
            return false;
36
        }
37
38
        try {
39
            $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']);
40
        } catch (midcom_error $e) {
41
            $handler->print_error("Deliverable {$args['deliverable']} not found: " . midcom_connection::get_error_string());
42
            return false;
43
        }
44
        $scheduler = new org_openpsa_invoices_scheduler($deliverable);
45
46
        return $scheduler->run_cycle($args['cycle']);
47
    }
48
49
    /**
50
     * Function to send a notification to owner of the deliverable - guid of deliverable is passed
51
     */
52
    public function new_notification_message(array $args, midcom_baseclasses_components_cron_handler $handler)
53
    {
54
        if (!isset($args['deliverable'])) {
55
            $handler->print_error('deliverable GUID not set, aborting');
56
            return false;
57
        }
58
        try {
59
            $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']);
60
        } catch (midcom_error $e) {
61
            $handler->print_error('no deliverable with passed GUID: ' . $args['deliverable'] . ', aborting');
62
            return false;
63
        }
64
65
        //get the owner of the salesproject the deliverable belongs to
66
        try {
67
            $project = new org_openpsa_sales_salesproject_dba($deliverable->salesproject);
68
        } catch (midcom_error $e) {
69
            $handler->print_error('Failed to load salesproject: ' . $e->getMessage());
70
            return false;
71
        }
72
73
        $message = [
74
            'title' => sprintf($this->_l10n->get('notification for agreement %s'), $deliverable->title),
75
            'content' => sprintf(
76
                $this->_l10n->get('agreement %s ends on %s. click here: %s'),
77
                $deliverable->title,
78
                $this->_l10n->get_formatter()->date($deliverable->end),
79
                midcom::get()->permalinks->create_permalink($deliverable->guid)
80
            )
81
        ];
82
83
        return org_openpsa_notifications::notify('org.openpsa.sales:new_notification_message', $project->owner, $message);
84
    }
85
}
86