WorkflowReminderTask::run()   C
last analyzed

Complexity

Conditions 8
Paths 3

Size

Total Lines 39
Code Lines 28

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 39
rs 5.3846
cc 8
eloc 28
nc 3
nop 1
1
<?php
2
/**
3
 * A task that sends a reminder email to users assigned to a workflow that has
4
 * not been actioned for n days.
5
 *
6
 * @package advancedworkflow
7
 */
8
class WorkflowReminderTask extends BuildTask {
0 ignored issues
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...
9
10
	protected $title       = 'Workflow Reminder Task';
11
	protected $description = 'Sends out workflow reminder emails to stale workflow instances';
12
13
	public function run($request) {
14
		$sent = 0;
15
		if (WorkflowInstance::get()->count()) { // Don't attempt the filter if no instances -- prevents a crash
16
			$active = WorkflowInstance::get()
17
					->innerJoin('WorkflowDefinition', '"DefinitionID" = "WorkflowDefinition"."ID"')
18
					->filter(array('WorkflowStatus' => array('Active', 'Paused'), 'RemindDays:GreaterThan' => '0'));
19
			$active->filter(array('RemindDays:GreaterThan' => '0'));
20
			if ($active) foreach ($active as $instance) {
21
				$edited = strtotime($instance->LastEdited);
22
				$days   = $instance->Definition()->RemindDays;
23
24
				if ($edited + $days * 3600 * 24 > time()) {
25
					continue;
26
				}
27
28
				$email   = new Email();
29
				$bcc     = '';
0 ignored issues
show
Unused Code introduced by
$bcc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
30
				$members = $instance->getAssignedMembers();
31
				$target  = $instance->getTarget();
32
33
				if (!$members || !count($members)) continue;
34
35
				$email->setSubject("Workflow Reminder: $instance->Title");
36
				$email->setBcc(implode(', ', $members->column('Email')));
37
				$email->setTemplate('WorkflowReminderEmail');
38
				$email->populateTemplate(array(
39
					'Instance' => $instance,
40
					'Link'     => $target instanceof SiteTree ? "admin/show/$target->ID" : ''
41
				));
42
43
				$email->send();
44
				$sent++;
45
46
				$instance->LastEdited = time();
47
				$instance->write();
48
			}
49
		}
50
		echo "Sent $sent workflow reminder emails.\n";
51
	}
52
53
}