ItemOperationController::__invoke()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 28
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 28
rs 9.6333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LAG\AdminBundle\Controller;
6
7
use LAG\AdminBundle\Metadata\AdminResource;
8
use LAG\AdminBundle\Request\Context\ContextProviderInterface;
9
use LAG\AdminBundle\Request\Uri\UriVariablesExtractorInterface;
10
use LAG\AdminBundle\Routing\UrlGenerator\UrlGeneratorInterface;
11
use LAG\AdminBundle\State\DataProcessorInterface;
12
use LAG\AdminBundle\State\DataProviderInterface;
13
use Symfony\Component\Form\FormFactoryInterface;
14
use Symfony\Component\HttpFoundation\RedirectResponse;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpFoundation\Response;
17
use Twig\Environment;
18
19
class ItemOperationController
20
{
21
    public function __construct(
22
        private UriVariablesExtractorInterface $uriVariablesExtractor,
23
        private ContextProviderInterface $contextProvider,
24
        private DataProviderInterface $dataProvider,
25
        private DataProcessorInterface $dataProcessor,
26
        private FormFactoryInterface $formFactory,
27
        private Environment $environment,
28
        private UrlGeneratorInterface $urlGenerator,
29
    ) {
30
    }
31
32
    public function __invoke(Request $request, AdminResource $resource): Response
33
    {
34
        $operation = $resource->getCurrentOperation();
35
        $uriVariables = $this->uriVariablesExtractor->extractVariables($operation, $request);
36
        $context = $this->contextProvider->getContext($operation, $request);
37
        $data = $this->dataProvider->provide($operation, $uriVariables, $context);
38
        $form = null;
39
40
        if ($operation->getFormType()) {
41
            $form = $this->formFactory->create($operation->getFormType(), $data, $operation->getFormOptions());
42
            $form->handleRequest($request);
43
44
            if ($form->isSubmitted() && $form->isValid()) {
45
                $data = $form->getData();
46
                $this->dataProcessor->process($data, $operation, $uriVariables, $context);
47
48
                return new RedirectResponse($this->urlGenerator->generateFromRouteName(
49
                    $operation->getTargetRoute() ?? $operation->getRoute(),
0 ignored issues
show
Bug introduced by
It seems like $operation->getTargetRou... $operation->getRoute() can also be of type null; however, parameter $routeName of LAG\AdminBundle\Routing\...generateFromRouteName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
                    /** @scrutinizer ignore-type */ $operation->getTargetRoute() ?? $operation->getRoute(),
Loading history...
50
                    $operation->getTargetRouteParameters() ?? $operation->getRouteParameters(),
0 ignored issues
show
Bug introduced by
It seems like $operation->getTargetRou...n->getRouteParameters() can also be of type null; however, parameter $routeParameters of LAG\AdminBundle\Routing\...generateFromRouteName() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
                    /** @scrutinizer ignore-type */ $operation->getTargetRouteParameters() ?? $operation->getRouteParameters(),
Loading history...
51
                ));
52
            }
53
        }
54
55
        return new Response($this->environment->render($operation->getTemplate(), [
56
            'resource' => $resource,
57
            'operation' => $operation,
58
            'data' => $data,
59
            'form' => $form?->createView(),
60
        ]));
61
    }
62
}
63