Completed
Push — master ( 2eb0ca...af375c )
by Marko
14s
created

RetrieveFormFieldElementAction   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 9
dl 0
loc 97
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __invoke() 0 43 4
A getFormRenderer() 0 20 3
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\Bridge\Twig\AppVariable;
19
use Symfony\Bridge\Twig\Command\DebugCommand;
20
use Symfony\Bridge\Twig\Extension\FormExtension;
21
use Symfony\Bridge\Twig\Form\TwigRenderer;
22
use Symfony\Component\Form\FormRenderer;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26
use Twig\Environment;
27
28
final class RetrieveFormFieldElementAction
29
{
30
    /**
31
     * @var Pool
32
     */
33
    private $pool;
34
35
    /**
36
     * @var AdminHelper
37
     */
38
    private $helper;
39
40
    /**
41
     * @var Environment
42
     */
43
    private $twig;
44
45
    public function __construct(Environment $twig, Pool $pool, AdminHelper $helper)
46
    {
47
        $this->pool = $pool;
48
        $this->helper = $helper;
49
        $this->twig = $twig;
50
    }
51
52
    /**
53
     * @throws NotFoundHttpException
54
     *
55
     * @return Response
56
     */
57
    public function __invoke(Request $request)
58
    {
59
        $code = $request->get('code');
60
        $elementId = $request->get('elementId');
61
        $objectId = $request->get('objectId');
62
        $uniqid = $request->get('uniqid');
63
64
        $admin = $this->pool->getInstance($code);
65
        $admin->setRequest($request);
66
67
        if ($uniqid) {
68
            $admin->setUniqid($uniqid);
69
        }
70
71
        if ($objectId) {
72
            $subject = $admin->getObject($objectId);
73
            if (!$subject) {
74
                throw new NotFoundHttpException(sprintf(
75
                    'Unable to find the object id: %s, class: %s',
76
                    $objectId,
77
                    $admin->getClass()
78
                ));
79
            }
80
        } else {
81
            $subject = $admin->getNewInstance();
82
        }
83
84
        $admin->setSubject($subject);
85
86
        $formBuilder = $admin->getFormBuilder();
87
88
        $form = $formBuilder->getForm();
89
        $form->setData($subject);
90
        $form->handleRequest($request);
91
92
        $view = $this->helper->getChildFormView($form->createView(), $elementId);
93
94
        // render the widget
95
        $renderer = $this->getFormRenderer();
96
        $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 92 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...
97
98
        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 92 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...
99
    }
100
101
    /**
102
     * @return FormRenderer|TwigRenderer
103
     */
104
    private function getFormRenderer()
105
    {
106
        // BC for Symfony < 3.2 where this runtime does not exists
107
        if (!method_exists(AppVariable::class, 'getToken')) {
108
            $extension = $this->twig->getExtension(FormExtension::class);
109
            $extension->initRuntime($this->twig);
110
111
            return $extension->renderer;
0 ignored issues
show
Bug introduced by
Accessing renderer on the interface Twig_ExtensionInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
112
        }
113
114
        // BC for Symfony < 3.4 where runtime should be TwigRenderer
115
        if (!method_exists(DebugCommand::class, 'getLoaderPaths')) {
116
            $runtime = $this->twig->getRuntime(TwigRenderer::class);
117
            $runtime->setEnvironment($this->twig);
118
119
            return $runtime;
120
        }
121
122
        return $this->twig->getRuntime(FormRenderer::class);
123
    }
124
}
125