Passed
Branch main (b6a268)
by Iain
04:11
created

RedirectRequestHandler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 37
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A generateDefaultOutput() 0 7 2
A handleForm() 0 3 1
A generateSuccessOutput() 0 3 1
A __construct() 0 2 1
A supports() 0 3 3
A generateErrorOutput() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Humbly Arrogant Ltd 2020-2022.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.0.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\Common\RequestHandler;
16
17
use Symfony\Component\Form\FormInterface;
18
use Symfony\Component\HttpFoundation\RedirectResponse;
19
use Symfony\Component\HttpFoundation\Request;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22
23
class RedirectRequestHandler implements RequestHandlerInterface
24
{
25
    public function __construct(private UrlGeneratorInterface $urlGenerator, private string $urlPath, private string $route)
26
    {
27
    }
28
29
    public function supports(Request $request): bool
30
    {
31
        return (null === $request->getContentType() || 'form' === $request->getContentType()) && $this->route === $request->get('_route');
32
    }
33
34
    public function handleForm(FormInterface $form, Request $request, array $extraOutput = []): void
35
    {
36
        $form->handleRequest($request);
37
    }
38
39
    public function generateDefaultOutput(?FormInterface $form, array $extraOutput = []): array|Response
40
    {
41
        if ($form) {
42
            $extraOutput['form'] = $form->createView();
43
        }
44
45
        return $extraOutput;
46
    }
47
48
    public function generateSuccessOutput(?FormInterface $form, array $extraOutput = []): array|Response
49
    {
50
        return new RedirectResponse($this->urlGenerator->generate($this->urlPath));
51
    }
52
53
    public function generateErrorOutput(?FormInterface $form, array $extraOutput = []): array|Response
54
    {
55
        if ($form) {
56
            $extraOutput['form'] = $form->createView();
57
        }
58
59
        return $extraOutput;
60
    }
61
}
62