forked from AppStateESS/InternshipInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkflowController.php
66 lines (49 loc) · 2.21 KB
/
WorkflowController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace Intern;
class WorkflowController {
private $internship;
private $t;
public function __construct(Internship $i, WorkflowTransition $t)
{
$this->internship = $i;
$this->t = $t;
}
public function doTransition($note = null)
{
// Make sure the transition makes sense based on the current state of the internship
$currStateName = $this->internship->getStateName();
$sourceStateName = $this->t->getSourceState();
if(is_array($sourceStateName)){
if(!in_array($currStateName, $sourceStateName)){
throw new \InvalidArgumentException('Invalid transition source state.');
}
}else if($sourceStateName != '*' && $sourceStateName != $currStateName){
throw new \InvalidArgumentException('Invalid transition source state.');
}
if(!$this->t->allowed($this->internship)){
throw new Exception("You do not have permission to set the internship to the requested status.");
}
// Check that the fields required to take this transition have been filled in
// Will throw an exception in the case of any missing data.
$this->t->checkRequiredFields($this->internship);
$sourceState = WorkflowStateFactory::getState($currStateName);
$destStateName = $this->t->getDestState();
if($destStateName == null){
// No destination state, so see if we need to add a note (no state change)
if(!is_null($note)){
$changeHistory = new ChangeHistory($this->internship, \Current_User::getUserObj(), time(), $sourceState, $sourceState, $note);
$changeHistory->save();
}
return;
}
$destState = WorkflowStateFactory::getState($destStateName);
$this->t->onTransition($this->internship);
$this->internship->setState($destState);
$this->internship->save();
$changeHistory = new ChangeHistory($this->internship, \Current_User::getUserObj(), time(), $sourceState, $destState, $note);
$changeHistory->save();
}
public function doNotification($note = null){
$this->t->doNotification($this->internship, $note);
}
}