1 | <?php |
||||||
2 | |||||||
3 | declare(strict_types=1); |
||||||
4 | |||||||
5 | /* |
||||||
6 | * (c) Christian Gripp <[email protected]> |
||||||
7 | * |
||||||
8 | * For the full copyright and license information, please view the LICENSE |
||||||
9 | * file that was distributed with this source code. |
||||||
10 | */ |
||||||
11 | |||||||
12 | namespace Core23\Form\Handler; |
||||||
13 | |||||||
14 | use Core23\Form\Handler\Exception\InvalidCallbackException; |
||||||
15 | use Symfony\Component\Form\FormInterface; |
||||||
16 | use Symfony\Component\HttpFoundation\Request; |
||||||
17 | use Symfony\Component\HttpFoundation\Response; |
||||||
18 | |||||||
19 | abstract class AbstractFormHandler implements FormHandlerInterface |
||||||
20 | { |
||||||
21 | /** |
||||||
22 | * {@inheritdoc} |
||||||
23 | */ |
||||||
24 | final public function handle(FormInterface $form, Request $request, callable $callback): ?Response |
||||||
25 | { |
||||||
26 | if ($response = $this->preProcess($form, $request)) { |
||||||
27 | return $response; |
||||||
28 | } |
||||||
29 | |||||||
30 | $form->handleRequest($request); |
||||||
31 | |||||||
32 | if (!$form->isSubmitted() || !$form->isValid()) { |
||||||
33 | return null; |
||||||
34 | } |
||||||
35 | |||||||
36 | if ($response = $this->validate($form, $request)) { |
||||||
37 | return $response; |
||||||
38 | } |
||||||
39 | |||||||
40 | if (!$this->process($form, $request)) { |
||||||
41 | return null; |
||||||
42 | } |
||||||
43 | |||||||
44 | if (!\is_callable($callback)) { |
||||||
45 | throw new InvalidCallbackException('No valid callable.'); |
||||||
46 | } |
||||||
47 | |||||||
48 | $response = $callback(); |
||||||
49 | |||||||
50 | if (!$response instanceof Response) { |
||||||
51 | throw new InvalidCallbackException('Invalid callback response.'); |
||||||
52 | } |
||||||
53 | |||||||
54 | if ($response = $this->postProcess($form, $request, $response)) { |
||||||
55 | return $response; |
||||||
56 | } |
||||||
57 | |||||||
58 | return null; |
||||||
59 | } |
||||||
60 | |||||||
61 | /** |
||||||
62 | * Executes before form validating and processing is started. |
||||||
63 | * |
||||||
64 | * @param FormInterface $form |
||||||
65 | * @param Request $request |
||||||
66 | * |
||||||
67 | * @return Response|null |
||||||
68 | */ |
||||||
69 | protected function preProcess(FormInterface $form, Request $request): ?Response |
||||||
0 ignored issues
–
show
The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||||
70 | { |
||||||
71 | return null; |
||||||
72 | } |
||||||
73 | |||||||
74 | /** |
||||||
75 | * Executes after preprocessing and before form processing is started. |
||||||
76 | * |
||||||
77 | * @param FormInterface $form |
||||||
78 | * @param Request $request |
||||||
79 | * |
||||||
80 | * @return Response|null |
||||||
81 | */ |
||||||
82 | protected function validate(FormInterface $form, Request $request): ?Response |
||||||
83 | { |
||||||
84 | return null; |
||||||
85 | } |
||||||
86 | |||||||
87 | /** |
||||||
88 | * Executes the form processing. |
||||||
89 | * |
||||||
90 | * @param FormInterface $form |
||||||
91 | * @param Request $request |
||||||
92 | * |
||||||
93 | * @return bool |
||||||
94 | */ |
||||||
95 | abstract protected function process(FormInterface $form, Request $request): bool; |
||||||
96 | |||||||
97 | /** |
||||||
98 | * Executes after form processing is finished and filters response. |
||||||
99 | * |
||||||
100 | * @param FormInterface $form |
||||||
101 | * @param Request $request |
||||||
102 | * @param Response $response |
||||||
103 | * |
||||||
104 | * @return Response|null |
||||||
105 | */ |
||||||
106 | protected function postProcess(FormInterface $form, Request $request, Response $response): ?Response |
||||||
0 ignored issues
–
show
The parameter
$form is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
The parameter
$request is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.
Loading history...
|
|||||||
107 | { |
||||||
108 | return $response; |
||||||
109 | } |
||||||
110 | } |
||||||
111 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.