Introduce Workflow::equals() and move decision to update a record to storage
[MAILPOET-4430]
This commit is contained in:
@ -38,21 +38,16 @@ class UpdateWorkflowController {
|
||||
throw Exceptions::workflowNotFound($id);
|
||||
}
|
||||
|
||||
$changed = false;
|
||||
$storedData = $workflow->toArray();
|
||||
|
||||
if (array_key_exists('name', $data) && $workflow->getName() !== $data['name']) {
|
||||
if (array_key_exists('name', $data)) {
|
||||
$workflow->setName($data['name']);
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if (array_key_exists('status', $data) && $workflow->getStatus() !== $data['status']) {
|
||||
if (array_key_exists('status', $data)) {
|
||||
$this->checkWorkflowStatus($data['status']);
|
||||
$workflow->setStatus($data['status']);
|
||||
$changed = true;
|
||||
}
|
||||
|
||||
if (array_key_exists('steps', $data) && json_decode($storedData['steps'], true) !== $data['steps']) {
|
||||
if (array_key_exists('steps', $data)) {
|
||||
$this->validateWorkflowSteps($workflow, $data['steps']);
|
||||
$this->updateStepsController->updateSteps($workflow, $data['steps']);
|
||||
foreach ($workflow->getSteps() as $step) {
|
||||
|
@ -111,6 +111,20 @@ class Workflow {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function equals(Workflow $compare): bool {
|
||||
$compareArray = $compare->toArray();
|
||||
$currentArray = $this->toArray();
|
||||
$ignoreValues = [
|
||||
'created_at',
|
||||
'updated_at',
|
||||
];
|
||||
foreach ($ignoreValues as $ignore) {
|
||||
unset($compareArray[$ignore]);
|
||||
unset($currentArray[$ignore]);
|
||||
}
|
||||
return $compareArray === $currentArray;
|
||||
}
|
||||
|
||||
public function toArray(): array {
|
||||
return [
|
||||
'name' => $this->name,
|
||||
|
@ -36,6 +36,10 @@ class WorkflowStorage {
|
||||
}
|
||||
|
||||
public function updateWorkflow(Workflow $workflow): void {
|
||||
$oldRecord = $this->getWorkflow($workflow->getId());
|
||||
if ($oldRecord && $oldRecord->equals($workflow)) {
|
||||
return;
|
||||
}
|
||||
$result = $this->wpdb->update($this->workflowTable, $this->workflowHeaderData($workflow), ['id' => $workflow->getId()]);
|
||||
if ($result === false) {
|
||||
throw Exceptions::databaseError($this->wpdb->last_error);
|
||||
|
Reference in New Issue
Block a user