Completed
Push — master ( 7668ba...b7b643 )
by Andreas
08:40
created

org_openpsa_sales_interface   C

Complexity

Total Complexity 23

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 19

Importance

Changes 0
Metric Value
dl 0
loc 213
rs 6.875
c 0
b 0
f 0
wmc 23
lcom 2
cbo 19

6 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve_object_link() 0 12 3
A org_openpsa_relatedto_find_suspects() 0 15 4
B _find_suspects_event() 0 43 4
B _find_suspects_person() 0 41 4
B new_subscription_cycle() 0 23 4
B new_notification_message() 0 42 4
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
1 ignored issue
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
15
implements midcom_services_permalinks_resolver
1 ignored issue
show
Coding Style introduced by
The implements keyword must be on the same line as the class name
Loading history...
16
{
17
    public function resolve_object_link(midcom_db_topic $topic, midcom_core_dbaobject $object)
18
    {
19
        if ($object instanceof org_openpsa_sales_salesproject_dba)
20
        {
21
            return "salesproject/{$object->guid}/";
22
        }
23
        if ($object instanceof org_openpsa_sales_salesproject_deliverable_dba)
24
        {
25
            return "deliverable/{$object->guid}/";
26
        }
27
        return null;
28
    }
29
30
    /**
31
     * Used by org_openpsa_relatedto_suspect::find_links_object to find "related to" information
32
     *
33
     * Currently handles persons
34
     */
35
    public function org_openpsa_relatedto_find_suspects(midcom_core_dbaobject $object, $defaults, array &$links_array)
36
    {
37
        switch (true)
38
        {
39
            case midcom::get()->dbfactory->is_a($object, 'midcom_db_person'):
40
                //List all projects and tasks given person is involved with
41
                $this->_find_suspects_person($object, $defaults, $links_array);
42
                break;
43
            case midcom::get()->dbfactory->is_a($object, 'midcom_db_event'):
44
            case midcom::get()->dbfactory->is_a($object, 'org_openpsa_calendar_event_dba'):
45
                $this->_find_suspects_event($object, $defaults, $links_array);
46
                break;
47
                //TODO: groups ? other objects ?
48
        }
49
    }
50
51
    /**
52
     * Used by org_openpsa_relatedto_find_suspects to in case the given object is a person
53
     *
54
     * Current rule: all participants of event must be either manager,contact or resource in task
55
     * that overlaps in time with the event.
56
     */
57
    private function _find_suspects_event(midcom_core_dbaobject $object, $defaults, array &$links_array)
58
    {
59
        if (   !is_array($object->participants)
60
            || count($object->participants) < 2)
61
        {
62
            //We have invalid list or less than two participants, abort
63
            return;
64
        }
65
        $mc = org_openpsa_contacts_role_dba::new_collector('role', org_openpsa_sales_salesproject_dba::ROLE_MEMBER);
66
        $mc->add_constraint('person', 'IN', array_keys($object->participants));
67
        $guids = $mc->get_values('objectGuid');
68
69
        $qb = org_openpsa_sales_salesproject_dba::new_query_builder();
70
71
        // Target sales project starts or ends inside given events window or starts before and ends after
72
        $qb->add_constraint('start', '<=', $object->end);
73
        $qb->begin_group('OR');
74
            $qb->add_constraint('end', '>=', $object->start);
75
            $qb->add_constraint('end', '=', 0);
76
        $qb->end_group();
77
78
        //Target sales project is active
79
        $qb->add_constraint('state', '=', org_openpsa_sales_salesproject_dba::STATE_ACTIVE);
80
81
        //Each event participant is either manager or member (resource/contact) in task
82
        $qb->begin_group('OR');
83
            $qb->add_constraint('owner', 'IN', array_keys($object->participants));
84
            $qb->add_constraint('guid', 'IN', $guids);
85
        $qb->end_group();
86
87
        $qbret = $qb->execute();
88
89
        foreach ($qbret as $salesproject)
0 ignored issues
show
Bug introduced by
The expression $qbret of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
90
        {
91
            $to_array = array('other_obj' => false, 'link' => false);
92
            $link = new org_openpsa_relatedto_dba();
93
            org_openpsa_relatedto_suspect::defaults_helper($link, $defaults, $this->_component, $salesproject);
94
            $to_array['other_obj'] = $salesproject;
95
            $to_array['link'] = $link;
96
97
            $links_array[] = $to_array;
98
        }
99
    }
100
101
    /**
102
     * Used by org_openpsa_relatedto_find_suspects to in case the given object is a person
103
     */
104
    private function _find_suspects_person(midcom_core_dbaobject $object, $defaults, array &$links_array)
105
    {
106
        $seen_sp = array();
107
        $mc = org_openpsa_contacts_role_dba::new_collector('role', org_openpsa_sales_salesproject_dba::ROLE_MEMBER);
108
        $mc->add_constraint('person', '=', $object->id);
109
        $guids = $mc->get_values('objectGuid');
110
111
        if (!empty($guids))
112
        {
113
            $qb = org_openpsa_sales_salesproject_dba::new_query_builder();
114
            $qb->add_constraint('state', '=', org_openpsa_sales_salesproject_dba::STATE_ACTIVE);
115
            $qb->add_constraint('guid', 'IN', $guids);
116
            $qbret = $qb->execute();
117
            foreach ($qbret as $salesproject)
0 ignored issues
show
Bug introduced by
The expression $qbret of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
118
            {
119
                $seen_sp[$salesproject->id] = true;
120
                $to_array = array('other_obj' => false, 'link' => false);
121
                $link = new org_openpsa_relatedto_dba();
122
                org_openpsa_relatedto_suspect::defaults_helper($link, $defaults, $this->_component, $salesproject);
123
                $to_array['other_obj'] = $salesproject;
124
                $to_array['link'] = $link;
125
126
                $links_array[] = $to_array;
127
            }
128
        }
129
        $qb2 = org_openpsa_sales_salesproject_dba::new_query_builder();
130
        $qb2->add_constraint('owner', '=', $object->id);
131
        $qb2->add_constraint('state', '=', org_openpsa_sales_salesproject_dba::STATE_ACTIVE);
132
        $qb2->add_constraint('id', 'NOT IN', array_keys($seen_sp));
133
        $qb2ret = $qb2->execute();
134
        foreach ($qb2ret as $sp)
0 ignored issues
show
Bug introduced by
The expression $qb2ret of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
135
        {
136
            $to_array = array('other_obj' => false, 'link' => false);
137
            $link = new org_openpsa_relatedto_dba();
138
            org_openpsa_relatedto_suspect::defaults_helper($link, $defaults, $this->_component, $sp);
139
            $to_array['other_obj'] = $sp;
140
            $to_array['link'] = $link;
141
142
            $links_array[] = $to_array;
143
        }
144
    }
145
146
    /**
147
     * AT handler for handling subscription cycles.
148
     *
149
     * @param array $args handler arguments
150
     * @param midcom_baseclasses_components_cron_handler $handler cron_handler object calling this method.
151
     * @return boolean indicating success/failure
152
     */
153
    function new_subscription_cycle(array $args, midcom_baseclasses_components_cron_handler $handler)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
154
    {
155
        if (   !isset($args['deliverable'])
156
            || !isset($args['cycle']))
157
        {
158
            $handler->print_error('deliverable GUID or cycle number not set, aborting');
159
            return false;
160
        }
161
162
        try
163
        {
164
            $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']);
165
        }
166
        catch (midcom_error $e)
167
        {
168
            $msg = "Deliverable {$args['deliverable']} not found, error " . midcom_connection::get_error_string();
169
            $handler->print_error($msg);
170
            return false;
171
        }
172
        $scheduler = new org_openpsa_invoices_scheduler($deliverable);
173
174
        return $scheduler->run_cycle($args['cycle']);
175
    }
176
177
    /**
178
     * Function to send a notification to owner of the deliverable - guid of deliverable is passed
179
     *
180
     * @param array $args handler arguments
181
     * @param midcom_baseclasses_components_cron_handler $handler cron_handler object calling this method.
182
     * @return boolean indicating success/failure
183
     */
184
    public function new_notification_message(array $args, midcom_baseclasses_components_cron_handler $handler)
185
    {
186
        if (!isset($args['deliverable']))
187
        {
188
            $handler->print_error('deliverable GUID not set, aborting');
189
            return false;
190
        }
191
        try
192
        {
193
            $deliverable = new org_openpsa_sales_salesproject_deliverable_dba($args['deliverable']);
194
        }
195
        catch (midcom_error $e)
196
        {
197
            $handler->print_error('no deliverable with passed GUID: ' . $args['deliverable'] . ', aborting');
198
            return false;
199
        }
200
201
        //get the owner of the salesproject the deliverable belongs to
202
        try
203
        {
204
            $project = new org_openpsa_sales_salesproject_dba($deliverable->salesproject);
205
        }
206
        catch (midcom_error $e)
207
        {
208
            $handler->print_error('Failed to load salesproject: ' . $e->getMessage());
209
            return false;
210
        }
211
212
        $message = array
213
        (
214
            'title' => sprintf($this->_l10n->get('notification for agreement %s'), $deliverable->title),
215
            'content' => sprintf
216
            (
217
                $this->_l10n->get('agreement %s ends on %s. click here: %s'),
218
                $deliverable->title,
219
                $this->_l10n->get_formatter()->date($deliverable->end),
220
                midcom::get()->permalinks->create_permalink($deliverable->guid)
221
            )
222
        );
223
224
        return org_openpsa_notifications::notify('org.openpsa.sales:new_notification_message', $project->owner, $message);
225
    }
226
}
227