Skip to content

Commit 5cfdf68

Browse files
committed
Update repository and database loader
1 parent 012b142 commit 5cfdf68

6 files changed

+123
-14
lines changed

src/DatabaseLoader.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Soap\WorkflowLoader;
44

55
use Soap\WorkflowLoader\Contracts\WorkflowDatabaseLoader;
6+
use Soap\WorkflowLoader\Repositories\WorkflowRepository;
67

78
class DatabaseLoader implements WorkflowDatabaseLoader
89
{
@@ -53,13 +54,15 @@ public function getWorkflowStateTransitionTableName(): string
5354

5455
public function load(string $workflowName): array
5556
{
56-
$workflowConfig = [];
57+
$repo = app()->make(WorkflowRepository::class);
5758

58-
return $workflowConfig;
59+
return $repo->findByName($workflowName);
5960
}
6061

6162
public function all(): array
6263
{
63-
return [];
64+
$repo = app()->make(WorkflowRepository::class);
65+
66+
return $repo->all();
6467
}
6568
}

src/Repositories/WorkflowRepository.php

+29-10
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,41 @@ public function __construct(Workflow $model)
1313
$this->model = $model;
1414
}
1515

16+
public function all()
17+
{
18+
$workflows = $this->model->with(['transitions', 'states'])->get();
19+
20+
return $workflows->map(function ($workflow) {
21+
return $this->makeWorkflowCofig($workflow);
22+
})->toArray();
23+
}
24+
1625
public function find($id)
1726
{
1827
$workflow = $this->model->with(['transitions', 'states'])->find($id);
28+
if (! $workflow) {
29+
return [];
30+
}
31+
32+
return $this->makeWorkflowCofig($workflow);
33+
}
34+
35+
public function findByName(string $name): array
36+
{
37+
$workflowId = $this->model->where('name', $name)->first()->id;
38+
if (! $workflowId) {
39+
return [];
40+
}
41+
42+
return $this->find($workflowId);
43+
}
44+
45+
protected function makeWorkflowCofig($workflow): array
46+
{
1947
$places = $workflow->states->map(function ($state) {
2048
return $state->name;
2149
})->toArray();
50+
2251
$transitions = $workflow->transitions->map(function ($transition) {
2352
return [
2453
$transition->name => [
@@ -38,14 +67,4 @@ public function find($id)
3867
],
3968
];
4069
}
41-
42-
public function findByName(string $name): array
43-
{
44-
$workflowId = $this->model->where('name', $name)->first()->id;
45-
if (! $workflowId) {
46-
return [];
47-
}
48-
49-
return $this->find($workflowId);
50-
}
5170
}

tests/Feature/WorkflowStorageTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Soap\WorkflowLoader\Repositories\WorkflowRepository;
66

77
beforeEach(function () {
8-
98
$workflow = Workflow::create([
109
'name' => 'test_workflow',
1110
'type' => 'workflow',

tests/Unit/DatabaseLoaderTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

tests/Unit/DatabaseStorageTest.php

Whitespace-only changes.

tests/Unit/WorkflowRepositoryTest.php

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
beforeEach(function () {
4+
$workflow = Workflow::create([
5+
'name' => 'test_workflow',
6+
'type' => 'workflow',
7+
'description' => 'Test Workflow Description',
8+
'supports' => ['App\Models\Article'],
9+
'metadata' => [],
10+
]);
11+
12+
$draftState = $workflow->states()->create([
13+
'name' => 'draft',
14+
'metadata' => [],
15+
]);
16+
17+
$onReviewState = $workflow->states()->create([
18+
'name' => 'on review',
19+
'metadata' => [],
20+
]);
21+
22+
$approvedState = $workflow->states()->create([
23+
'name' => 'approved',
24+
'metadata' => [],
25+
]);
26+
27+
$rejectedState = $workflow->states()->create([
28+
'name' => 'rejected',
29+
'metadata' => [],
30+
]);
31+
32+
$onReviewTransition = $workflow->transitions()->create([
33+
'name' => 'submit',
34+
'to_state_id' => $onReviewState->id,
35+
'metadata' => [],
36+
]);
37+
38+
$onReviewTransition->fromStates()->create([
39+
'from_state_id' => $draftState->id,
40+
]);
41+
42+
$orderWorkflow = Workflow::create([
43+
'name' => 'order_process',
44+
'type' => 'workflow',
45+
'description' => 'Test Workflow Description',
46+
'supports' => ['App\Models\Order'],
47+
'metadata' => [],
48+
]);
49+
50+
$pendingState = $orderWorkflow->states()->create([
51+
'name' => 'pending',
52+
'metadata' => [],
53+
]);
54+
55+
$approvedState = $orderWorkflow->states()->create([
56+
'name' => 'approved',
57+
'metadata' => [],
58+
]);
59+
60+
$cancelledState = $orderWorkflow->states()->create([
61+
'name' => 'cancelled',
62+
'metadata' => [],
63+
]);
64+
65+
$packeddState = $orderWorkflow->states()->create([
66+
'name' => 'packed',
67+
'metadata' => [],
68+
]);
69+
70+
$shippedState = $orderWorkflow->states()->create([
71+
'name' => 'shipped',
72+
'metadata' => [],
73+
]);
74+
75+
$orderWorkflow->transitions()->create([
76+
'name' => 'approve',
77+
'to_state_id' => $approvedState->id,
78+
'metadata' => [],
79+
]);
80+
81+
$orderWorkflow->transitions()->create([
82+
'name' => 'cancel',
83+
'to_state_id' => $cancelledState->id,
84+
'metadata' => [],
85+
]);
86+
87+
});

0 commit comments

Comments
 (0)