99
1010namespace solutionDrive \MultiStepBundle \Controller ;
1111
12+ use solutionDrive \MultiStepBundle \Context \FlowContext ;
13+ use solutionDrive \MultiStepBundle \Context \FlowContextInterface ;
1214use solutionDrive \MultiStepBundle \Exception \NoNextOrPreviousStepException ;
1315use solutionDrive \MultiStepBundle \Model \MultiStepFlowInterface ;
1416use solutionDrive \MultiStepBundle \Model \MultiStepInterface ;
17+ use solutionDrive \MultiStepBundle \Router \MultistepRouterInterface ;
1518use Symfony \Bundle \FrameworkBundle \Controller \Controller ;
1619use Symfony \Component \HttpFoundation \RedirectResponse ;
1720use Symfony \Component \HttpFoundation \Request ;
1821use Symfony \Component \HttpFoundation \Response ;
1922
20- class DefaultStepController extends Controller implements TemplateAwareControllerInterface, StepDirectionAwareInterface , FlowAwareInterface, StepAwareInterface
23+ class DefaultStepController extends Controller implements TemplateAwareControllerInterface, StepAwareInterface , FlowAwareInterface
2124{
2225 /** @var string */
2326 private $ template = 'MultiStepBundle::default_step.html.twig ' ;
2427
2528 /** @var MultiStepFlowInterface */
2629 private $ flow ;
2730
28- /** @var MultiStepInterface */
29- private $ step ;
31+ /** @var FlowContext */
32+ private $ flowContext ;
3033
31- /** @var MultiStepInterface|null */
32- private $ previousStep ;
33-
34- /** @var MultiStepInterface|null */
35- private $ nextStep ;
36-
37- /** @var string|null */
38- private $ previousStepLink ;
39-
40- /** @var string|null */
41- private $ nextStepLink ;
34+ /** @var MultistepRouterInterface */
35+ private $ router ;
4236
4337 public function renderAction (Request $ request ): Response
4438 {
@@ -77,10 +71,7 @@ public function getTemplateVariables(): array
7771 */
7872 public function redirectToNextStep (int $ statusCode = 302 ): RedirectResponse
7973 {
80- if (null === $ this ->nextStepLink ) {
81- throw new NoNextOrPreviousStepException ();
82- }
83- return $ this ->redirect ($ this ->nextStepLink , $ statusCode );
74+ return $ this ->redirectToStep ($ this ->getNextStepLink (), $ statusCode );
8475 }
8576
8677 /**
@@ -89,26 +80,32 @@ public function redirectToNextStep(int $statusCode = 302): RedirectResponse
8980 */
9081 public function redirectToPreviousStep (int $ statusCode = 302 ): RedirectResponse
9182 {
92- if (null === $ this ->previousStepLink ) {
83+ return $ this ->redirectToStep ($ this ->getPreviousStep (), $ statusCode );
84+ }
85+
86+
87+ protected function redirectToStep (MultiStepInterface $ step , int $ statusCode )
88+ {
89+ if (null === $ step ) {
9390 throw new NoNextOrPreviousStepException ();
9491 }
95- return $ this ->redirect ($ this ->previousStepLink , $ statusCode );
92+ return $ this ->redirect ($ this ->router -> generateStepLink ( $ step ) , $ statusCode );
9693 }
9794
9895 /**
9996 * Returns true if there is a next step in the flow.
10097 */
10198 public function hasNextStep (): bool
10299 {
103- return null !== $ this ->nextStep ;
100+ return $ this ->flowContext -> hasNextStep () ;
104101 }
105102
106103 /**
107104 * Returns true if there is a previous step in the flow.
108105 */
109106 public function hasPreviousStep (): bool
110107 {
111- return null !== $ this ->previousStep ;
108+ return $ this ->flowContext -> hasPreviousStep () ;
112109 }
113110
114111 /**
@@ -130,61 +127,57 @@ public function setTemplate(string $template): void
130127
131128 public function getNextStep (): ?MultiStepInterface
132129 {
133- return $ this ->nextStep ;
134- }
135-
136- public function setNextStep (?MultiStepInterface $ step ): void
137- {
138- $ this ->nextStep = $ step ;
130+ return $ this ->flowContext ->getNextStep ();
139131 }
140132
141133 public function getPreviousStep (): ?MultiStepInterface
142134 {
143- return $ this ->previousStep ;
135+ return $ this ->flowContext -> getPreviousStep () ;
144136 }
145137
146- public function setPreviousStep (? MultiStepInterface $ step ): void
138+ public function getNextStepLink ( ): ? string
147139 {
148- $ this ->previousStep = $ step ;
140+ return $ this ->hasNextStep () ? $ this -> router -> generateStepLink ( $ this -> getNextStep ()) : null ;
149141 }
150142
151- public function getNextStepLink (): ?string
143+
144+ public function getPreviousStepLink (): ?string
152145 {
153- return $ this ->nextStepLink ;
146+ return $ this ->hasPreviousStep () ? $ this -> router -> generateStepLink ( $ this -> getPreviousStep ()) : null ;
154147 }
155148
156- public function setNextStepLink (? string $ link ): void
149+ public function getFlow ( ): ? MultiStepFlowInterface
157150 {
158- $ this ->nextStepLink = $ link ;
151+ return $ this ->flow ;
159152 }
160153
161- public function getPreviousStepLink ( ): ? string
154+ public function setFlow (? MultiStepFlowInterface $ flow ): void
162155 {
163- return $ this ->previousStepLink ;
156+ $ this ->flow = $ flow ;
164157 }
165158
166- public function setPreviousStepLink (? string $ link ): void
159+ public function getStep ( ): ? MultiStepInterface
167160 {
168- $ this ->previousStepLink = $ link ;
161+ return $ this ->flowContext -> getCurrentStep () ;
169162 }
170163
171- public function getFlow (): ? MultiStepFlowInterface
164+ public function getFlowContext (): FlowContextInterface
172165 {
173- return $ this ->flow ;
166+ return $ this ->flowContext ;
174167 }
175168
176- public function setFlow (? MultiStepFlowInterface $ flow ): void
169+ public function setFlowContext ( FlowContextInterface $ flowContext ): void
177170 {
178- $ this ->flow = $ flow ;
171+ $ this ->flowContext = $ flowContext ;
179172 }
180173
181- public function getStep ( ): ? MultiStepInterface
174+ public function setRouter ( MultistepRouterInterface $ router ): void
182175 {
183- return $ this ->step ;
176+ $ this ->router = $ router ;
184177 }
185178
186- public function setStep (? MultiStepInterface $ step ): void
179+ public function getRouter ( ): MultistepRouterInterface
187180 {
188- $ this ->step = $ step ;
181+ return $ this ->router ;
189182 }
190183}
0 commit comments