Move workflow list response building to workflow mapper

[MAILPOET-4540]
This commit is contained in:
Jan Jakes
2022-10-14 09:36:17 +02:00
committed by David Remer
parent dd033d2f78
commit 1b20bf45dd
2 changed files with 33 additions and 30 deletions

View File

@@ -2,38 +2,32 @@
namespace MailPoet\Automation\Engine\Endpoints\Workflows;
use DateTimeImmutable;
use MailPoet\API\REST\Request;
use MailPoet\API\REST\Response;
use MailPoet\Automation\Engine\API\Endpoint;
use MailPoet\Automation\Engine\Data\Workflow;
use MailPoet\Automation\Engine\Data\WorkflowStatistics;
use MailPoet\Automation\Engine\Storage\WorkflowStatisticsStorage;
use MailPoet\Automation\Engine\Mappers\WorkflowMapper;
use MailPoet\Automation\Engine\Storage\WorkflowStorage;
use MailPoet\Validator\Builder;
class WorkflowsGetEndpoint extends Endpoint {
/** @var WorkflowMapper */
private $workflowMapper;
/** @var WorkflowStorage */
private $workflowStorage;
/** @var WorkflowStatisticsStorage */
private $statisticsStorage;
public function __construct(
WorkflowStorage $workflowStorage,
WorkflowStatisticsStorage $statisticsStorage
WorkflowMapper $workflowMapper,
WorkflowStorage $workflowStorage
) {
$this->workflowMapper = $workflowMapper;
$this->workflowStorage = $workflowStorage;
$this->statisticsStorage = $statisticsStorage;
}
public function handle(Request $request): Response {
$status = $request->getParam('status') ? (array)$request->getParam('status') : null;
$workflows = $this->workflowStorage->getWorkflows($status);
$statistics = $this->statisticsStorage->getWorkflowStatisticsForWorkflows(...$workflows);
return new Response(array_map(function (Workflow $workflow) use ($statistics) {
return $this->buildWorkflow($workflow, $statistics[$workflow->getId()]);
}, $workflows));
return new Response($this->workflowMapper->buildWorkflowList($workflows));
}
public static function getRequestSchema(): array {
@@ -41,20 +35,4 @@ class WorkflowsGetEndpoint extends Endpoint {
'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,
],
];
}
}