forked from AppStateESS/InternshipInventory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkflowTransitionFactory.php
93 lines (73 loc) · 2.98 KB
/
WorkflowTransitionFactory.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace Intern;
use \PHPWS_Core;
class WorkflowTransitionFactory {
private static $dir = 'WorkflowTransition';
/**
* Get the list of transitions currently available for a given state
*
* @param WorkflowState $state State object to get transitions for
* @param Internship $i Internship object that this state applies to
* @return Array Array of available transitions
*/
public static function getTransitionsFromState(WorkflowState $state, Internship $i)
{
$stateName = $state->getName();
// Strip namespace from state name by matching the last word character in the path
preg_match('/\w*$/', $stateName, $matches);
$stateName = $matches[0];
$transitions = self::getAllTransitions(); // Get all transitions
$outgoingTrans = array();
// Filter the list of all transitions by finding the transitions which have this state
// in their list of potential source states
foreach($transitions as $t){
// Set the actual source state
$t->setSourceState($state);
if(is_array($t->getSourceState()) && in_array($stateName, $t->getSourceState()) && $t->isApplicable($i)){
$outgoingTrans[] = $t;
}else if(($t->getSourceState() == $stateName || $t->getSourceState() == '*') && $t->isApplicable($i)){
$outgoingTrans[] = $t;
}
}
uasort($outgoingTrans, array('self', 'sortTransitions'));
return $outgoingTrans;
}
public static function getTransitionByName($name)
{
if(!isset($name)){
throw new \InvalidArgumentException('Missing transition name.');
}
return new $name;
}
public static function getAllTransitions()
{
$dir = PHPWS_SOURCE_DIR . 'mod/intern/class/' . self::$dir;
// Get the directory listing and filter out anything that doesn't look right
$files = scandir("{$dir}/");
$transitions = array();
foreach($files as $f){
// Look for directories that don't start with '.'
if(!is_dir($dir . '/' . $f) && substr($f, 0, 1) != '.'){
// Include each one
PHPWS_Core::initModClass('intern', self::$dir . '/' . $f);
$className = preg_replace('/\.php/', '', $f);
$className = '\Intern\WorkflowTransition\\' . $className;
// Instanciate each one
$transitions[] = new $className;
}
}
return $transitions;
}
/**
* Call-back function for sorting transitions by their priority. Lower sort index => lower priority.
* @param WorkflowTransition $a
* @param WorkflowTransition $b
*/
private static function sortTransitions(WorkflowTransition $a, WorkflowTransition $b)
{
if($a->getSortIndex() == $b->getSortIndex()){
return 0;
}
return ($a->getSortIndex() < $b->getSortIndex()) ? -1 : 1;
}
}