Completed
Push — 3.x ( 04f815...b35b79 )
by Oskar
03:03
created

AppendFormFieldElementAction::__invoke()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 9.0328
c 0
b 0
f 0
cc 5
nc 6
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\AdminBundle\Action;
15
16
use Sonata\AdminBundle\Admin\AdminHelper;
17
use Sonata\AdminBundle\Admin\Pool;
18
use Symfony\Component\Form\Form;
19
use Symfony\Component\Form\FormRenderer;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
23
use Twig\Environment;
24
25
final class AppendFormFieldElementAction
26
{
27
    /**
28
     * @var Pool
29
     */
30
    private $pool;
31
32
    /**
33
     * @var AdminHelper
34
     */
35
    private $helper;
36
37
    /**
38
     * @var Environment
39
     */
40
    private $twig;
41
42
    public function __construct(Environment $twig, Pool $pool, AdminHelper $helper)
43
    {
44
        $this->pool = $pool;
45
        $this->helper = $helper;
46
        $this->twig = $twig;
47
    }
48
49
    /**
50
     * @throws NotFoundHttpException
51
     */
52
    public function __invoke(Request $request): Response
53
    {
54
        $code = $request->get('code');
55
        $elementId = $request->get('elementId');
56
        $objectId = $request->get('objectId');
57
        $uniqid = $request->get('uniqid');
58
59
        $admin = $this->pool->getInstance($code);
60
        $admin->setRequest($request);
61
62
        if ($uniqid) {
63
            $admin->setUniqid($uniqid);
64
        }
65
66
        $subject = $admin->getObject($objectId);
67
        if ($objectId && !$subject) {
68
            throw new NotFoundHttpException(sprintf('Could not find subject for id "%s"', $objectId));
69
        }
70
71
        if (!$subject) {
72
            $subject = $admin->getNewInstance();
73
        }
74
75
        $admin->setSubject($subject);
76
77
        list(, $form) = $this->helper->appendFormFieldElement($admin, $subject, $elementId);
78
79
        \assert($form instanceof Form);
80
        $view = $this->helper->getChildFormView($form->createView(), $elementId);
81
82
        // render the widget
83
        $renderer = $this->getFormRenderer();
84
        $renderer->setTheme($view, $admin->getFormTheme());
0 ignored issues
show
Bug introduced by
It seems like $view defined by $this->helper->getChildF...eateView(), $elementId) on line 80 can be null; however, Symfony\Component\Form\FormRenderer::setTheme() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
85
86
        return new Response($renderer->searchAndRenderBlock($view, 'widget'));
0 ignored issues
show
Bug introduced by
It seems like $view defined by $this->helper->getChildF...eateView(), $elementId) on line 80 can be null; however, Symfony\Component\Form\F...:searchAndRenderBlock() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
87
    }
88
89
    private function getFormRenderer(): FormRenderer
90
    {
91
        return $this->twig->getRuntime(FormRenderer::class);
92
    }
93
}
94