Passed
Push — master ( f35cc9...e819cb )
by Andreas
18:06
created

org_openpsa_invoices_scheduler   B

Complexity

Total Complexity 52

Size/Duplication

Total Lines 386
Duplicated Lines 0 %

Test Coverage

Coverage 93.46%

Importance

Changes 0
Metric Value
eloc 212
dl 0
loc 386
ccs 200
cts 214
cp 0.9346
rs 7.44
c 0
b 0
f 0
wmc 52

11 Methods

Rating   Name   Duplication   Size   Complexity  
B calculate_cycles() 0 30 7
A get_cycle_start() 0 13 3
A _add_month() 0 15 2
A get_cycle_identifier() 0 27 5
A __construct() 0 5 1
A _create_at_entry() 0 19 2
A create_task() 0 44 4
B _notify_owner() 0 58 5
C run_cycle() 0 75 13
A render_task_info() 0 11 3
B calculate_cycle_next() 0 31 7

How to fix   Complexity   

Complex Class

Complex classes like org_openpsa_invoices_scheduler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use org_openpsa_invoices_scheduler, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @package org.openpsa.invoices
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
 * Helper class to process subscription invoicing
11
 *
12
 * @package org.openpsa.invoices
13
 */
14
class org_openpsa_invoices_scheduler extends midcom_baseclasses_components_purecode
15
{
16
    /**
17
     * @var org_openpsa_sales_salesproject_deliverable_dba
18
     */
19
    private $_deliverable;
20
21
    /**
22
     * The day of month on which subscriptions are invoiced (if none is set, they are invoiced continuously)
23
     *
24
     * @var int
25
     */
26
    private $subscription_day;
27
28 32
    public function __construct(org_openpsa_sales_salesproject_deliverable_dba $deliverable)
29
    {
30 32
        parent::__construct();
31 32
        $this->_deliverable = $deliverable;
32 32
        $this->subscription_day = midcom_baseclasses_components_configuration::get('org.openpsa.sales', 'config')->get('subscription_invoice_day_of_month');
33 32
    }
34
35
    /**
36
     * Initiates a new subscription cycle and registers a midcom.services.at call for the next cycle.
37
     *
38
     * The subscription cycles rely on midcom.services.at. I'm not sure if it is wise to rely on it for such
39
     * a totally mission critical part of OpenPSA. Some safeguards might be wise to add.
40
     */
41 8
    public function run_cycle($cycle_number, bool $send_invoice = true) : bool
42
    {
43 8
        if (time() < $this->_deliverable->start) {
44 1
            debug_add('Subscription hasn\'t started yet, register the start-up event to $start');
45 1
            return $this->_create_at_entry($cycle_number, $this->_deliverable->start);
46
        }
47
48 7
        debug_add('Running cycle ' . $cycle_number . ' for deliverable "' . $this->_deliverable->title . '"');
49
50 7
        $this_cycle_start = $this->get_cycle_start($cycle_number, time());
51 7
        if ($this->subscription_day && $cycle_number == 1) {
52
            // If there's a fixed day for invoicing, get_cycle_start already picked a future date for cycle 1
53
            $next_cycle_start = $this_cycle_start + 2; // +2 so we don't get overlaps in task
54
        } else {
55 7
            $next_cycle_start = $this->calculate_cycle_next($this_cycle_start);
56
        }
57 7
        $product = org_openpsa_products_product_dba::get_cached($this->_deliverable->product);
58
59 7
        if ($this->_deliverable->state < org_openpsa_sales_salesproject_deliverable_dba::STATE_STARTED) {
60 2
            $this->_deliverable->state = org_openpsa_sales_salesproject_deliverable_dba::STATE_STARTED;
61 2
            $this->_deliverable->update();
62
        }
63
64 7
        if ($send_invoice) {
65 7
            $calculator = new org_openpsa_invoices_calculator();
66 7
            $this_cycle_amount = $calculator->process_deliverable($this->_deliverable, $cycle_number);
67
        }
68
69 7
        $tasks_completed = [];
70 7
        $tasks_not_completed = [];
71 7
        $new_task = null;
72
73 7
        if ($product->orgOpenpsaObtype == org_openpsa_products_product_dba::TYPE_SERVICE) {
0 ignored issues
show
Bug Best Practice introduced by
The property orgOpenpsaObtype does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
74
            // Close previous task(s)
75 2
            $last_task = null;
76
77 2
            $qb = org_openpsa_projects_task_dba::new_query_builder();
78 2
            $qb->add_constraint('agreement', '=', $this->_deliverable->id);
79 2
            $qb->add_constraint('status', '<', org_openpsa_projects_task_status_dba::COMPLETED);
80
81 2
            foreach ($qb->execute() as $task) {
82 2
                if (org_openpsa_projects_workflow::complete($task, sprintf($this->_i18n->get_string('completed by subscription %s', 'org.openpsa.sales'), $cycle_number))) {
83 2
                    $tasks_completed[] = $task;
84
                } else {
85
                    $tasks_not_completed[] = $task;
86
                }
87 2
                $last_task = $task;
88
            }
89
90
            // Create task for the duration of this cycle
91 2
            $task_title = sprintf('%s %s', $this->_deliverable->title, $this->get_cycle_identifier($this_cycle_start));
0 ignored issues
show
Bug introduced by
It seems like $this->get_cycle_identifier($this_cycle_start) can also be of type false; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
            $task_title = sprintf('%s %s', $this->_deliverable->title, /** @scrutinizer ignore-type */ $this->get_cycle_identifier($this_cycle_start));
Loading history...
92 2
            $new_task = $this->create_task($this_cycle_start, $next_cycle_start - 1, $task_title, $last_task);
93
        }
94
95
        // TODO: Warehouse management: create new order
96 7
        if (   $this->_deliverable->end < $next_cycle_start
97 7
            && $this->_deliverable->end != 0) {
98 1
            debug_add('Do not register next cycle, the contract ends before');
99 1
            return $this->_deliverable->end_subscription();
100
        }
101
102 7
        if (!$this->_create_at_entry($cycle_number + 1, $next_cycle_start)) {
0 ignored issues
show
Bug introduced by
It seems like $next_cycle_start can also be of type false; however, parameter $start of org_openpsa_invoices_scheduler::_create_at_entry() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

102
        if (!$this->_create_at_entry($cycle_number + 1, /** @scrutinizer ignore-type */ $next_cycle_start)) {
Loading history...
103
            return false;
104
        }
105 7
        if ($send_invoice) {
106
            $data = [
107 7
                'cycle_number' => $cycle_number,
108 7
                'next_run' => $next_cycle_start,
109 7
                'invoiced_sum' => $this_cycle_amount,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $this_cycle_amount does not seem to be defined for all execution paths leading up to this point.
Loading history...
110 7
                'tasks_completed' => $tasks_completed,
111 7
                'tasks_not_completed' => $tasks_not_completed
112
            ];
113 7
            $this->_notify_owner($calculator, $new_task, $data);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $calculator does not seem to be defined for all execution paths leading up to this point.
Loading history...
114
        }
115 7
        return true;
116
    }
117
118 8
    private function _create_at_entry($cycle_number, int $start) : bool
119
    {
120
        $args = [
121 8
            'deliverable' => $this->_deliverable->guid,
122 8
            'cycle'       => $cycle_number,
123
        ];
124 8
        $at_entry = new midcom_services_at_entry_dba();
125 8
        $at_entry->start = $start;
126 8
        $at_entry->component = 'org.openpsa.sales';
127 8
        $at_entry->method = 'new_subscription_cycle';
128 8
        $at_entry->arguments = $args;
129
130 8
        if (!$at_entry->create()) {
131
            debug_add('AT registration failed, last midgard error was: ' . midcom_connection::get_error_string(), MIDCOM_LOG_WARN);
132
            return false;
133
        }
134 8
        debug_add('AT entry for cycle ' . $cycle_number . ' created');
135 8
        org_openpsa_relatedto_plugin::create($at_entry, 'midcom.services.at', $this->_deliverable, 'org.openpsa.sales');
136 8
        return true;
137
    }
138
139 7
    private function _notify_owner(org_openpsa_invoices_calculator $calculator, ?org_openpsa_projects_task_dba $new_task, array $data)
140
    {
141 7
        $siteconfig = org_openpsa_core_siteconfig::get_instance();
142 7
        $message = [];
143 7
        $salesproject = org_openpsa_sales_salesproject_dba::get_cached($this->_deliverable->salesproject);
144
        try {
145 7
            $owner = midcom_db_person::get_cached($salesproject->owner);
0 ignored issues
show
Bug Best Practice introduced by
The property owner does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
146
        } catch (midcom_error $e) {
147
            $e->log();
148
            return;
149
        }
150 7
        $customer = $salesproject->get_customer();
0 ignored issues
show
Bug introduced by
The method get_customer() does not exist on midcom_core_dbaobject. It seems like you code against a sub-type of midcom_core_dbaobject such as org_openpsa_invoices_invoice_dba or org_openpsa_sales_salesproject_dba. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

150
        /** @scrutinizer ignore-call */ 
151
        $customer = $salesproject->get_customer();
Loading history...
151 7
        $l10n = $this->_i18n->get_l10n('org.openpsa.sales');
152 7
        if ($data['next_run'] === null) {
153
            $next_run_label = $l10n->get('no more cycles');
154
        } else {
155 7
            $next_run_label = $l10n->get_formatter()->date($data['next_run']);
156
        }
157
158
        // Title for long notifications
159 7
        $message['title'] = sprintf($l10n->get('subscription cycle %d closed for agreement %s (%s)'), $data['cycle_number'], $this->_deliverable->title, $customer->get_label());
160
161
        // Content for long notifications
162 7
        $message['content'] = "{$message['title']}\n\n";
163 7
        $message['content'] .= $l10n->get('invoiced') . ': ' . $l10n->get_formatter()->number($data['invoiced_sum']) . "\n\n";
164
165 7
        if ($data['invoiced_sum'] > 0) {
166 4
            $invoice = $calculator->get_invoice();
167 4
            $message['content'] .= $this->_l10n->get('invoice') . " {$invoice->number}:\n";
168 4
            $url = $siteconfig->get_node_full_url('org.openpsa.invoices');
169 4
            $message['content'] .= $url . 'invoice/' . $invoice->guid . "/\n\n";
0 ignored issues
show
Bug introduced by
Are you sure $url of type false|mixed|null can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

169
            $message['content'] .= /** @scrutinizer ignore-type */ $url . 'invoice/' . $invoice->guid . "/\n\n";
Loading history...
170
        }
171
172 7
        $message['content'] .= $this->render_task_info($l10n->get('tasks completed'), $data['tasks_completed']);
173 7
        $message['content'] .= $this->render_task_info($l10n->get('tasks not completed'), $data['tasks_not_completed']);
174
175 7
        if ($new_task) {
176 2
            $message['content'] .= "\n" . $l10n->get('created new task') . ":\n";
177 2
            $message['content'] .= "{$new_task->title}\n";
178
        }
179
180 7
        $message['content'] .= "\n" . $l10n->get('next run') . ": {$next_run_label}\n\n";
181 7
        $message['content'] .= $this->_i18n->get_string('agreement', 'org.openpsa.projects') . ":\n";
182
183 7
        $url = $siteconfig->get_node_full_url('org.openpsa.sales');
184 7
        $message['content'] .= $url . 'deliverable/' . $this->_deliverable->guid . '/';
185
186
        // Content for short notifications
187 7
        $message['abstract'] = sprintf(
188 7
            $l10n->get('%s: closed subscription cycle %d for agreement %s. invoiced %d. next cycle %s'),
189 7
            $customer->get_label(),
190 7
            $data['cycle_number'],
191 7
            $this->_deliverable->title,
192 7
            $data['invoiced_sum'],
193 7
            $next_run_label);
194
195
        // Send the message out
196 7
        org_openpsa_notifications::notify('org.openpsa.sales:new_subscription_cycle', $owner->guid, $message);
197 7
    }
198
199 7
    private function render_task_info(string $label, array $tasks) : string
200
    {
201 7
        $content = '';
202 7
        if (!empty($tasks)) {
203 2
            $content .= "\n{$label}:\n";
204
205 2
            foreach ($tasks as $task) {
206 2
                $content .= "{$task->title}: {$task->reportedHours}h\n";
207
            }
208
        }
209 7
        return $content;
210
    }
211
212
    /**
213
     * @todo Check if we already have an open task for this delivery?
214
     */
215 3
    public function create_task(int $start, int $end, string $title, org_openpsa_projects_task_dba $source_task = null) : org_openpsa_projects_task_dba
216
    {
217
        // Create the task
218 3
        $task = new org_openpsa_projects_task_dba();
219 3
        $task->title = $title;
220 3
        $task->start = $start;
221 3
        $task->end = $end;
222
        // TODO: Figure out if we really want to keep this
223 3
        $task->hoursInvoiceableDefault = true;
224
225 3
        $task->agreement = $this->_deliverable->id;
226 3
        $task->description = $this->_deliverable->description;
227 3
        $task->plannedHours = $this->_deliverable->plannedUnits;
228
229 3
        $salesproject = org_openpsa_sales_salesproject_dba::get_cached($this->_deliverable->salesproject);
230 3
        $task->customer = $salesproject->customer;
0 ignored issues
show
Bug Best Practice introduced by
The property customer does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
231 3
        $task->manager = $salesproject->owner;
0 ignored issues
show
Bug Best Practice introduced by
The property owner does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
232
233 3
        $project = $salesproject->get_project();
0 ignored issues
show
Bug introduced by
The method get_project() does not exist on midcom_core_dbaobject. Did you maybe mean get_parent()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

233
        /** @scrutinizer ignore-call */ 
234
        $project = $salesproject->get_project();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
234 3
        $task->project = $project->id;
235 3
        $task->orgOpenpsaAccesstype = $project->orgOpenpsaAccesstype;
236 3
        $task->orgOpenpsaOwnerWg = $project->orgOpenpsaOwnerWg;
237
238 3
        if (!empty($source_task)) {
239 3
            $task->priority = $source_task->priority;
240 3
            $task->manager = $source_task->manager;
241 3
            $task->hoursInvoiceableDefault = $source_task->hoursInvoiceableDefault;
242
        }
243
244 3
        if (!$task->create()) {
245
            throw new midcom_error("The task for this cycle could not be created. Last Midgard error was: " . midcom_connection::get_error_string());
246
        }
247 3
        $task->add_members('contacts', array_keys($salesproject->contacts));
0 ignored issues
show
Bug Best Practice introduced by
The property contacts does not exist on midcom_core_dbaobject. Since you implemented __get, consider adding a @property annotation.
Loading history...
248 3
        if (!empty($source_task)) {
249 3
            $source_task->get_members();
250 3
            $task->add_members('resources', array_keys($source_task->resources));
251
        }
252
253
        // Copy tags from deliverable so we can seek resources
254 3
        $tagger = new net_nemein_tag_handler();
255 3
        $tagger->copy_tags($this->_deliverable, $task);
256
257 3
        midcom::get()->uimessages->add($this->_i18n->get_string('org.openpsa.sales', 'org.openpsa.sales'), sprintf($this->_i18n->get_string('created task "%s"', 'org.openpsa.sales'), $task->title));
258 3
        return $task;
259
    }
260
261
    /**
262
     * Calculcate remaining cycles until salesproject's end or the specified number of months passes
263
     *
264
     * @param integer $months The maximum number of months to look forward
265
     * @param integer $start The timestamp from which to begin
266
     */
267 13
    public function calculate_cycles(int $months = null, int $start = null) : int
268
    {
269 13
        if ($start === null) {
270 9
            $start = time();
271
        }
272 13
        $cycles = 0;
273 13
        $cycle_time = $this->_deliverable->start;
274 13
        $end_time = $this->_deliverable->end;
275
276
        // This takes care of invalid/unsupported unit configs
277 13
        if ($this->calculate_cycle_next($cycle_time) === false) {
278
            return $cycles;
279
        }
280
281 13
        while ($cycle_time < $start) {
282 9
            $cycle_time = $this->calculate_cycle_next($cycle_time);
283
        }
284
285 13
        if ($months !== null) {
286 9
            $end_time = mktime(date('H', $cycle_time), date('m', $cycle_time), date('i', $cycle_time), date('m', $cycle_time) + $months, date('d', $cycle_time), date('Y', $cycle_time));
0 ignored issues
show
Bug introduced by
date('H', $cycle_time) of type string is incompatible with the type integer expected by parameter $hour of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

286
            $end_time = mktime(/** @scrutinizer ignore-type */ date('H', $cycle_time), date('m', $cycle_time), date('i', $cycle_time), date('m', $cycle_time) + $months, date('d', $cycle_time), date('Y', $cycle_time));
Loading history...
Bug introduced by
It seems like $cycle_time can also be of type false; however, parameter $timestamp of date() does only seem to accept integer|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

286
            $end_time = mktime(date('H', /** @scrutinizer ignore-type */ $cycle_time), date('m', $cycle_time), date('i', $cycle_time), date('m', $cycle_time) + $months, date('d', $cycle_time), date('Y', $cycle_time));
Loading history...
Bug introduced by
date('Y', $cycle_time) of type string is incompatible with the type integer expected by parameter $year of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

286
            $end_time = mktime(date('H', $cycle_time), date('m', $cycle_time), date('i', $cycle_time), date('m', $cycle_time) + $months, date('d', $cycle_time), /** @scrutinizer ignore-type */ date('Y', $cycle_time));
Loading history...
Bug introduced by
date('i', $cycle_time) of type string is incompatible with the type integer expected by parameter $second of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

286
            $end_time = mktime(date('H', $cycle_time), date('m', $cycle_time), /** @scrutinizer ignore-type */ date('i', $cycle_time), date('m', $cycle_time) + $months, date('d', $cycle_time), date('Y', $cycle_time));
Loading history...
Bug introduced by
date('m', $cycle_time) of type string is incompatible with the type integer expected by parameter $minute of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

286
            $end_time = mktime(date('H', $cycle_time), /** @scrutinizer ignore-type */ date('m', $cycle_time), date('i', $cycle_time), date('m', $cycle_time) + $months, date('d', $cycle_time), date('Y', $cycle_time));
Loading history...
Bug introduced by
date('d', $cycle_time) of type string is incompatible with the type integer expected by parameter $day of mktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

286
            $end_time = mktime(date('H', $cycle_time), date('m', $cycle_time), date('i', $cycle_time), date('m', $cycle_time) + $months, /** @scrutinizer ignore-type */ date('d', $cycle_time), date('Y', $cycle_time));
Loading history...
287
        }
288
289 13
        $cycles = 0;
290 13
        while ($cycle_time < $end_time) {
291 13
            $cycle_time = $this->calculate_cycle_next($cycle_time);
0 ignored issues
show
Bug introduced by
It seems like $cycle_time can also be of type false; however, parameter $time of org_openpsa_invoices_sch...:calculate_cycle_next() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

291
            $cycle_time = $this->calculate_cycle_next(/** @scrutinizer ignore-type */ $cycle_time);
Loading history...
292 13
            if ($cycle_time <= $end_time) {
293 13
                $cycles++;
294
            }
295
        }
296 13
        return $cycles;
297
    }
298
299 21
    public function calculate_cycle_next(int $time)
300
    {
301 21
        switch ($this->_deliverable->unit) {
302 21
            case 'm':
303
                // Monthly recurring subscription
304 14
                $new_date = $this->_add_month($time, 1);
305 14
                break;
306 7
            case 'q':
307
                // Quarterly recurring subscription
308 3
                $new_date = $this->_add_month($time, 3);
309 3
                break;
310 4
            case 'hy':
311
                // Half-yearly recurring subscription
312 1
                $new_date = $this->_add_month($time, 6);
313 1
                break;
314 3
            case 'y':
315
                // Yearly recurring subscription
316 2
                $new_date = new DateTime('+1 year ' . gmdate('Y-m-d', $time), new DateTimeZone('GMT'));
317 2
                break;
318
            default:
319 1
                debug_add('Unrecognized unit value "' . $this->_deliverable->unit . '" for deliverable ' . $this->_deliverable->guid . ", returning false", MIDCOM_LOG_WARN);
320 1
                return false;
321
        }
322
323
        //If previous cycle was run at the end of the month, the new one should be at the end of the month as well
324 20
        $date = new DateTime(gmdate('Y-m-d', $time), new DateTimeZone('GMT'));
325 20
        if (   $date->format('t') == $date->format('j')
326 20
            && $new_date->format('t') != $new_date->format('j')) {
327
            $new_date->setDate((int) $new_date->format('Y'), (int) $new_date->format('m'), (int) $new_date->format('t'));
328
        }
329 20
        return (int) $new_date->format('U');
330
    }
331
332
    /**
333
     * Workaround for odd PHP DateTime behavior where for example
334
     * 2012-10-31 + 1 month would return 2012-12-01. This function makes
335
     * sure the new date is always in the expected month (so in the example above
336
     * it would return 2012-11-30)
337
     *
338
     * @param integer $time Original timestamp
339
     * @param integer $offset number of months to add
340
     */
341 18
    private function _add_month(int $time, int $offset) : DateTime
342
    {
343 18
        $orig = new DateTime(gmdate('Y-m-d', $time), new DateTimeZone('GMT'));
344 18
        $new_date = clone $orig;
345 18
        $new_date->modify('+' . $offset . ' months');
346 18
        $control = clone $new_date;
347 18
        $control->modify('-' . $offset . ' months');
348
349 18
        while ($orig->format('m') !== $control->format('m')) {
350 1
            $new_date->modify('-1 day');
351 1
            $control = clone $new_date;
352 1
            $control->modify('-' . $offset . ' months');
353
        }
354
355 18
        return $new_date;
356
    }
357
358 10
    public function get_cycle_start($cycle_number, int $time)
359
    {
360 10
        if ($cycle_number == 1) {
361 7
            if ($this->subscription_day) {
362 3
                return gmmktime(0, 0, 0, date('n', $time) + 1, $this->subscription_day, date('Y', $time));
0 ignored issues
show
Bug introduced by
date('Y', $time) of type string is incompatible with the type integer expected by parameter $year of gmmktime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

362
                return gmmktime(0, 0, 0, date('n', $time) + 1, $this->subscription_day, /** @scrutinizer ignore-type */ date('Y', $time));
Loading history...
363
            }
364
365
            // no explicit day of month set for invoicing, use the deliverable start date
366 4
            return $this->_deliverable->start;
367
        }
368
369
        // cycle number > 1
370 4
        return $time;
371
    }
372
373 6
    public function get_cycle_identifier(int $time)
374
    {
375 6
        $date = new DateTime(gmdate('Y-m-d', $time), new DateTimeZone('GMT'));
376
377 6
        switch ($this->_deliverable->unit) {
378 6
            case 'm':
379
                // Monthly recurring subscription
380 3
                $identifier = $date->format('Y-m');
381 3
                break;
382 3
            case 'q':
383
                // Quarterly recurring subscription
384 1
                $identifier = ceil(((int)$date->format('n')) / 4) . 'Q' . $date->format('y');
385 1
                break;
386 2
            case 'hy':
387
                // Half-yearly recurring subscription
388 1
                $identifier = ceil(((int)$date->format('n')) / 6) . '/' . $date->format('Y');
389 1
                break;
390 1
            case 'y':
391
                // Yearly recurring subscription
392 1
                $identifier = $date->format('Y');
393 1
                break;
394
            default:
395
                debug_add('Unrecognized unit value "' . $this->_deliverable->unit . '" for deliverable ' . $this->_deliverable->guid . ", returning false", MIDCOM_LOG_WARN);
396
                return false;
397
        }
398
399 6
        return $identifier;
400
    }
401
}
402