Move workflow list response building to workflow mapper
[MAILPOET-4540]
This commit is contained in:
@@ -2,38 +2,32 @@
|
|||||||
|
|
||||||
namespace MailPoet\Automation\Engine\Endpoints\Workflows;
|
namespace MailPoet\Automation\Engine\Endpoints\Workflows;
|
||||||
|
|
||||||
use DateTimeImmutable;
|
|
||||||
use MailPoet\API\REST\Request;
|
use MailPoet\API\REST\Request;
|
||||||
use MailPoet\API\REST\Response;
|
use MailPoet\API\REST\Response;
|
||||||
use MailPoet\Automation\Engine\API\Endpoint;
|
use MailPoet\Automation\Engine\API\Endpoint;
|
||||||
use MailPoet\Automation\Engine\Data\Workflow;
|
use MailPoet\Automation\Engine\Mappers\WorkflowMapper;
|
||||||
use MailPoet\Automation\Engine\Data\WorkflowStatistics;
|
|
||||||
use MailPoet\Automation\Engine\Storage\WorkflowStatisticsStorage;
|
|
||||||
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
|
||||||
use MailPoet\Validator\Builder;
|
use MailPoet\Validator\Builder;
|
||||||
|
|
||||||
class WorkflowsGetEndpoint extends Endpoint {
|
class WorkflowsGetEndpoint extends Endpoint {
|
||||||
|
/** @var WorkflowMapper */
|
||||||
|
private $workflowMapper;
|
||||||
|
|
||||||
/** @var WorkflowStorage */
|
/** @var WorkflowStorage */
|
||||||
private $workflowStorage;
|
private $workflowStorage;
|
||||||
|
|
||||||
/** @var WorkflowStatisticsStorage */
|
|
||||||
private $statisticsStorage;
|
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
WorkflowStorage $workflowStorage,
|
WorkflowMapper $workflowMapper,
|
||||||
WorkflowStatisticsStorage $statisticsStorage
|
WorkflowStorage $workflowStorage
|
||||||
) {
|
) {
|
||||||
|
$this->workflowMapper = $workflowMapper;
|
||||||
$this->workflowStorage = $workflowStorage;
|
$this->workflowStorage = $workflowStorage;
|
||||||
$this->statisticsStorage = $statisticsStorage;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(Request $request): Response {
|
public function handle(Request $request): Response {
|
||||||
$status = $request->getParam('status') ? (array)$request->getParam('status') : null;
|
$status = $request->getParam('status') ? (array)$request->getParam('status') : null;
|
||||||
$workflows = $this->workflowStorage->getWorkflows($status);
|
$workflows = $this->workflowStorage->getWorkflows($status);
|
||||||
$statistics = $this->statisticsStorage->getWorkflowStatisticsForWorkflows(...$workflows);
|
return new Response($this->workflowMapper->buildWorkflowList($workflows));
|
||||||
return new Response(array_map(function (Workflow $workflow) use ($statistics) {
|
|
||||||
return $this->buildWorkflow($workflow, $statistics[$workflow->getId()]);
|
|
||||||
}, $workflows));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function getRequestSchema(): array {
|
public static function getRequestSchema(): array {
|
||||||
@@ -41,20 +35,4 @@ class WorkflowsGetEndpoint extends Endpoint {
|
|||||||
'status' => Builder::array(Builder::string()),
|
'status' => Builder::array(Builder::string()),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildWorkflow(Workflow $workflow, WorkflowStatistics $statistics): array {
|
|
||||||
return [
|
|
||||||
'id' => $workflow->getId(),
|
|
||||||
'name' => $workflow->getName(),
|
|
||||||
'status' => $workflow->getStatus(),
|
|
||||||
'created_at' => $workflow->getCreatedAt()->format(DateTimeImmutable::W3C),
|
|
||||||
'updated_at' => $workflow->getUpdatedAt()->format(DateTimeImmutable::W3C),
|
|
||||||
'stats' => $statistics->toArray(),
|
|
||||||
'activated_at' => $workflow->getActivatedAt() ? $workflow->getActivatedAt()->format(DateTimeImmutable::W3C) : null,
|
|
||||||
'author' => [
|
|
||||||
'id' => $workflow->getAuthor()->ID,
|
|
||||||
'name' => $workflow->getAuthor()->display_name,
|
|
||||||
],
|
|
||||||
];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@@ -6,6 +6,7 @@ use DateTimeImmutable;
|
|||||||
use MailPoet\Automation\Engine\Data\NextStep;
|
use MailPoet\Automation\Engine\Data\NextStep;
|
||||||
use MailPoet\Automation\Engine\Data\Step;
|
use MailPoet\Automation\Engine\Data\Step;
|
||||||
use MailPoet\Automation\Engine\Data\Workflow;
|
use MailPoet\Automation\Engine\Data\Workflow;
|
||||||
|
use MailPoet\Automation\Engine\Data\WorkflowStatistics;
|
||||||
use MailPoet\Automation\Engine\Storage\WorkflowStatisticsStorage;
|
use MailPoet\Automation\Engine\Storage\WorkflowStatisticsStorage;
|
||||||
|
|
||||||
class WorkflowMapper {
|
class WorkflowMapper {
|
||||||
@@ -44,4 +45,28 @@ class WorkflowMapper {
|
|||||||
}, $workflow->getSteps()),
|
}, $workflow->getSteps()),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @param Workflow[] $workflows */
|
||||||
|
public function buildWorkflowList(array $workflows): array {
|
||||||
|
$statistics = $this->statisticsStorage->getWorkflowStatisticsForWorkflows(...$workflows);
|
||||||
|
return array_map(function (Workflow $workflow) use ($statistics) {
|
||||||
|
return $this->buildWorkflowListItem($workflow, $statistics[$workflow->getId()]);
|
||||||
|
}, $workflows);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function buildWorkflowListItem(Workflow $workflow, WorkflowStatistics $statistics): array {
|
||||||
|
return [
|
||||||
|
'id' => $workflow->getId(),
|
||||||
|
'name' => $workflow->getName(),
|
||||||
|
'status' => $workflow->getStatus(),
|
||||||
|
'created_at' => $workflow->getCreatedAt()->format(DateTimeImmutable::W3C),
|
||||||
|
'updated_at' => $workflow->getUpdatedAt()->format(DateTimeImmutable::W3C),
|
||||||
|
'stats' => $statistics->toArray(),
|
||||||
|
'activated_at' => $workflow->getActivatedAt() ? $workflow->getActivatedAt()->format(DateTimeImmutable::W3C) : null,
|
||||||
|
'author' => [
|
||||||
|
'id' => $workflow->getAuthor()->ID,
|
||||||
|
'name' => $workflow->getAuthor()->display_name,
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user