-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathProcess.php
More file actions
209 lines (183 loc) · 7.75 KB
/
Process.php
File metadata and controls
209 lines (183 loc) · 7.75 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/*
* Copyright (c) Atsuhiro Kubo <kubo@iteman.jp> and contributors,
* All rights reserved.
*
* This file is part of Workflower.
*
* This program and the accompanying materials are made available under
* the terms of the BSD 2-Clause License which accompanies this
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
*/
namespace PHPMentors\Workflower\Process;
use PHPMentors\Workflower\Workflow\Activity\ActivityInterface;
use PHPMentors\Workflower\Workflow\Activity\UnexpectedActivityStateException;
use PHPMentors\Workflower\Workflow\Event\StartEvent;
use PHPMentors\Workflower\Workflow\Operation\OperationRunnerInterface;
use PHPMentors\Workflower\Workflow\Workflow;
use PHPMentors\Workflower\Workflow\WorkflowRepositoryInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
class Process
{
/**
* @var int|string|WorkflowContextInterface
*/
private $workflowContext;
/**
* @var WorkflowRepositoryInterface
*/
private $workflowRepository;
/**
* @var ExpressionLanguage
*
* @since 1.2.0
*/
private $expressionLanguage;
/**
* @var OperationRunnerInterface
*
* @since 1.2.0
*/
private $operationRunner;
/**
* @param int|string|WorkflowContextInterface $workflowContext
* @param WorkflowRepositoryInterface $workflowRepository
* @param OperationRunnerInterface $operationRunner
*/
public function __construct($workflowContext, WorkflowRepositoryInterface $workflowRepository, OperationRunnerInterface $operationRunner)
{
$this->workflowContext = $workflowContext;
$this->workflowRepository = $workflowRepository;
$this->operationRunner = $operationRunner;
}
/**
* @param ExpressionLanguage $expressionLanguage
*
* @since 1.2.0
*/
public function setExpressionLanguage(ExpressionLanguage $expressionLanguage)
{
$this->expressionLanguage = $expressionLanguage;
}
/**
* @param EventContextInterface $eventContext
*/
public function start(EventContextInterface $eventContext)
{
assert($eventContext->getProcessContext() !== null);
assert($eventContext->getProcessContext()->getWorkflow() === null);
assert($eventContext->getEventId() !== null);
$workflow = $this->configureWorkflow($this->createWorkflow());
$eventContext->getProcessContext()->setWorkflow($workflow);
$workflow->setProcessData($eventContext->getProcessContext()->getProcessData());
$flowObject = $workflow->getFlowObject($eventContext->getEventId());
$workflow->start(/* @var $flowObject StartEvent */$flowObject);
}
/**
* @param WorkItemContextInterface $workItemContext
*/
public function allocateWorkItem(WorkItemContextInterface $workItemContext)
{
assert($workItemContext->getProcessContext() !== null);
assert($workItemContext->getProcessContext()->getWorkflow() !== null);
assert($workItemContext->getActivityId() !== null);
$workflow = $this->configureWorkflow($workItemContext->getProcessContext()->getWorkflow());
$flowObject = $workflow->getFlowObject($workItemContext->getActivityId());
$workflow->allocateWorkItem(/* @var $flowObject ActivityInterface */$flowObject, $workItemContext->getParticipant());
}
/**
* @param WorkItemContextInterface $workItemContext
*/
public function startWorkItem(WorkItemContextInterface $workItemContext)
{
assert($workItemContext->getProcessContext() !== null);
assert($workItemContext->getProcessContext()->getWorkflow() !== null);
assert($workItemContext->getActivityId() !== null);
$workflow = $this->configureWorkflow($workItemContext->getProcessContext()->getWorkflow());
$flowObject = $workflow->getFlowObject($workItemContext->getActivityId());
$workflow->startWorkItem(/* @var $flowObject ActivityInterface */$flowObject, $workItemContext->getParticipant());
}
/**
* @param WorkItemContextInterface $workItemContext
*/
public function completeWorkItem(WorkItemContextInterface $workItemContext)
{
assert($workItemContext->getProcessContext() !== null);
assert($workItemContext->getProcessContext()->getWorkflow() !== null);
assert($workItemContext->getActivityId() !== null);
$workflow = $this->configureWorkflow($workItemContext->getProcessContext()->getWorkflow());
$workflow->setProcessData($workItemContext->getProcessContext()->getProcessData());
$flowObject = $workflow->getFlowObject($workItemContext->getActivityId());
$workflow->completeWorkItem(/* @var $flowObject ActivityInterface */$flowObject, $workItemContext->getParticipant());
}
/**
* @param WorkItemContextInterface $workItemContext
*
* @throws UnexpectedActivityStateException
*/
public function executeWorkItem(WorkItemContextInterface $workItemContext)
{
assert($workItemContext->getProcessContext() !== null);
assert($workItemContext->getProcessContext()->getWorkflow() !== null);
assert($workItemContext->getActivityId() !== null);
assert($workItemContext->getProcessContext()->getWorkflow()->getFlowObject($workItemContext->getActivityId()) instanceof ActivityInterface);
$activity = $workItemContext->getProcessContext()->getWorkflow()->getFlowObject($workItemContext->getActivityId()); /* @var $activity ActivityInterface */
if ($activity->isAllocatable()) {
$this->allocateWorkItem($workItemContext);
$nextWorkItemContext = new WorkItemContext($workItemContext->getParticipant());
$nextWorkItemContext->setActivityId($workItemContext->getActivityId());
$nextWorkItemContext->setProcessContext($workItemContext->getProcessContext());
return $this->executeWorkItem($nextWorkItemContext);
} elseif ($activity->isStartable()) {
$this->startWorkItem($workItemContext);
$nextWorkItemContext = new WorkItemContext($workItemContext->getParticipant());
$nextWorkItemContext->setActivityId($workItemContext->getActivityId());
$nextWorkItemContext->setProcessContext($workItemContext->getProcessContext());
return $this->executeWorkItem($nextWorkItemContext);
} elseif ($activity->isCompletable()) {
$this->completeWorkItem($workItemContext);
} else {
throw new UnexpectedActivityStateException(sprintf('The current work item of the activity "%s" is not executable.', $activity->getId()));
}
}
/**
* @return int|string|WorkflowContextInterface
*
* @since 1.1.0
*/
public function getWorkflowContext()
{
return $this->workflowContext;
}
/**
* @return Workflow
*
* @throws WorkflowNotFoundException
*/
private function createWorkflow()
{
$workflowId = $this->workflowContext instanceof WorkflowContextInterface ? $this->workflowContext->getWorkflowId() : $this->workflowContext;
$workflow = $this->workflowRepository->findById($workflowId);
if ($workflow === null) {
throw new WorkflowNotFoundException(sprintf('The workflow "%s" is not found.', $workflowId));
}
return $workflow;
}
/**
* @param Workflow $workflow
*
* @return Workflow
*
* @since 1.2.0
*/
private function configureWorkflow(Workflow $workflow)
{
if ($this->expressionLanguage !== null) {
$workflow->setExpressionLanguage($this->expressionLanguage);
}
if ($this->operationRunner !== null) {
$workflow->setOperationRunner($this->operationRunner);
}
return $workflow;
}
}