WorkflowReminderJob   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5
Metric Value
wmc 13
lcom 1
cbo 5
dl 0
loc 93
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getTitle() 0 3 1
A getSignature() 0 3 1
A __construct() 0 7 3
C process() 0 62 8
1
<?php
2
3
if (class_exists('AbstractQueuedJob')) {
4
/**
5
 * @author <[email protected]>
6
 * @license BSD License http://www.silverstripe.org/bsd-license
7
 */
8
class WorkflowReminderJob extends AbstractQueuedJob {
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
	const DEFAULT_REPEAT = 600;
10
	
11
	/**
12
	 *
13
	 * @var QueuedJobService
14
	 */
15
	public $queuedJobService;
16
	
17
	public function __construct($repeatInterval = 0) {
18
		if (!$this->repeatInterval) {
19
			$this->repeatInterval = $repeatInterval ? $repeatInterval : self::DEFAULT_REPEAT;
20
			$this->totalSteps = 2;
21
			$this->currentStep = 1;
22
		}
23
	}
24
	
25
	public function getTitle() {
26
		return _t('AdvancedWorkflow.WORKFLOW_REMINDER_JOB', 'Workflow Reminder Job');
27
	}
28
	
29
	/**
30
	 * We only want one instance of this job ever
31
	 * 
32
	 * @return string
33
	 */
34
	public function getSignature() {
35
		return md5($this->getTitle());
36
	}
37
	
38
	public function process() {
39
		$sent   = 0;
40
		$filter = array(
41
			'WorkflowStatus'						=> array('Active', 'Paused'),
42
			'Definition.RemindDays:GreaterThan'		=> 0
43
		);
44
		
45
		$active = WorkflowInstance::get()->filter($filter);
46
		
47
		foreach ($active as $instance) {
48
			$edited = strtotime($instance->LastEdited);
49
			$days   = $instance->Definition()->RemindDays;
50
51
			if ($edited + ($days * 3600 * 24) > time()) {
52
				continue;
53
			}
54
55
			$email   = new Email();
56
			$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...
57
			$members = $instance->getAssignedMembers();
58
			$target  = $instance->getTarget();
59
60
			if (!$members || !count($members)) {
61
				continue;
62
			}
63
64
			$email->setSubject("Workflow Reminder: $instance->Title");
65
			$email->setBcc(implode(', ', $members->column('Email')));
66
			$email->setTemplate('WorkflowReminderEmail');
67
			$email->populateTemplate(array(
68
				'Instance' => $instance,
69
				'Link'     => $target instanceof SiteTree ? "admin/show/$target->ID" : ''
70
			));
71
72
			$email->send();
73
			$sent++;
74
75
			// add a comment to the workflow if possible
76
			$action = $instance->CurrentAction();
77
			
78
			$currentComment = $action->Comment;
79
			$action->Comment = sprintf(_t('AdvancedWorkflow.JOB_REMINDER_COMMENT', '%s: Reminder email sent\n\n'), date('Y-m-d H:i:s')) . $currentComment;
80
			try {
81
				$action->write();
82
			} catch (Exception $ex) {
83
				SS_Log::log($ex, SS_Log::WARN);
84
			}
85
86
			$instance->LastEdited = time();
87
			try {
88
				$instance->write();
89
			} catch (Exception $ex) {
90
				SS_Log::log($ex, SS_Log::WARN);
91
			}
92
		}
93
94
		$this->currentStep = 2;
95
		$this->isComplete = true;
96
		
97
		$nextDate = date('Y-m-d H:i:s', time() + $this->repeatInterval);
98
		$this->queuedJobService->queueJob(new WorkflowReminderJob($this->repeatInterval), $nextDate);
99
	}
100
}
101
}