Extract workflow duplication logic to a controller, fix some issues

[MAILPOET-4540]
This commit is contained in:
Jan Jakes
2022-10-13 14:37:44 +02:00
committed by David Remer
parent 6158f5a64b
commit 89c43a5cb9
6 changed files with 115 additions and 45 deletions

View File

@ -37,28 +37,6 @@ class WorkflowStorage {
return $id;
}
public function duplicateWorkflow(Workflow $workflow): int {
$data = $workflow->toArray();
$now = (new DateTimeImmutable())->format(DateTimeImmutable::W3C);
$data['created_at'] = $now;
$data['updated_at'] = $now;
$data['status'] = Workflow::STATUS_DRAFT;
$prefix = 'Copy of ';
$newName = $prefix . $workflow->getName();
$nameColumnLengthInfo = $this->wpdb->get_col_length($this->workflowTable, 'name');
$maxLength = is_array($nameColumnLengthInfo)
? $nameColumnLengthInfo['length'] ?? 255
: 255;
if (strlen($newName) > $maxLength) {
$truncateWith = '…';
$truncationLength = strlen($truncateWith);
$newName = substr($newName, 0, $maxLength - $truncationLength) . $truncateWith;
}
$data['name'] = $newName;
$duplicate = Workflow::fromArray($data);
return $this->createWorkflow($duplicate);
}
public function updateWorkflow(Workflow $workflow): void {
$oldRecord = $this->getWorkflow($workflow->getId());
if ($oldRecord && $oldRecord->equals($workflow)) {
@ -200,6 +178,13 @@ class WorkflowStorage {
$this->wpdb->query("truncate $versionTable;") === true;
}
public function getNameColumnLength(): int {
$nameColumnLengthInfo = $this->wpdb->get_col_length($this->workflowTable, 'name');
return is_array($nameColumnLengthInfo)
? $nameColumnLengthInfo['length'] ?? 255
: 255;
}
private function getWorkflowHeaderData(Workflow $workflow): array {
$workflowHeader = $workflow->toArray();
unset($workflowHeader['steps']);