public function createTransitionItems($status, $WithScenarioNames, $withEventSequence)
{
$start = $this->getWorkflowStatus();
$end = $status;
$scenario = [];
$events = ['before' => [], 'after' => []];
$defaultEventCfg = $newStatus = null;
$emptyStart = empty($start);
$emptyEnd = empty($end);
if ($emptyStart && !$emptyEnd) {
// (potential) entering workflow -----------------------------------
$end = $this->ensureStatusInstance($end, true);
$workflow = $this->_wfSource->getWorkflow($end->getWorkflowId());
$initialStatusId = $workflow->getInitialStatusId();
if ($end->getId() !== $initialStatusId) {
throw new WorkflowException('Not an initial status : ' . $end->getId() . ' ("' . $initialStatusId . '" expected)');
}
if ($WithScenarioNames) {
$scenario = [WorkflowScenario::enterWorkflow($end->getWorkflowId()), WorkflowScenario::enterStatus($end->getId())];
}
if ($withEventSequence && $this->_eventSequence !== null) {
$events = $this->_eventSequence->createEnterWorkflowSequence($end, $this);
}
if ($this->fireDefaultEvent) {
$defaultEventCfg = ['end' => $end, 'sender' => $this];
}
$newStatus = $end;
} elseif (!$emptyStart && $emptyEnd) {
// leaving workflow -------------------------------------------------
if ($WithScenarioNames) {
$scenario = [WorkflowScenario::leaveWorkflow($start->getWorkflowId()), WorkflowScenario::leaveStatus($start->getId())];
}
if ($withEventSequence && $this->_eventSequence !== null) {
$events = $this->_eventSequence->createLeaveWorkflowSequence($start, $this);
}
if ($this->fireDefaultEvent) {
$defaultEventCfg = ['start' => $start, 'sender' => $this];
}
$newStatus = $end;
} elseif (!$emptyStart && !$emptyEnd) {
// change status ---------------------------------------
$end = $this->ensureStatusInstance($end, true);
$transition = $this->_wfSource->getTransition($start->getId(), $end->getId(), $this->selectDefaultWorkflowId());
// TODO : replace $this->owner with defaultWorkflowId
if ($transition === null && $start->getId() != $end->getId()) {
throw new WorkflowException('No transition found between status ' . $start->getId() . ' and ' . $end->getId());
}
if ($transition != null) {
if ($WithScenarioNames) {
$scenario = [WorkflowScenario::leaveStatus($start->getId()), WorkflowScenario::changeStatus($start->getId(), $end->getId()), WorkflowScenario::enterStatus($end->getId())];
}
if ($withEventSequence && $this->_eventSequence !== null) {
$events = $this->_eventSequence->createChangeStatusSequence($transition, $this);
}
if ($this->fireDefaultEvent) {
$defaultEventCfg = ['start' => $transition->getStartStatus(), 'end' => $transition->getEndStatus(), 'transition' => $transition, 'sender' => $this];
}
}
$newStatus = $end;
}
if (count($events) != 0 && (!isset($events['before']) || !isset($events['after']))) {
throw new WorkflowException('Invalid event sequence format : "before" and "after" keys are mandatory');
}
if ($this->fireDefaultEvent && $defaultEventCfg != null) {
array_unshift($events['before'], new WorkflowEvent(self::EVENT_BEFORE_CHANGE_STATUS, $defaultEventCfg));
array_unshift($events['after'], new WorkflowEvent(self::EVENT_AFTER_CHANGE_STATUS, $defaultEventCfg));
}
return [$newStatus, $scenario, $events];
}