|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Blokctl\Action\Workflow; |
| 6 | + |
| 7 | +use Storyblok\ManagementApi\Endpoints\WorkflowApi; |
| 8 | +use Storyblok\ManagementApi\Endpoints\WorkflowStageApi; |
| 9 | +use Storyblok\ManagementApi\ManagementApiClient; |
| 10 | + |
| 11 | +final readonly class WorkflowsListAction |
| 12 | +{ |
| 13 | + public function __construct( |
| 14 | + private ManagementApiClient $client, |
| 15 | + ) {} |
| 16 | + |
| 17 | + public function execute(string $spaceId): WorkflowsListResult |
| 18 | + { |
| 19 | + $workflowApi = new WorkflowApi($this->client, $spaceId); |
| 20 | + $workflows = $workflowApi->list()->data(); |
| 21 | + |
| 22 | + $stageApi = new WorkflowStageApi($this->client, $spaceId); |
| 23 | + |
| 24 | + /** @var array<int, array{id: int, name: string, isDefault: bool, stages: array<int, array{id: int, name: string, position: int}>}> $items */ |
| 25 | + $items = []; |
| 26 | + |
| 27 | + /** @phpstan-ignore foreach.nonIterable */ |
| 28 | + foreach ($workflows as $workflow) { |
| 29 | + /** @var int $workflowId */ |
| 30 | + $workflowId = $workflow->get('id'); |
| 31 | + /** @var string $workflowName */ |
| 32 | + $workflowName = $workflow->get('name'); |
| 33 | + $isDefault = $workflow->getBoolean('is_default'); |
| 34 | + |
| 35 | + $stages = $stageApi->list((string) $workflowId)->data(); |
| 36 | + |
| 37 | + /** @var array<int, array{id: int, name: string, position: int}> $stageItems */ |
| 38 | + $stageItems = []; |
| 39 | + |
| 40 | + /** @phpstan-ignore foreach.nonIterable */ |
| 41 | + foreach ($stages as $stage) { |
| 42 | + /** @var int $stageId */ |
| 43 | + $stageId = $stage->get('id'); |
| 44 | + /** @var string $stageName */ |
| 45 | + $stageName = $stage->get('name'); |
| 46 | + /** @var int $position */ |
| 47 | + $position = $stage->get('position'); |
| 48 | + $stageItems[] = [ |
| 49 | + 'id' => $stageId, |
| 50 | + 'name' => $stageName, |
| 51 | + 'position' => $position, |
| 52 | + ]; |
| 53 | + } |
| 54 | + |
| 55 | + $items[] = [ |
| 56 | + 'id' => $workflowId, |
| 57 | + 'name' => $workflowName, |
| 58 | + 'isDefault' => $isDefault, |
| 59 | + 'stages' => $stageItems, |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + return new WorkflowsListResult(workflows: $items); |
| 64 | + } |
| 65 | +} |
0 commit comments