66
77/**
88 * Fluent workflow builder for simplified workflow creation
9- *
9+ *
1010 * @example
1111 * $workflow = WorkflowBuilder::create('user-onboarding')
1212 * ->description('Complete user onboarding process')
2020class WorkflowBuilder
2121{
2222 private string $ name ;
23+
2324 private string $ version = '1.0 ' ;
25+
2426 private string $ description = '' ;
27+
2528 private array $ steps = [];
29+
2630 private array $ transitions = [];
31+
2732 private array $ metadata = [];
33+
2834 private array $ conditionalSteps = [];
2935
3036 private function __construct (string $ name )
@@ -46,6 +52,7 @@ public static function create(string $name): self
4652 public function description (string $ description ): self
4753 {
4854 $ this ->description = $ description ;
55+
4956 return $ this ;
5057 }
5158
@@ -55,6 +62,7 @@ public function description(string $description): self
5562 public function version (string $ version ): self
5663 {
5764 $ this ->version = $ version ;
65+
5866 return $ this ;
5967 }
6068
@@ -75,6 +83,7 @@ public function addStep(
7583 'timeout ' => $ timeout ,
7684 'retry_attempts ' => $ retryAttempts ,
7785 ];
86+
7887 return $ this ;
7988 }
8089
@@ -87,7 +96,8 @@ public function startWith(
8796 ?int $ timeout = null ,
8897 int $ retryAttempts = 0
8998 ): self {
90- $ stepId = 'step_ ' . (count ($ this ->steps ) + 1 );
99+ $ stepId = 'step_ ' .(count ($ this ->steps ) + 1 );
100+
91101 return $ this ->addStep ($ stepId , $ action , $ config , $ timeout , $ retryAttempts );
92102 }
93103
@@ -100,7 +110,8 @@ public function then(
100110 ?int $ timeout = null ,
101111 int $ retryAttempts = 0
102112 ): self {
103- $ stepId = 'step_ ' . (count ($ this ->steps ) + 1 );
113+ $ stepId = 'step_ ' .(count ($ this ->steps ) + 1 );
114+
104115 return $ this ->addStep ($ stepId , $ action , $ config , $ timeout , $ retryAttempts );
105116 }
106117
@@ -112,12 +123,12 @@ public function when(string $condition, callable $callback): self
112123 $ originalStepsCount = count ($ this ->steps );
113124 $ callback ($ this );
114125 $ newStepsCount = count ($ this ->steps );
115-
126+
116127 // Mark new steps as conditional
117128 for ($ i = $ originalStepsCount ; $ i < $ newStepsCount ; $ i ++) {
118129 $ this ->steps [$ i ]['condition ' ] = $ condition ;
119130 }
120-
131+
121132 return $ this ;
122133 }
123134
@@ -131,7 +142,7 @@ public function email(
131142 array $ data = []
132143 ): self {
133144 return $ this ->addStep (
134- 'email_ ' . count ($ this ->steps ),
145+ 'email_ ' . count ($ this ->steps ),
135146 'SolutionForest \\WorkflowMastery \\Actions \\EmailAction ' ,
136147 [
137148 'template ' => $ template ,
@@ -145,14 +156,14 @@ public function email(
145156 /**
146157 * Add delay step (common pattern)
147158 */
148- public function delay (int $ seconds = null , int $ minutes = null , int $ hours = null ): self
159+ public function delay (? int $ seconds = null , ? int $ minutes = null , ? int $ hours = null ): self
149160 {
150161 $ totalSeconds = $ seconds ?? 0 ;
151162 $ totalSeconds += ($ minutes ?? 0 ) * 60 ;
152163 $ totalSeconds += ($ hours ?? 0 ) * 3600 ;
153164
154165 return $ this ->addStep (
155- 'delay_ ' . count ($ this ->steps ),
166+ 'delay_ ' . count ($ this ->steps ),
156167 'SolutionForest \\WorkflowMastery \\Actions \\DelayAction ' ,
157168 ['seconds ' => $ totalSeconds ]
158169 );
@@ -168,7 +179,7 @@ public function http(
168179 array $ headers = []
169180 ): self {
170181 return $ this ->addStep (
171- 'http_ ' . count ($ this ->steps ),
182+ 'http_ ' . count ($ this ->steps ),
172183 'SolutionForest \\WorkflowMastery \\Actions \\HttpAction ' ,
173184 [
174185 'url ' => $ url ,
@@ -185,7 +196,7 @@ public function http(
185196 public function condition (string $ condition ): self
186197 {
187198 return $ this ->addStep (
188- 'condition_ ' . count ($ this ->steps ),
199+ 'condition_ ' . count ($ this ->steps ),
189200 'SolutionForest \\WorkflowMastery \\Actions \\ConditionAction ' ,
190201 ['condition ' => $ condition ]
191202 );
@@ -197,6 +208,7 @@ public function condition(string $condition): self
197208 public function withMetadata (array $ metadata ): self
198209 {
199210 $ this ->metadata = array_merge ($ this ->metadata , $ metadata );
211+
200212 return $ this ;
201213 }
202214
@@ -237,7 +249,7 @@ public function build(): WorkflowDefinition
237249 */
238250 public static function quick (): QuickWorkflowBuilder
239251 {
240- return new QuickWorkflowBuilder () ;
252+ return new QuickWorkflowBuilder ;
241253 }
242254}
243255
@@ -295,10 +307,10 @@ public function documentApproval(string $name = 'document-approval'): WorkflowBu
295307 subject: 'Document Review Request '
296308 )
297309 ->addStep ('review_document ' , 'App \\Actions \\ReviewDocumentAction ' )
298- ->when ('review.approved ' , function ($ builder ) {
310+ ->when ('review.approved ' , function ($ builder ) {
299311 $ builder ->addStep ('approve_document ' , 'App \\Actions \\ApproveDocumentAction ' );
300312 })
301- ->when ('review.rejected ' , function ($ builder ) {
313+ ->when ('review.rejected ' , function ($ builder ) {
302314 $ builder ->addStep ('reject_document ' , 'App \\Actions \\RejectDocumentAction ' );
303315 });
304316 }
0 commit comments