Add workflow data object and an empty step object
[MAILPOET-4136]
This commit is contained in:
126
mailpoet/lib/Automation/Engine/Workflows/Workflow.php
Normal file
126
mailpoet/lib/Automation/Engine/Workflows/Workflow.php
Normal file
@ -0,0 +1,126 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\Workflows;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class Workflow {
|
||||
public const STATUS_ACTIVE = 'active';
|
||||
public const STATUS_INACTIVE = 'inactive';
|
||||
|
||||
/** @var int */
|
||||
private $id;
|
||||
|
||||
/** @var string */
|
||||
private $name;
|
||||
|
||||
/** @var string */
|
||||
private $status = self::STATUS_INACTIVE;
|
||||
|
||||
/** @var DateTimeImmutable */
|
||||
private $createdAt;
|
||||
|
||||
/** @var DateTimeImmutable */
|
||||
private $updatedAt;
|
||||
|
||||
/** @var array<string, Step> */
|
||||
private $steps;
|
||||
|
||||
/** @param Step[] $steps */
|
||||
public function __construct(
|
||||
string $name,
|
||||
array $steps
|
||||
) {
|
||||
$this->name = $name;
|
||||
$this->steps = [];
|
||||
foreach ($steps as $step) {
|
||||
$this->steps[$step->getId()] = $step;
|
||||
}
|
||||
|
||||
$now = new DateTimeImmutable();
|
||||
$this->createdAt = $now;
|
||||
$this->updatedAt = $now;
|
||||
}
|
||||
|
||||
public function getId(): int {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getName(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getStatus(): string {
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): DateTimeImmutable {
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function getUpdatedAt(): DateTimeImmutable {
|
||||
return $this->updatedAt;
|
||||
}
|
||||
|
||||
/** @return array<string, Step> */
|
||||
public function getSteps(): array {
|
||||
return $this->steps;
|
||||
}
|
||||
|
||||
public function getStep(string $id): ?Step {
|
||||
return $this->steps[$id] ?? null;
|
||||
}
|
||||
|
||||
public function getTrigger(string $key): ?Step {
|
||||
foreach ($this->steps as $step) {
|
||||
if ($step->getType() === Step::TYPE_TRIGGER && $step->getKey() === $key) {
|
||||
return $step;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function toArray(): array {
|
||||
return [
|
||||
'name' => $this->name,
|
||||
'status' => $this->status,
|
||||
'created_at' => $this->createdAt->format(DateTimeImmutable::W3C),
|
||||
'updated_at' => $this->updatedAt->format(DateTimeImmutable::W3C),
|
||||
'steps' => json_encode(array_map(function (Step $step) {
|
||||
return $step->toArray();
|
||||
}, $this->steps)),
|
||||
'trigger_keys' => json_encode(
|
||||
array_reduce($this->steps, function (array $triggerKeys, Step $step): array {
|
||||
if ($step->getType() === Step::TYPE_TRIGGER) {
|
||||
$triggerKeys[] = $step->getKey();
|
||||
}
|
||||
return $triggerKeys;
|
||||
}, [])
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
public static function fromArray(array $data): self {
|
||||
// TODO: validation
|
||||
$workflow = new self($data['name'], self::parseSteps(json_decode($data['steps'], true)));
|
||||
$workflow->id = (int)$data['id'];
|
||||
$workflow->status = $data['status'];
|
||||
$workflow->createdAt = $data['created_at'];
|
||||
$workflow->updatedAt = $data['updated_at'];
|
||||
return $workflow;
|
||||
}
|
||||
|
||||
private static function parseSteps(array $data): array {
|
||||
$steps = [];
|
||||
foreach ($data as $step) {
|
||||
$steps[] = new Step(
|
||||
$step['id'],
|
||||
$step['type'],
|
||||
$step['key'],
|
||||
$step['next_step_id'] ?? null,
|
||||
$step['args'] ?? []
|
||||
);
|
||||
}
|
||||
return $steps;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user