Skip to content

Commit 2b6dabb

Browse files
author
Tobias Wojtylak
committed
Removed StepDirectionAwareInterface & use StepAwareInterface: Simplified DefaultStepController
1 parent c565102 commit 2b6dabb

4 files changed

Lines changed: 81 additions & 112 deletions

File tree

src/Controller/DefaultStepController.php

Lines changed: 40 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,30 @@
99

1010
namespace solutionDrive\MultiStepBundle\Controller;
1111

12+
use solutionDrive\MultiStepBundle\Context\FlowContext;
13+
use solutionDrive\MultiStepBundle\Context\FlowContextInterface;
1214
use solutionDrive\MultiStepBundle\Exception\NoNextOrPreviousStepException;
1315
use solutionDrive\MultiStepBundle\Model\MultiStepFlowInterface;
1416
use solutionDrive\MultiStepBundle\Model\MultiStepInterface;
17+
use solutionDrive\MultiStepBundle\Router\MultistepRouterInterface;
1518
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1619
use Symfony\Component\HttpFoundation\RedirectResponse;
1720
use Symfony\Component\HttpFoundation\Request;
1821
use 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
}

src/Controller/MultiStepController.php

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99

1010
namespace solutionDrive\MultiStepBundle\Controller;
1111

12+
use solutionDrive\MultiStepBundle\Factory\FlowContextFactoryInterface;
13+
use solutionDrive\MultiStepBundle\Factory\MultistepRouterFactoryInterface;
1214
use solutionDrive\MultiStepBundle\Registry\MultiStepFlowRegistryInterface;
1315
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
1416
use Symfony\Component\HttpFoundation\RedirectResponse;
1517
use Symfony\Component\HttpFoundation\Request;
1618
use Symfony\Component\HttpFoundation\Response;
1719
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
1820
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
19-
use Symfony\Component\Routing\RouterInterface;
2021

2122
class MultiStepController extends Controller
2223
{
@@ -28,27 +29,40 @@ class MultiStepController extends Controller
2829

2930
/** @var ArgumentResolverInterface */
3031
private $argumentResolver;
32+
/**
33+
* @var FlowContextFactoryInterface
34+
*/
35+
private $flowContextFactory;
36+
/**
37+
* @var MultistepRouterFactoryInterface
38+
*/
39+
private $stepRouterFactory;
3140

3241
public function __construct(
3342
MultiStepFlowRegistryInterface $flowRegistry,
3443
ControllerResolverInterface $controllerResolver,
35-
ArgumentResolverInterface $argumentResolver
44+
ArgumentResolverInterface $argumentResolver,
45+
FlowContextFactoryInterface $flowContextFactory,
46+
MultistepRouterFactoryInterface $stepRouterFactory
3647
) {
3748
$this->flowRegistry = $flowRegistry;
3849
$this->controllerResolver = $controllerResolver;
3950
$this->argumentResolver = $argumentResolver;
51+
$this->flowContextFactory = $flowContextFactory;
52+
$this->stepRouterFactory = $stepRouterFactory;
4053
}
4154

4255
public function stepAction(Request $request, string $flow_slug, string $step_slug): Response
4356
{
4457
$flow = $this->flowRegistry->getFlowBySlug($flow_slug);
45-
$step = $flow->getStepBySlug($step_slug);
58+
$flowContext = $this->flowContextFactory->create($flow, $step_slug);
59+
$router = $this->stepRouterFactory->create($request, $flow_slug);
60+
61+
$step = $flowContext->getCurrentStep();
4662
$configuredController = $step->getControllerAction();
4763

4864
$request->attributes->set('_controller', $configuredController);
4965

50-
$currentRoute = $request->get('_route');
51-
5266
$callableController = $this->controllerResolver->getController($request);
5367
$arguments = $this->argumentResolver->getArguments($request, $callableController);
5468

@@ -59,40 +73,17 @@ public function stepAction(Request $request, string $flow_slug, string $step_slu
5973
$controller->setTemplate($step->getTemplate());
6074
}
6175

62-
// set previous and next steps
63-
if ($controller instanceof StepDirectionAwareInterface) {
64-
/** @var RouterInterface $router */
65-
$router = $this->get('router');
66-
$nextStep = $flow->getStepAfter($step);
67-
$previousStep = $flow->getStepBefore($step);
68-
69-
if (null !== $nextStep) {
70-
$nextStepLink = $router->generate(
71-
$currentRoute,
72-
['flow_slug' => $flow_slug, 'step_slug' => $nextStep->getSlug()]
73-
);
74-
$controller->setNextStep($nextStep);
75-
$controller->setNextStepLink($nextStepLink);
76-
}
77-
if (null !== $previousStep) {
78-
$previousStepLink = $router->generate(
79-
$currentRoute,
80-
['flow_slug' => $flow_slug, 'step_slug' => $previousStep->getSlug()]
81-
);
82-
$controller->setPreviousStep($previousStep);
83-
$controller->setPreviousStepLink($previousStepLink);
84-
}
76+
// set flow context
77+
if ($controller instanceof StepAwareInterface) {
78+
$controller->setFlowContext($flowContext);
79+
$controller->setRouter($router);
8580
}
8681

8782
// set flow
8883
if ($controller instanceof FlowAwareInterface) {
8984
$controller->setFlow($flow);
9085
}
9186

92-
// set step
93-
if ($controller instanceof StepAwareInterface) {
94-
$controller->setStep($step);
95-
}
9687

9788
return call_user_func_array($callableController, $arguments);
9889
}

src/Controller/StepAwareInterface.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,27 @@
99

1010
namespace solutionDrive\MultiStepBundle\Controller;
1111

12+
use solutionDrive\MultiStepBundle\Context\FlowContextInterface;
1213
use solutionDrive\MultiStepBundle\Model\MultiStepInterface;
14+
use solutionDrive\MultiStepBundle\Router\MultistepRouterInterface;
1315

1416
interface StepAwareInterface
1517
{
16-
public function getStep(): ?MultiStepInterface;
18+
public function setFlowContext(FlowContextInterface $flowContext): void;
19+
20+
public function getFlowContext(): FlowContextInterface;
21+
22+
public function setRouter(MultistepRouterInterface $router): void;
23+
24+
public function getRouter(): MultistepRouterInterface;
25+
26+
public function getNextStep(): ?MultiStepInterface;
1727

18-
public function setStep(?MultiStepInterface $step): void;
28+
public function getNextStepLink(): ?string;
29+
30+
public function getPreviousStep(): ?MultiStepInterface;
31+
32+
public function getPreviousStepLink(): ?string;
33+
34+
public function getStep(): ?MultiStepInterface;
1935
}

src/Controller/StepDirectionAwareInterface.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)