-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkflow.php
More file actions
188 lines (141 loc) · 4.54 KB
/
Copy pathWorkflow.php
File metadata and controls
188 lines (141 loc) · 4.54 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace App\Workflows;
abstract class Workflow
{
public static $STARTED_STATUS_VALUE = "STARTED";
public static $ENDED_STATUS_VALUE = "FINISHED";
/**
* Cada workflow possui uma entidade que devemos registrar seu estado. Ex.: PurchaseWorkflow temos o compra,
* EditalWorkflow temos edital
* @var \Object artifact
*/
protected $artifact;
/**
* Tipos de usuários no qual o artifact pode estar presente.
* @var array places
*/
protected $places = [];
/**
* Transições que o artifact tem. Exemplo:
* <code>
* "transition_name" => [
* "from" => "previous_place",
* "to" => "next_place"
* ]
* </code>
* @var array transitions
*/
protected $transitions = [];
/**
* CRUD
*
* @var array
*/
protected $actions = [
'create',
'read',
'update',
'delete'
];
/**
* Retorna array de estados
*/
private function flushTransitions() : array {
$temp = [];
foreach ($this->transitions as $transition => $value) {
array_push($temp, $transition);
}
return $temp;
}
/**
* Dado um estado, retorna próximo estado
*/
protected function getNext($string) : string {
$transitions = $this->flushTransitions();
if($string == null) return $transitions[0];
if($string == Workflow::$ENDED_STATUS_VALUE) return Workflow::$ENDED_STATUS_VALUE;
$length = sizeof($transitions);
for ($i = 0; $i < $length; $i++) {
if($transitions[$i] == $string) {
if($i == $length - 1) {
return Workflow::$ENDED_STATUS_VALUE;
}
return $transitions[$i + 1];
}
}
return $transitions[0];
}
/**
* Dado um estado, retorna ao estado posterior.
*/
protected function getPrevious($string) : string {
$transitions = $this->flushTransitions();
$length = sizeof($transitions);
if($string == null) return Workflow::$STARTED_STATUS_VALUE;
if($string == Workflow::$STARTED_STATUS_VALUE) return Workflow::$STARTED_STATUS_VALUE;
if($string == Workflow::$ENDED_STATUS_VALUE) return $transitions[$length - 1];
for ($i = -1; $i < $length - 1; $i++) {
if($transitions[$i + 1] == $string) {
if($i == -1) {
return Workflow::$STARTED_STATUS_VALUE;
}
return $transitions[$i];
}
}
return $transitions[0];
}
/**
* Função responsável por voltar o estado do $artifact
*/
public function previous() {
$oldStatus = $this->artifact->status;
$newStatus = $this->getPrevious($oldStatus);
$this->artifact->status = $newStatus;
$this->notify($oldStatus, $newStatus, false);
}
/**
* Função responsável por avançar o estado do $artifact
*/
public function next() {
$oldStatus = $this->artifact->status;
$newStatus = $this->getNext($oldStatus);
$this->artifact->status = $newStatus;
$this->notify($oldStatus, $newStatus, true);
}
/**
* Função Handler que é chamada sempre quando ocorre mudança no estado do Workflow.
*
* @param $oldStatus
* @param $newStatus
* @param bool $isNext
* @return void
*/
private function notify($oldStatus, $newStatus, $isNext = true) {
$oldStatus = ucfirst(strtolower($oldStatus));
$newStatus = ucfirst(strtolower($newStatus));
$method = "onChangeFrom{$oldStatus}To{$newStatus}";
if(method_exists($this, $method)) {
$this->{$method}();
}
$text = $isNext ? "avancou" : "voltou";
//.. envio de e-mail, início de outro workflow, etc..
print("Alerta! Status mudou: ({$text}) {$oldStatus} => {$newStatus}. Novo Objeto: {$this->artifact->toJson()} \n");
}
/**
* Configura o artefato principal do projeto.
*
* @param WorkflowArtifact $artifact
*/
public function setArtifact($artifact) {
$this->artifact = $artifact;
}
public function getArtifact($id = null) {
return $this->artifact;
}
/**
* Função responsável por fazer um filtro por usuário. A função é o ponto de encontro em Workflow$places e
* Workflow$actions.
* @return boolean
*/
abstract function authorize($action) : boolean;
}